如何使用Interop在C#中创建具有特定月份日期明智表的Excel文件

时间:2016-01-06 04:59:13

标签: c# excel datatable

我想创建一个Excel工作表,我想在其中创建包含特定月份所有日期的标签。例如选项卡名称:1 jan,2jan,3jan .....等。

另外,我有日期数据,如datatable1jan,datatble2jan,datatable3jan ....等。我想用这个数据表和选项卡名称创建Excel文件。

我知道如何使用interop创建Excel文件:

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)
               {
               }
           }
       }
       catch (Exception ex)
       {
       } 
   }

0 个答案:

没有答案