ExcelHelper.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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, bool isNeedNumeric = 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. for (int i = 0; i < ds.Tables.Count; i++)
  38. {
  39. worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
  40. worksheetPart.Worksheet = new Worksheet(new SheetData());
  41. sheets = workbookpart.Workbook.GetFirstChild<Sheets>();
  42. uint sheetID = 1;
  43. if (sheets.Descendants<Sheet>().Count() > 0)
  44. {
  45. sheetID = sheets.Descendants<Sheet>().Select(S => S.SheetId.Value).Max() + 1;
  46. }
  47. Sheet sheet = new Sheet()
  48. {
  49. Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart),
  50. SheetId = sheetID,
  51. Name = ds.Tables[i].TableName,
  52. };
  53. sheets.Append(sheet);
  54. var sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
  55. Row rowCaption = new Row();
  56. for (int col = 0; col < ds.Tables[i].Columns.Count; col++)
  57. {
  58. Cell titleCell = new Cell();
  59. titleCell.CellValue = new CellValue(ds.Tables[i].Columns[col].Caption);
  60. titleCell.DataType = new EnumValue<CellValues>(CellValues.String);
  61. rowCaption.Append(titleCell);
  62. }
  63. sheetData.Append(rowCaption);
  64. for (int row = 0; row < ds.Tables[i].Rows.Count; row++)
  65. {
  66. Row rowData = new Row();
  67. for (int col = 0; col < ds.Tables[i].Columns.Count; col++)
  68. {
  69. Cell dataCell = new Cell();
  70. var data = ds.Tables[i].Rows[row][col];
  71. bool isNumeric = false;
  72. if (isNeedNumeric)
  73. {
  74. if (data != null)
  75. {
  76. if (double.TryParse(data.ToString(), out double doubleValue))
  77. {
  78. // 转换为 double 类型成功,表示为数字
  79. isNumeric = true;
  80. }
  81. else if (int.TryParse(data.ToString(), out int intValue))
  82. {
  83. isNumeric = true;
  84. }
  85. else if (float.TryParse(data.ToString(), out float floatValue))
  86. {
  87. isNumeric = true;
  88. }
  89. }
  90. }
  91. if (isNumeric)
  92. {
  93. // 处理数字类型的值
  94. dataCell.CellValue = new CellValue(data.ToString());
  95. dataCell.DataType = new EnumValue<CellValues>(CellValues.Number);
  96. }
  97. else
  98. {
  99. // 处理非数字类型的值
  100. dataCell.CellValue = new CellValue(data.ToString());
  101. dataCell.DataType = new EnumValue<CellValues>(CellValues.String);
  102. }
  103. //if (data is DateTime time)
  104. //{
  105. // dataCell.CellValue = new CellValue(time);
  106. // dataCell.DataType = new EnumValue<CellValues>(CellValues.String);
  107. //}
  108. //else if (data is double value)
  109. //{
  110. // dataCell.CellValue = new CellValue(data.ToString());
  111. // dataCell.DataType = new EnumValue<CellValues>(CellValues.String);
  112. //}
  113. //else if (data is int intValue)
  114. //{
  115. // dataCell.CellValue = new CellValue(intValue.ToString());
  116. // dataCell.DataType = new EnumValue<CellValues>(CellValues.Number);
  117. //}
  118. //else if (data is float floatValue)
  119. //{
  120. // dataCell.CellValue = new CellValue(floatValue.ToString());
  121. // dataCell.DataType = new EnumValue<CellValues>(CellValues.Number);
  122. //}
  123. //else
  124. //{
  125. // dataCell.CellValue = new CellValue(data.ToString());
  126. // dataCell.DataType = new EnumValue<CellValues>(CellValues.String);
  127. //}
  128. rowData.Append(dataCell);
  129. }
  130. sheetData.Append(rowData);
  131. }
  132. }
  133. workbookpart.Workbook.Save();
  134. spreadsheetDocument.Close();
  135. }
  136. catch (Exception ex)
  137. {
  138. LOG.Write(ex);
  139. reason = ex.Message;
  140. return false;
  141. }
  142. return true;
  143. }
  144. public static bool ExportProcessLogToExcel(string filepath, DataSet ds, out string reason, bool createNewFile = true)
  145. {
  146. reason = string.Empty;
  147. try
  148. {
  149. SpreadsheetDocument spreadsheetDocument;
  150. WorkbookPart workbookpart;
  151. WorksheetPart worksheetPart;
  152. Sheets sheets;
  153. if (createNewFile)
  154. {
  155. spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook);
  156. workbookpart = spreadsheetDocument.AddWorkbookPart();
  157. workbookpart.Workbook = new Workbook();
  158. sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
  159. }
  160. else
  161. {
  162. spreadsheetDocument = SpreadsheetDocument.Open(filepath, true);
  163. workbookpart = spreadsheetDocument.WorkbookPart;
  164. workbookpart.Workbook = new Workbook();
  165. sheets = spreadsheetDocument.WorkbookPart.Workbook.Sheets;
  166. }
  167. for (int i = 0; i < ds.Tables.Count; i++)
  168. {
  169. worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
  170. worksheetPart.Worksheet = new Worksheet(new SheetData());
  171. sheets = workbookpart.Workbook.GetFirstChild<Sheets>();
  172. uint sheetID = 1;
  173. if (sheets.Descendants<Sheet>().Count() > 0)
  174. {
  175. sheetID = sheets.Descendants<Sheet>().Select(S => S.SheetId.Value).Max() + 1;
  176. }
  177. Sheet sheet = new Sheet()
  178. {
  179. Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart),
  180. SheetId = sheetID,
  181. Name = ds.Tables[i].TableName,
  182. };
  183. sheets.Append(sheet);
  184. var sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
  185. Row rowCaption = new Row();
  186. for (int col = 0; col < ds.Tables[i].Columns.Count; col++)
  187. {
  188. Cell titleCell = new Cell();
  189. titleCell.CellValue = new CellValue(ds.Tables[i].Columns[col].Caption);
  190. titleCell.DataType = new EnumValue<CellValues>(CellValues.String);
  191. rowCaption.Append(titleCell);
  192. }
  193. sheetData.Append(rowCaption);
  194. for (int row = 0; row < ds.Tables[i].Rows.Count; row++)
  195. {
  196. Row rowData = new Row();
  197. for (int col = 0; col < ds.Tables[i].Columns.Count; col++)
  198. {
  199. Cell dataCell = new Cell();
  200. var data = ds.Tables[i].Rows[row][col];
  201. bool isNumeric = false;
  202. if (data != null)
  203. {
  204. if (double.TryParse(data.ToString(), out double doubleValue))
  205. {
  206. // 转换为 double 类型成功,表示为数字
  207. isNumeric = true;
  208. }
  209. else if (int.TryParse(data.ToString(), out int intValue))
  210. {
  211. isNumeric = true;
  212. }
  213. else if (float.TryParse(data.ToString(), out float floatValue))
  214. {
  215. isNumeric = true;
  216. }
  217. }
  218. dataCell.CellValue = new CellValue(data.ToString());
  219. // 处理数字类型的值 // 处理非数字类型的值
  220. dataCell.DataType = isNumeric ? new EnumValue<CellValues>(CellValues.Number) : new EnumValue<CellValues>(CellValues.String);
  221. rowData.Append(dataCell);
  222. }
  223. sheetData.Append(rowData);
  224. }
  225. }
  226. workbookpart.Workbook.Save();
  227. spreadsheetDocument.Close();
  228. }
  229. catch (Exception ex)
  230. {
  231. LOG.Write(ex);
  232. reason = ex.Message;
  233. return false;
  234. }
  235. return true;
  236. }
  237. public static bool ExportProcessLogToExcel(string filepath, DataSet ds, int fristRow, int endRow, out string reason, bool createNewFile = true)
  238. {
  239. reason = string.Empty;
  240. try
  241. {
  242. SpreadsheetDocument spreadsheetDocument;
  243. WorkbookPart workbookpart;
  244. WorksheetPart worksheetPart;
  245. Sheets sheets;
  246. if (createNewFile)
  247. {
  248. spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook);
  249. workbookpart = spreadsheetDocument.AddWorkbookPart();
  250. workbookpart.Workbook = new Workbook();
  251. sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
  252. }
  253. else
  254. {
  255. spreadsheetDocument = SpreadsheetDocument.Open(filepath, true);
  256. workbookpart = spreadsheetDocument.WorkbookPart;
  257. workbookpart.Workbook = new Workbook();
  258. sheets = spreadsheetDocument.WorkbookPart.Workbook.Sheets;
  259. }
  260. for (int i = 0; i < ds.Tables.Count; i++)
  261. {
  262. worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
  263. worksheetPart.Worksheet = new Worksheet(new SheetData());
  264. sheets = workbookpart.Workbook.GetFirstChild<Sheets>();
  265. uint sheetID = 1;
  266. if (sheets.Descendants<Sheet>().Count() > 0)
  267. {
  268. sheetID = sheets.Descendants<Sheet>().Select(S => S.SheetId.Value).Max() + 1;
  269. }
  270. Sheet sheet = new Sheet()
  271. {
  272. Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart),
  273. SheetId = sheetID,
  274. Name = ds.Tables[i].TableName,
  275. };
  276. sheets.Append(sheet);
  277. var sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
  278. Row rowCaption = new Row();
  279. for (int col = 0; col < ds.Tables[i].Columns.Count; col++)
  280. {
  281. Cell titleCell = new Cell();
  282. titleCell.CellValue = new CellValue(ds.Tables[i].Columns[col].Caption);
  283. titleCell.DataType = new EnumValue<CellValues>(CellValues.String);
  284. rowCaption.Append(titleCell);
  285. }
  286. sheetData.Append(rowCaption);
  287. for (int row = fristRow; row < endRow; row++)
  288. {
  289. Row rowData = new Row();
  290. for (int col = 0; col < ds.Tables[i].Columns.Count; col++)
  291. {
  292. Cell dataCell = new Cell();
  293. var data = ds.Tables[i].Rows[row][col];
  294. bool isNumeric = false;
  295. if (data != null)
  296. {
  297. if (double.TryParse(data.ToString(), out double doubleValue))
  298. {
  299. // 转换为 double 类型成功,表示为数字
  300. isNumeric = true;
  301. }
  302. else if (int.TryParse(data.ToString(), out int intValue))
  303. {
  304. isNumeric = true;
  305. }
  306. else if (float.TryParse(data.ToString(), out float floatValue))
  307. {
  308. isNumeric = true;
  309. }
  310. }
  311. dataCell.CellValue = new CellValue(data.ToString());
  312. // 处理数字类型的值 // 处理非数字类型的值
  313. dataCell.DataType = isNumeric ? new EnumValue<CellValues>(CellValues.Number) : new EnumValue<CellValues>(CellValues.String);
  314. rowData.Append(dataCell);
  315. }
  316. sheetData.Append(rowData);
  317. }
  318. }
  319. workbookpart.Workbook.Save();
  320. spreadsheetDocument.Close();
  321. }
  322. catch (Exception ex)
  323. {
  324. LOG.Write(ex);
  325. reason = ex.Message;
  326. return false;
  327. }
  328. return true;
  329. }
  330. }
  331. }