2014年7月21日 星期一

ASP.NET DataTable轉成Excel實體檔案

ASP.NET DataTable轉成Excel實體檔案


C#:
  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. #region 建立虛擬表格
  4. DataTable dt = new DataTable();
  5. DataColumn column;
  6. column = new DataColumn();
  7. column.DataType = Type.GetType("System.String");
  8. column.ColumnName = "company_name";
  9. dt.Columns.Add(column);
  10. column = new DataColumn();
  11. column.DataType = Type.GetType("System.String");
  12. column.ColumnName = "tel";
  13. dt.Columns.Add(column);
  14. column = new DataColumn();
  15. column.DataType = Type.GetType("System.String");
  16. column.ColumnName = "fax";
  17. dt.Columns.Add(column);
  18. column = new DataColumn();
  19. column.DataType = Type.GetType("System.String");
  20. column.ColumnName = "email";
  21. dt.Columns.Add(column);
  22. column = new DataColumn();
  23. column.DataType = Type.GetType("System.String");
  24. column.ColumnName = "contact";
  25. dt.Columns.Add(column);
  26. column = new DataColumn();
  27. column.DataType = Type.GetType("System.String");
  28. column.ColumnName = "company_address";
  29. dt.Columns.Add(column);
  30. #endregion
  31.  
  32. //輸出
  33. ExcelFileStream(dt, @"D:\", "abc.xls");
  34. }
  1. private void ExcelFileStream(DataTable dt, string savepath, string filename)
  2. {
  3. try
  4. {
  5. Stream fs = RenderDataTableToExcel(dt);
  6. SaveStreamToFile(savepath + filename, fs);
  7. }
  8. catch (Exception)
  9. {
  10. throw;
  11. }
  12. }
  1. public static Stream RenderDataTableToExcel(DataTable srcTable)
  2. {
  3. try
  4. {
  5. HSSFWorkbook workbook = new HSSFWorkbook();
  6. HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet();
  7. HSSFRow headerRow = (HSSFRow)sheet.CreateRow(0);
  8. foreach (DataColumn column in srcTable.Columns)
  9. {
  10. headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
  11. }
  12. int rowIndex = 1;
  13. foreach (DataRow row in srcTable.Rows)
  14. {
  15. HSSFRow dataRow = (HSSFRow)sheet.CreateRow(rowIndex);
  16. foreach (DataColumn column in srcTable.Columns)
  17. {
  18. dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
  19. }
  20. rowIndex++;
  21. }
  22.  
  23. MemoryStream stream = new MemoryStream();
  24. workbook.Write(stream);
  25. stream.Flush();
  26. stream.Position = 0;
  27. sheet = null;
  28. headerRow = null;
  29. workbook = null;
  30. return stream;
  31. }
  32. catch (Exception)
  33. {
  34.  
  35. throw;
  36. }
  37.  
  38. }
  1. public void SaveStreamToFile(string fileFullPath, Stream stream)
  2. {
  3. try
  4. {
  5. if (stream.Length == 0) return;
  6.  
  7. // Create a FileStream object to write a stream to a file
  8. using (FileStream fileStream = System.IO.File.Create(fileFullPath, (int)stream.Length))
  9. {
  10. // Fill the bytes[] array with the stream data
  11. byte[] bytesInStream = new byte[stream.Length];
  12. stream.Read(bytesInStream, 0, (int)bytesInStream.Length);
  13.  
  14. // Use FileStream object to write to the specified file
  15. fileStream.Write(bytesInStream, 0, bytesInStream.Length);
  16. }
  17. }
  18. catch (Exception)
  19. {
  20. throw;
  21. }
  22.  
  23. }

沒有留言:

張貼留言