Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to create one excel sheet where, I want to create tab with all date of particular month's. like tab name- 1 jan, 2jan, 3jan.....etc. also I have date-wise datatable like datatable1jan,datatble2jan,datatable3jan....etc. I want to create excel with this datatable and tab name.

I know how to create excel using Interlop which as folllows,


C#
Public static void CreateExcel_TabNameUnique( DataTable dt1,string ExcelFilePath, string Tab_Name1)
       {
           try
           {
               runDate = DateTime.Today;
               currMonth = DateTime.Today;
               object missing = Type.Missing;
               Excel.Application oXL = new Excel.Application();
               oXL.Visible = false;
               Excel.Workbook oWB = oXL.Workbooks.Add(missing);
               Excel.Worksheet oSheet = oWB.ActiveSheet as Excel.Worksheet;
               oSheet.Name = Tab_Name1;
               // column headings
               for (int i = 0; i < dt1.Columns.Count; i++)
               {
                   oSheet.Cells[1, (i + 1)] = dt1.Columns[i].ColumnName;
               }
               // rows
               for (int i = 0; i < dt1.Rows.Count; i++)
               {
                   for (int j = 0; j < dt1.Columns.Count; j++)
                   {
                       oSheet.Cells[(i + 2), (j + 1)] = dt1.Rows[i][j];
                   }
               }


               if (ExcelFilePath != null && ExcelFilePath != "")
               {
                   try
                   {
                       oWB.SaveAs(ExcelFilePath, Excel.XlFileFormat.xlOpenXMLWorkbook,
                   missing, missing, missing, missing,
                   Excel.XlSaveAsAccessMode.xlNoChange,
                   missing, missing, missing, missing, missing);
                       oWB.Close(missing, missing, missing);
                       oXL.UserControl = true;
                       oXL.Quit();
                   }
                   catch (Exception ex)
                   {
                       throw new Exception("ExportToExcel: Excel file could not be saved! Check filepath.\n"
                           + ex.Message);
                   }
               }
           }
           catch (Exception ex)
           {
           }

       }


Please help me.
Posted
Comments
Nathan Minier 5-Jan-16 8:01am    
This isn't necessarily pertinent, but I'd advise trying OpenXML instead, since the Interop library requires an Office installation on the executing computer while OpenXML just needs .NET.

There's a tutorial at:
http://www.codeproject.com/Articles/670141/Read-and-Write-Microsoft-Excel-with-Open-XML-SDK
Member 12036327 5-Jan-16 23:47pm    
@ Nathan Minier - No Interlop is ok for me. I have no permission to use other than Interlop. and my question is different.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900