DataTable数据存入指定路径Excel文件(套用Excel模板)

如题,此方法将DataTable的数据存入指定路径的Excel文件中,并套用现成的Excel模板的样式来显示数据,可以通过每个Sheet中指定写入的行数,来将数据写到不同的sheet中,类似数据分页的功能。

具体如下:

  1         /// <summary>
  2         /// 将DataTable数据写入Excel文件(套用模板并分页)
  3         /// </summary>
  4         /// <param name="dt">DataTable</param>
  5         /// <param name="rows">每个WorkSheet写入多少行数据</param>
  6         /// <param name="top">行索引</param>
  7         /// <param name="left">列索引</param>
  8         /// <param name="sheetPrefixName">WorkSheet前缀名,比如:前缀名为“Sheet”,那么WorkSheet名称依次为“Sheet-,Sheet-”</param>
  9         public void DataTableToExcel(DataTable dt, int rows, int top, int left, string sheetPrefixName, Label lb)
 10         {
 11             int rowCount = dt.Rows.Count;        //源DataTable行数
 12             int colCount = dt.Columns.Count;    //源DataTable列数
 13             int sheetCount = this.GetSheetCount(rowCount, rows);    //WorkSheet个数
 14             DateTime beforeTime;
 15             DateTime afterTime;
 16 
 17             if (sheetPrefixName == null || sheetPrefixName.Trim() == "")
 18                 sheetPrefixName = "Sheet";
 19 
 20             //创建一个Application对象并使其可见
 21             beforeTime = DateTime.Now;
 22             Excel.Application app = new Excel.Application();
 23             app.Visible = false;
 24             afterTime = DateTime.Now;
 25 
 26             //打开模板文件,得到WorkBook对象
 27             Excel.Workbook workBook = app.Workbooks.Open(templetFile, missing, missing, missing, missing, missing,
 28                                 missing, missing, missing, missing, missing, missing, missing);
 29 
 30             //得到WorkSheet对象
 31             Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(1);
 32 
 33             //复制sheetCount-个WorkSheet对象
 34             for (int i = 1; i < sheetCount; i++)
 35             {
 36                 ((Excel.Worksheet)workBook.Worksheets.get_Item(i)).Copy(missing, workBook.Worksheets[i]);
 37             }
 38 
 39             //将源DataTable数据写入Excel#region 将源DataTable数据写入Excel
 40             for (int i = 1; i <= sheetCount; i++)
 41             {
 42                 int startRow = (i - 1) * rows;        //记录起始行索引
 43                 int endRow = i * rows;                //记录结束行索引
 44 
 45                 //若是最后一个WorkSheet,那么记录结束行索引为源DataTable行数
 46                 if (i == sheetCount)
 47                     endRow = rowCount;
 48 
 49 
 50                 //获取要写入数据的WorkSheet对象,并重命名
 51                 Excel.Worksheet sheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
 52                 sheet.Name = sheetPrefixName + "-" + i.ToString();
 53 
 54                 //将dt中的数据写入WorkSheet
 55                 for (int j = 0; j < endRow - startRow; j++)
 56                 {
 57                     for (int k = 0; k < colCount; k++)
 58                     {
 59                         sheet.Cells[top + j, left + k] = dt.Rows[startRow + j][k].ToString();
 60                     }
 61                     lb.Text = "Saving lines: " + sheet.Name + "+" + (1 + j) + "/" + (dt.Rows.Count);
 62                 }
 63             }
 64 
 65             //输出Excel文件并退出
 66             try
 67             {
 68                 workBook.SaveAs(outputFile, missing, missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing);
 69                 workBook.Close(null, null, null);
 70                 app.Workbooks.Close();
 71                 app.Application.Quit();
 72                 app.Quit();
 73 
 74                 System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
 75                 System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
 76                 System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
 77 
 78                 workSheet = null;
 79                 workBook = null;
 80                 app = null;
 81 
 82                 GC.Collect();
 83             }
 84             catch (Exception e)
 85             {
 86                 throw e;
 87             }
 88             finally
 89             {
 90                 Process[] myProcesses;
 91                 DateTime startTime;
 92                 myProcesses = Process.GetProcessesByName("Excel");
 93 
 94                 //得不到Excel进程ID,暂时只能判断进程启动时间
 95                 foreach (Process myProcess in myProcesses)
 96                 {
 97                     startTime = myProcess.StartTime;
 98 
 99                     if (startTime > beforeTime && startTime < afterTime)
100                     {
101                         myProcess.Kill();
102                     }
103                 }
104             }
105 
106         }

 

posted @ 2012-05-04 10:36  IT女汉纸  阅读(674)  评论(0编辑  收藏  举报