How to export data in excel sheet

  public static void exportExcel(System.Data.DataTable dt, string fileName,string logoImagePath,string compName,string compDetails,string reportTitle)
        {
            // BackUp objBackUp = new BackUp();
            /*
             * un comment following code for password protected excel file
             */
            //Filename = AppDomain.CurrentDomain.BaseDirectory + fileName.Trim()+".xls";
            Filename = fileName;
            FileInfo f = new FileInfo(Filename);
            if (File.Exists(Filename))
            {
                try
                {
                    File.Delete(Filename);
                }
                catch
                {
                    MessageBox.Show("File is in use. Please Close Report.xls File...");
                    return;
                }
            }
            try
            {

                //File name and path, here i used abc file to be

                //stored in Bin directory in the sloution directory
                Microsoft.Office.Interop.Excel.Application exc = new Microsoft.Office.Interop.Excel.Application();

                //check if file already exists then delete

                //it to create a new file

                if (File.Exists(Filename))
                    File.Delete(Filename);
                if (!File.Exists(Filename))
                {
                    //create new excel application               
                    Microsoft.Office.Interop.Excel.Application oexcel = new Microsoft.Office.Interop.Excel.Application();

                    oexcel.DisplayAlerts = false;                //add a new workbook

                    obook = oexcel.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);

                    oexcel.Visible = false;
                    osheet = (Microsoft.Office.Interop.Excel.Worksheet)oexcel.Worksheets[1];
                    osheet.Name = "Sheet1";

                    Range picRange = (Range)osheet.get_Range("A1", "C1");
                    picRange.Merge(true);
                    picRange.RowHeight = 60;
                    Image img = Image.FromFile(logoImagePath);
                    Clipboard.SetImage(img);
                    osheet.Paste(picRange, logoImagePath);

                   
                    Range rangeCompName = (Range)osheet.get_Range("E1", "J1");
                    rangeCompName.Merge(true);
                    rangeCompName.Font.Bold = true;
                    rangeCompName.Font.Size = 16;
                    rangeCompName.Value2 = compName;

                    Range rangeCompDetails = (Range)osheet.get_Range("C2", "J2");
                    rangeCompDetails.Merge(true);
                    rangeCompDetails.RowHeight = 40;
                    rangeCompDetails.Font.Bold = true;
                    rangeCompDetails.Font.Size = 12;
                    rangeCompDetails.Font.Underline = true;
                    rangeCompDetails.Value2 = compDetails;

                    Range rangeReportTitle = (Range)osheet.get_Range("A3", "J3");
                    rangeReportTitle.Merge(true);
                    rangeReportTitle.Font.Bold = true;
                    rangeReportTitle.Font.Size = 12;
                    rangeReportTitle.HorizontalAlignment = HorizontalAlignment.Center;
                    rangeReportTitle.Value2 = reportTitle;

                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        osheet.Cells[6, i + 1] = dt.Columns[i].ColumnName.ToString();
                        double w = GetExcelColWidths(dt, i);
                        if (w < 255)
                        {
                            ((Range)osheet.Cells[6, i + 1]).ColumnWidth = w;
                        }
                        else
                        {
                            ((Range)osheet.Cells[6, i + 1]).ColumnWidth = 255;
                        }
                        ((Range)osheet.Cells[6, i + 1]).HorizontalAlignment = HorizontalAlignment.Center;                      
                    }
                    osheet.get_Range("A6", "AZ6").Font.Bold = true; ;
                   
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        for (int j = 0; j < dt.Columns.Count; j++)
                        {
                            Range range = (Range)osheet.Cells[i + 8, j + 1];
                            range.Value2 = dt.Rows[i][j] == null ? "" : dt.Rows[i][j].ToString();
                            range.HorizontalAlignment = HorizontalAlignment.Center;                           
                        }
                    }                   
                    oexcel.ActiveWorkbook.SaveCopyAs(Filename);                   
                    osheet = null;
                    obook.Close(true, Filename, Missing.Value);
                    obook = null;
                    oexcel.Quit();
                    oexcel = null;
                }
            }
            catch
            {
                //MessageBox.Show(ex.Message);
            }
        }


-------------------------------

 public static void genReport(DataTable dtReport, string compName, string compDetails)
        {
            if (dtReport != null && dtReport.Rows.Count > 0)
            {
                string fileName = Common.strPathExcelReport;
                if (string.IsNullOrEmpty(fileName))
                {
                    Common.MsgBox(MessageType.ForInvalidFile);
                    return;
                }
                string logoFile = Common.strPathExcelReportLogo;
                string ProjectName = "";
                ProjectName = "";
                cls_GenExcel.exportExcel(dtReport, fileName, logoFile, compName, ProjectName, compDetails);
                FileDownload fileDownload = new FileDownload(fileName, Common.ReadExcel(fileName), FilePath.FileNameWithExtension);
            }
            else
            {
                MessageBox.Show("No Data Found. Report Couldn't Generated...");
            }
        }

Comments

Popular posts from this blog

ASP.NET Session States in SQL Server Mode

How to find client's MAC Address in Asp.Net and C#.net

Use XML Data For Save in Database