ExcelExport.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. using NPOI.SS.UserModel;
  2. using NPOI.SS.UserModel.Charts;
  3. using NPOI.SS.Util;
  4. using NPOI.XSSF.UserModel;
  5. using OTSCommon.DBOperate.Model;
  6. using OTSIncAReportApp.DataOperation.DataAccess;
  7. using OTSIncAReportMailInterface;
  8. using System;
  9. using System.Data;
  10. using System.Drawing;
  11. using System.IO;
  12. using System.Linq;
  13. namespace OTSIncAReportGrids
  14. {
  15. public class ExcelExport
  16. {
  17. private System.Windows.Forms.DataGridView dgV_ParticlesDevidePage;
  18. DataTable particlesFilter;
  19. ParticleData Particledata;
  20. Frm_UserProgress frm_userprogress;
  21. public ExcelExport(System.Windows.Forms.DataGridView a_dgV_ParticlesDevidePage,
  22. DataTable a_particlesFilter, ParticleData a_particlesData, Frm_UserProgress a_frm_userprogress)
  23. {
  24. dgV_ParticlesDevidePage = a_dgV_ParticlesDevidePage;
  25. particlesFilter= a_particlesFilter;
  26. Particledata = a_particlesData;
  27. frm_userprogress = a_frm_userprogress;
  28. }
  29. public void ExportToExcel(OTSIncAReportApp._1_UI.Control_Grids.SpectrumExportConfirmation sfd)
  30. {
  31. IWorkbook workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
  32. ISheet sheet = workbook.CreateSheet("Particles");
  33. ICellStyle cellStyle = CreateDefaultCellStyle(workbook);
  34. //设置颗粒名列宽
  35. sheet.SetColumnWidth(1, 30 * 256); //夹杂物名列宽
  36. //插入表头
  37. IRow headerRow = sheet.CreateRow(1);
  38. headerRow.Height = 30 * 20;
  39. for (int i = 0; i < dgV_ParticlesDevidePage.Columns.Count; i++)
  40. {
  41. if(i < 2)
  42. {
  43. ICell cell = headerRow.CreateCell(i);
  44. cell.CellStyle = cellStyle;
  45. cell.SetCellValue(dgV_ParticlesDevidePage.Columns[i].HeaderText);
  46. }
  47. else if (i > 2)
  48. {
  49. ICell cell = headerRow.CreateCell(i-1);
  50. cell.CellStyle = cellStyle;
  51. cell.SetCellValue(dgV_ParticlesDevidePage.Columns[i].HeaderText);
  52. }
  53. else if(sfd.IfExportPicture)
  54. {
  55. ICell cell = headerRow.CreateCell(i);
  56. cell.CellStyle = cellStyle;
  57. cell.SetCellValue(dgV_ParticlesDevidePage.Columns[i].HeaderText);
  58. sheet.SetColumnWidth(2, 7 * 256); //图像列宽
  59. }
  60. }
  61. frm_userprogress?.SetProgressValueAndText(0, $"export: 0/total: {dgV_ParticlesDevidePage.Rows.Count}");
  62. ExportParticleData(workbook, sheet, cellStyle, sfd);
  63. ExportSpectrumData(workbook, cellStyle, sfd);
  64. ExportSpectrumConfirmation(workbook, cellStyle, sfd);
  65. //完成后,对Excel进行保存
  66. using (FileStream file = new FileStream(sfd.FileName, FileMode.Create))
  67. {
  68. workbook.Write(file);
  69. }
  70. frm_userprogress?.SetProgressValueAndText(100,
  71. $"export: {dgV_ParticlesDevidePage.Rows.Count}/total: {dgV_ParticlesDevidePage.Rows.Count}");
  72. }
  73. private ICellStyle CreateDefaultCellStyle(IWorkbook workbook)
  74. {
  75. ICellStyle cellStyle = workbook.CreateCellStyle();
  76. cellStyle.Alignment = HorizontalAlignment.Center;
  77. cellStyle.VerticalAlignment = VerticalAlignment.Center;
  78. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  79. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  80. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  81. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  82. IFont font = workbook.CreateFont();
  83. font.FontName = "黑体";
  84. font.FontHeightInPoints = 10;
  85. cellStyle.SetFont(font);
  86. return cellStyle;
  87. }
  88. private void ExportParticleData(IWorkbook workbook, ISheet sheet, ICellStyle cellStyle,
  89. OTSIncAReportApp._1_UI.Control_Grids.SpectrumExportConfirmation sfd)
  90. {
  91. IDrawing patriarch = sheet.CreateDrawingPatriarch();
  92. for (int i = 0; i < dgV_ParticlesDevidePage.Rows.Count; i++)
  93. {
  94. UpdateExportProgress(i, dgV_ParticlesDevidePage.Rows.Count);
  95. IRow row = sheet.CreateRow(2 + i);
  96. row.Height = 45 * 20;
  97. for (int j = 0; j < dgV_ParticlesDevidePage.Columns.Count; j++)
  98. {
  99. if (sfd.IfExportPicture)
  100. {
  101. ExportCellData(workbook, sheet, patriarch, row, cellStyle, i, j, sfd);
  102. }
  103. else
  104. {
  105. if(j<2)
  106. {
  107. ExportCellData(workbook, sheet, patriarch, row, cellStyle, i, j, sfd);
  108. }
  109. else if(j>2)
  110. {
  111. ExportCellData(workbook, sheet, patriarch, row, cellStyle, i, j-1, sfd);
  112. }
  113. }
  114. }
  115. }
  116. }
  117. private void UpdateExportProgress(int current, int total)
  118. {
  119. if (total > 100 && current % (total / 100) == 0)
  120. {
  121. frm_userprogress?.SetProgressValueAndText(
  122. current * 100 / total,
  123. $"export: {current}/total: {total}");
  124. }
  125. }
  126. private void ExportCellData(IWorkbook workbook, ISheet sheet, IDrawing patriarch,
  127. IRow row, ICellStyle cellStyle, int rowIndex, int colIndex,
  128. OTSIncAReportApp._1_UI.Control_Grids.SpectrumExportConfirmation sfd)
  129. {
  130. if (dgV_ParticlesDevidePage[colIndex, rowIndex].Value == null) return;
  131. if (sfd.IfExportPicture)
  132. {
  133. if (colIndex == 2) //图像列
  134. {
  135. ICell cell = row.CreateCell(colIndex);
  136. cell.CellStyle = cellStyle;
  137. ExportImageCell(workbook, sheet, patriarch, cell, rowIndex, colIndex);
  138. }
  139. else //非图像列
  140. {
  141. ICell cell = row.CreateCell(colIndex);
  142. cell.CellStyle = cellStyle;
  143. ExportDataCell(cell, rowIndex, colIndex);
  144. }
  145. }
  146. else
  147. {
  148. if (colIndex < 2) //前两列
  149. {
  150. ICell cell = row.CreateCell(colIndex);
  151. cell.CellStyle = cellStyle;
  152. ExportDataCell(cell, rowIndex, colIndex);
  153. }
  154. else if (colIndex >= 2) //后面列
  155. {
  156. ICell cell = row.CreateCell(colIndex);
  157. cell.CellStyle = cellStyle;
  158. ExportDataCell(cell, rowIndex, colIndex+1);
  159. }
  160. }
  161. }
  162. private void ExportImageCell(IWorkbook workbook, ISheet sheet, IDrawing patriarch,
  163. ICell cell, int rowIndex, int colIndex)
  164. {
  165. if (dgV_ParticlesDevidePage.Rows[rowIndex].Cells[colIndex].Value is Bitmap bp)
  166. {
  167. using (MemoryStream ms = new MemoryStream())
  168. {
  169. bp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
  170. byte[] bytes = ms.ToArray();
  171. int pictureIdx = workbook.AddPicture(bytes, PictureType.JPEG);
  172. IClientAnchor anchor = patriarch.CreateAnchor(1, 1, 2, 2,
  173. colIndex, rowIndex + 2, colIndex + 1, rowIndex + 3);
  174. patriarch.CreatePicture(anchor, pictureIdx);
  175. }
  176. }
  177. }
  178. private void ExportDataCell(ICell cell, int rowIndex, int colIndex)
  179. {
  180. var cellValue = dgV_ParticlesDevidePage[colIndex, rowIndex].Value;
  181. if (double.TryParse(cellValue.ToString(), out double dval))
  182. {
  183. cell.SetCellType(CellType.Numeric);
  184. cell.SetCellValue(dval);
  185. }
  186. else
  187. {
  188. cell.SetCellValue(cellValue.ToString());
  189. }
  190. }
  191. private void ExportSpectrumData(IWorkbook workbook, ICellStyle cellStyle,
  192. OTSIncAReportApp._1_UI.Control_Grids.SpectrumExportConfirmation sfd)
  193. {
  194. if (!sfd.IfExportSpectrumData) return;
  195. ISheet sheet = workbook.CreateSheet("Peak spectrum Data");
  196. IRow headerRow = sheet.CreateRow(1);
  197. headerRow.Height = 30 * 20;
  198. ICell firstCell = headerRow.CreateCell(0);
  199. firstCell.CellStyle = cellStyle;
  200. firstCell.SetCellValue("rowid");
  201. for (int i = 1; i < 2001; i++)
  202. {
  203. ICell cell = headerRow.CreateCell(i);
  204. cell.CellStyle = cellStyle;
  205. if (i == 1)
  206. {
  207. cell.SetCellValue("PeakSpectrumData");
  208. }
  209. }
  210. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 1, 2000));
  211. for (int j = 0; j < particlesFilter?.Rows.Count; j++)
  212. {
  213. ExportSpectrumDataRow(sheet, cellStyle, j);
  214. }
  215. }
  216. private void ExportSpectrumDataRow(ISheet sheet, ICellStyle cellStyle, int rowIndex)
  217. {
  218. IRow row = sheet.CreateRow(2 + rowIndex);
  219. ICell cell = row.CreateCell(0);
  220. cell.CellStyle = cellStyle;
  221. cell.SetCellValue(rowIndex + 1);
  222. var tmpPart = Particledata.GetParticleXrayDataByFidAndPid(
  223. particlesFilter.Rows[rowIndex]["fieldid"].ToString(),
  224. particlesFilter.Rows[rowIndex]["particleid"].ToString());
  225. if (tmpPart?.XrayId > -1)
  226. {
  227. ExportSpectrumValues(row, cellStyle, tmpPart);
  228. }
  229. }
  230. private void ExportSpectrumValues(IRow row, ICellStyle cellStyle, Particle tmpPart)
  231. {
  232. uint[] analysisXray = new uint[2000];
  233. for (int i = 0; i < 2000; i++)
  234. {
  235. analysisXray[i] = BitConverter.ToUInt32(tmpPart.XRayData, i * 4);
  236. ICell cell = row.CreateCell(i + 1);
  237. cell.CellStyle = cellStyle;
  238. cell.SetCellValue(analysisXray[i]);
  239. }
  240. }
  241. private void ExportSpectrumConfirmation(IWorkbook workbook, ICellStyle cellStyle,
  242. OTSIncAReportApp._1_UI.Control_Grids.SpectrumExportConfirmation sfd)
  243. {
  244. if (!sfd.IfExportSpectrumConfirmation) return;
  245. ISheet sheet = workbook.CreateSheet("Peak spectrum");
  246. sheet.SetColumnWidth(1, 200 * 256); //图像列宽
  247. IRow headerRow = sheet.CreateRow(1);
  248. headerRow.Height = 30 * 20;
  249. ICell firstCell = headerRow.CreateCell(0);
  250. firstCell.CellStyle = cellStyle;
  251. firstCell.SetCellValue("rowid");
  252. ICell secondCell = headerRow.CreateCell(1);
  253. secondCell.CellStyle = cellStyle;
  254. secondCell.SetCellValue("PeakSpectrum");
  255. for (int j = 0; j < particlesFilter?.Rows.Count; j++)
  256. {
  257. ExportSpectrumChart(sheet, cellStyle, j);
  258. }
  259. }
  260. private void ExportSpectrumChart(ISheet sheet, ICellStyle cellStyle, int rowIndex)
  261. {
  262. IRow row = sheet.CreateRow(rowIndex + 2);
  263. row.Height = 120 * 20;
  264. ICell cell = row.CreateCell(0);
  265. cell.CellStyle = cellStyle;
  266. cell.SetCellValue(rowIndex + 1);
  267. IDrawing drawing = sheet.CreateDrawingPatriarch();
  268. IClientAnchor anchor = drawing.CreateAnchor(0, 0, 0, 0, 1, rowIndex + 2, 2, rowIndex + 3);
  269. CreateChart(drawing, sheet, anchor, rowIndex + 2);
  270. }
  271. private void CreateChart(IDrawing drawing, ISheet sheet, IClientAnchor anchor, int rowid)
  272. {
  273. if (!(drawing.CreateChart(anchor) is XSSFChart chart))
  274. return;
  275. var data = chart.ChartDataFactory.CreateLineChartData<double, double>();
  276. IChartAxis bottomAxis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom);
  277. IValueAxis leftAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left);
  278. leftAxis.Crosses = AxisCrosses.AutoZero;
  279. leftAxis.IsVisible = true;
  280. bottomAxis.IsVisible = true;
  281. IChartDataSource<double> ys = DataSources.FromNumericCellRange(
  282. sheet, new CellRangeAddress(rowid, rowid, 1, 2001));
  283. double[] doubles = Enumerable.Range(0, 2000).Select(i => (double)i).ToArray();
  284. IChartDataSource<double> xs = DataSources.FromArray(doubles);
  285. data.AddSeries(xs, ys);
  286. }
  287. }
  288. }