使用ExcelPackage生成Excel2007电子表格

ExcelPackage是一个在服务端生成Excel 2007 电子表格的Library. 下面的代码是生成XLSX文件示例:

   1:    FileInfo XLSXFile = new FileInfo(XLSXFileName);

   2:    using (ExcelPackage xlPackage = new ExcelPackage(XLSXFile))

   3:    {

   4:        ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets.Add("Sheet1");

   5:  

 6:  int row = 1;  7:  foreach (DataRow dr in dataTable.Rows)  8:  {  9:  int col = 1;  10:    11:  foreach (object o in dr.ItemArray)  12:  {  13:  worksheet.Cell(row, col++).Value = o.ToString().Trim();  14:  }  15:    16:  row++;  17:  }  18:    19:  xlPackage.Save();  20:  }

是不是很简单?,希望对您有帮助!

Author: Petter Liu http://wintersun.cnblogs.com

你可能感兴趣的:(C#)