ExcelHelper.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Aitex.Core.RT.Log;
  8. using DocumentFormat.OpenXml;
  9. using DocumentFormat.OpenXml.Packaging;
  10. using DocumentFormat.OpenXml.Spreadsheet;
  11. namespace MECF.Framework.Common.Utilities
  12. {
  13. public class ExcelHelper
  14. {
  15. public static bool ExportToExcel(string filepath, DataSet ds, out string reason, bool createNewFile = true)
  16. {
  17. reason = string.Empty;
  18. try
  19. {
  20. SpreadsheetDocument spreadsheetDocument;
  21. WorkbookPart workbookpart;
  22. WorksheetPart worksheetPart;
  23. Sheets sheets;
  24. if (createNewFile)
  25. {
  26. spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook);
  27. workbookpart = spreadsheetDocument.AddWorkbookPart();
  28. workbookpart.Workbook = new Workbook();
  29. sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
  30. }
  31. else
  32. {
  33. spreadsheetDocument = SpreadsheetDocument.Open(filepath, true);
  34. workbookpart = spreadsheetDocument.WorkbookPart;
  35. sheets = spreadsheetDocument.WorkbookPart.Workbook.Sheets;
  36. }
  37. worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
  38. worksheetPart.Worksheet = new Worksheet(new SheetData());
  39. uint sheetID = 1;
  40. if (sheets.Descendants<Sheet>().Count() > 0)
  41. {
  42. sheetID = sheets.Descendants<Sheet>().Select(S => S.SheetId.Value).Max() + 1;
  43. }
  44. for (int i = 0; i < ds.Tables.Count; i++)
  45. {
  46. Sheet sheet = new Sheet()
  47. {
  48. Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart),
  49. SheetId = sheetID,
  50. Name = ds.Tables[i].TableName,
  51. };
  52. sheets.Append(sheet);
  53. var sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
  54. Row rowCaption = new Row();
  55. for (int col = 0; col < ds.Tables[i].Columns.Count; col++)
  56. {
  57. Cell titleCell = new Cell();
  58. titleCell.CellValue = new CellValue(ds.Tables[i].Columns[col].Caption);
  59. titleCell.DataType = new EnumValue<CellValues>(CellValues.String);
  60. rowCaption.Append(titleCell);
  61. }
  62. sheetData.Append(rowCaption);
  63. for (int row = 0; row < ds.Tables[i].Rows.Count; row++)
  64. {
  65. Row rowData = new Row();
  66. for (int col = 0; col < ds.Tables[i].Columns.Count; col++)
  67. {
  68. Cell dataCell = new Cell();
  69. var data = ds.Tables[i].Rows[row][col];
  70. if (data is DateTime time)
  71. {
  72. dataCell.CellValue = new CellValue(time);
  73. dataCell.DataType = new EnumValue<CellValues>(CellValues.String);
  74. }
  75. else if (data is double value)
  76. {
  77. dataCell.CellValue = new CellValue(data.ToString());
  78. dataCell.DataType = new EnumValue<CellValues>(CellValues.Number);
  79. }
  80. else
  81. {
  82. dataCell.CellValue = new CellValue(data.ToString());
  83. dataCell.DataType = new EnumValue<CellValues>(CellValues.String);
  84. }
  85. rowData.Append(dataCell);
  86. }
  87. sheetData.Append(rowData);
  88. }
  89. }
  90. workbookpart.Workbook.Save();
  91. spreadsheetDocument.Close();
  92. }
  93. catch (Exception ex)
  94. {
  95. LOG.Write(ex);
  96. reason = ex.Message;
  97. return false;
  98. }
  99. return true;
  100. }
  101. }
  102. }