A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© sunrise2 高级黑马   /  2014-7-21 21:42  /  918 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. public static DataTable ExcelToDataTable(string filePath)
  2.         {
  3.             DataTable dt = new DataTable();

  4.             HSSFWorkbook hssfworkbook;
  5.             using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
  6.             {
  7.                 hssfworkbook = new HSSFWorkbook(file);
  8.             }
  9.             ISheet sheet = hssfworkbook.GetSheetAt(0);
  10.             System.Collections.IEnumerator rows = sheet.GetRowEnumerator();

  11.             IRow headerRow = sheet.GetRow(0);
  12.             int cellCount = headerRow.LastCellNum;

  13.             for (int j = 0; j < cellCount; j++)
  14.             {
  15.                 ICell cell = headerRow.GetCell(j);
  16.                 dt.Columns.Add(cell.ToString());
  17.             }

  18.             for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
  19.             {
  20.                 IRow row = sheet.GetRow(i);
  21.                 DataRow dataRow = dt.NewRow();
  22.                 if (row == null)
  23.                 {
  24.                     break;
  25.                 }
  26.                 for (int j = row.FirstCellNum; j < cellCount; j++)
  27.                 {
  28.                     if (row.GetCell(j) != null)
  29.                         dataRow[j] = row.GetCell(j).ToString();
  30.                 }

  31.                 dt.Rows.Add(dataRow);
  32.             }
  33.             return dt;
  34.         }
复制代码
导出到excel
  1. public static MemoryStream DataToExcel(DataTable dt)
  2.         {
  3.             MemoryStream ms = new MemoryStream();
  4.             using (dt)
  5.             {
  6.                 IWorkbook workbook = new HSSFWorkbook();//创建excel工作簿
  7.                 ISheet sheet = workbook.CreateSheet();//在该表中创建工作表
  8.                 IRow headerRow = sheet.CreateRow(0); //在表中添加一行
  9.                 foreach (DataColumn column in dt.Columns)
  10.                     headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption);
  11.                 int rowIndex = 1;
  12.                 foreach (DataRow row in dt.Rows)
  13.                 {
  14.                     IRow dataRow = sheet.CreateRow(rowIndex);
  15.                     foreach (DataColumn column in dt.Columns)
  16.                     {
  17.                         dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
  18.                     }
  19.                     rowIndex++;
  20.                 }
  21.                 workbook.Write(ms);
  22.                 ms.Flush();
  23.                 ms.Position = 0;
  24.             }
  25.             return ms;
  26.         }
复制代码
接着

  1. MemoryStream ms = ExcelHelper.DataToExcel(dt);
  2. FileStream fs = new FileStream("e:\\2.xls", FileMode.Create);
  3. ms.WriteTo(fs);
  4. fs.Close();
  5. ms.Close();
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马