ExcelExport.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. sheet.SetColumnWidth(2, 7 * 256); //图像列宽
  37. //插入表头
  38. IRow headerRow = sheet.CreateRow(1);
  39. headerRow.Height = 30 * 20;
  40. for (int i = 0; i < dgV_ParticlesDevidePage.Columns.Count; i++)
  41. {
  42. ICell cell = headerRow.CreateCell(i);
  43. cell.CellStyle = cellStyle;
  44. cell.SetCellValue(dgV_ParticlesDevidePage.Columns[i].HeaderText);
  45. }
  46. frm_userprogress?.SetProgressValueAndText(0, $"export: 0/total: {dgV_ParticlesDevidePage.Rows.Count}");
  47. ExportParticleData(workbook, sheet, cellStyle, sfd);
  48. ExportSpectrumData(workbook, cellStyle, sfd);
  49. ExportSpectrumConfirmation(workbook, cellStyle, sfd);
  50. //完成后,对Excel进行保存
  51. using (FileStream file = new FileStream(sfd.FileName, FileMode.Create))
  52. {
  53. workbook.Write(file);
  54. }
  55. frm_userprogress?.SetProgressValueAndText(100,
  56. $"export: {dgV_ParticlesDevidePage.Rows.Count}/total: {dgV_ParticlesDevidePage.Rows.Count}");
  57. }
  58. private ICellStyle CreateDefaultCellStyle(IWorkbook workbook)
  59. {
  60. ICellStyle cellStyle = workbook.CreateCellStyle();
  61. cellStyle.Alignment = HorizontalAlignment.Center;
  62. cellStyle.VerticalAlignment = VerticalAlignment.Center;
  63. //设置边框
  64. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  65. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  66. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  67. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  68. //设置字体
  69. IFont font = workbook.CreateFont();
  70. font.FontName = "黑体";
  71. font.FontHeightInPoints = 10;
  72. cellStyle.SetFont(font);
  73. return cellStyle;
  74. }
  75. private void ExportParticleData(IWorkbook workbook, ISheet sheet, ICellStyle cellStyle,
  76. OTSIncAReportApp._1_UI.Control_Grids.SpectrumExportConfirmation sfd)
  77. {
  78. IDrawing patriarch = sheet.CreateDrawingPatriarch();
  79. for (int i = 0; i < dgV_ParticlesDevidePage.Rows.Count; i++)
  80. {
  81. UpdateExportProgress(i, dgV_ParticlesDevidePage.Rows.Count);
  82. IRow row = sheet.CreateRow(2 + i);
  83. row.Height = 45 * 20;
  84. for (int j = 0; j < dgV_ParticlesDevidePage.Columns.Count; j++)
  85. {
  86. ExportCellData(workbook, sheet, patriarch, row, cellStyle, i, j, sfd);
  87. }
  88. }
  89. }
  90. private void UpdateExportProgress(int current, int total)
  91. {
  92. if (total > 100 && current % (total / 100) == 0)
  93. {
  94. frm_userprogress?.SetProgressValueAndText(
  95. current * 100 / total,
  96. $"export: {current}/total: {total}");
  97. }
  98. }
  99. private void ExportCellData(IWorkbook workbook, ISheet sheet, IDrawing patriarch,
  100. IRow row, ICellStyle cellStyle, int rowIndex, int colIndex,
  101. OTSIncAReportApp._1_UI.Control_Grids.SpectrumExportConfirmation sfd)
  102. {
  103. ICell cell = row.CreateCell(colIndex);
  104. cell.CellStyle = cellStyle;
  105. if (dgV_ParticlesDevidePage[colIndex, rowIndex].Value == null) return;
  106. if (colIndex == 2 && sfd.IfExportPicture) //图像列
  107. {
  108. ExportImageCell(workbook, sheet, patriarch, cell, rowIndex, colIndex);
  109. }
  110. else //非图像列
  111. {
  112. ExportDataCell(cell, rowIndex, colIndex);
  113. }
  114. }
  115. private void ExportImageCell(IWorkbook workbook, ISheet sheet, IDrawing patriarch,
  116. ICell cell, int rowIndex, int colIndex)
  117. {
  118. if (dgV_ParticlesDevidePage.Rows[rowIndex].Cells[colIndex].Value is Bitmap bp)
  119. {
  120. using (MemoryStream ms = new MemoryStream())
  121. {
  122. bp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
  123. byte[] bytes = ms.ToArray();
  124. int pictureIdx = workbook.AddPicture(bytes, PictureType.JPEG);
  125. IClientAnchor anchor = patriarch.CreateAnchor(1, 1, 2, 2,
  126. colIndex, rowIndex + 2, colIndex + 1, rowIndex + 3);
  127. patriarch.CreatePicture(anchor, pictureIdx);
  128. }
  129. }
  130. }
  131. private void ExportDataCell(ICell cell, int rowIndex, int colIndex)
  132. {
  133. var cellValue = dgV_ParticlesDevidePage[colIndex, rowIndex].Value;
  134. if (double.TryParse(cellValue.ToString(), out double dval))
  135. {
  136. cell.SetCellType(CellType.Numeric);
  137. cell.SetCellValue(dval);
  138. }
  139. else
  140. {
  141. cell.SetCellValue(cellValue.ToString());
  142. }
  143. }
  144. private void ExportSpectrumData(IWorkbook workbook, ICellStyle cellStyle,
  145. OTSIncAReportApp._1_UI.Control_Grids.SpectrumExportConfirmation sfd)
  146. {
  147. if (!sfd.IfExportSpectrumData) return;
  148. ISheet sheet = workbook.CreateSheet("Peak spectrum Data");
  149. IRow headerRow = sheet.CreateRow(1);
  150. headerRow.Height = 30 * 20;
  151. ICell firstCell = headerRow.CreateCell(0);
  152. firstCell.CellStyle = cellStyle;
  153. firstCell.SetCellValue("rowid");
  154. for (int i = 1; i < 2001; i++)
  155. {
  156. ICell cell = headerRow.CreateCell(i);
  157. cell.CellStyle = cellStyle;
  158. if (i == 1)
  159. {
  160. cell.SetCellValue("PeakSpectrumData");
  161. }
  162. }
  163. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 1, 2000));
  164. for (int j = 0; j < particlesFilter?.Rows.Count; j++)
  165. {
  166. ExportSpectrumDataRow(sheet, cellStyle, j);
  167. }
  168. }
  169. private void ExportSpectrumDataRow(ISheet sheet, ICellStyle cellStyle, int rowIndex)
  170. {
  171. IRow row = sheet.CreateRow(2 + rowIndex);
  172. ICell cell = row.CreateCell(0);
  173. cell.CellStyle = cellStyle;
  174. cell.SetCellValue(rowIndex + 1);
  175. var tmpPart = Particledata.GetParticleXrayDataByFidAndPid(
  176. particlesFilter.Rows[rowIndex]["fieldid"].ToString(),
  177. particlesFilter.Rows[rowIndex]["particleid"].ToString());
  178. if (tmpPart?.XrayId > -1)
  179. {
  180. ExportSpectrumValues(row, cellStyle, tmpPart);
  181. }
  182. }
  183. private void ExportSpectrumValues(IRow row, ICellStyle cellStyle, Particle tmpPart)
  184. {
  185. uint[] analysisXray = new uint[2000];
  186. for (int i = 0; i < 2000; i++)
  187. {
  188. analysisXray[i] = BitConverter.ToUInt32(tmpPart.XRayData, i * 4);
  189. ICell cell = row.CreateCell(i + 1);
  190. cell.CellStyle = cellStyle;
  191. cell.SetCellValue(analysisXray[i]);
  192. }
  193. }
  194. private void ExportSpectrumConfirmation(IWorkbook workbook, ICellStyle cellStyle,
  195. OTSIncAReportApp._1_UI.Control_Grids.SpectrumExportConfirmation sfd)
  196. {
  197. if (!sfd.IfExportSpectrumConfirmation) return;
  198. ISheet sheet = workbook.CreateSheet("Peak spectrum");
  199. sheet.SetColumnWidth(1, 200 * 256); //图像列宽
  200. IRow headerRow = sheet.CreateRow(1);
  201. headerRow.Height = 30 * 20;
  202. ICell firstCell = headerRow.CreateCell(0);
  203. firstCell.CellStyle = cellStyle;
  204. firstCell.SetCellValue("rowid");
  205. ICell secondCell = headerRow.CreateCell(1);
  206. secondCell.CellStyle = cellStyle;
  207. secondCell.SetCellValue("PeakSpectrum");
  208. for (int j = 0; j < particlesFilter?.Rows.Count; j++)
  209. {
  210. ExportSpectrumChart(sheet, cellStyle, j);
  211. }
  212. }
  213. private void ExportSpectrumChart(ISheet sheet, ICellStyle cellStyle, int rowIndex)
  214. {
  215. IRow row = sheet.CreateRow(rowIndex + 2);
  216. row.Height = 120 * 20;
  217. ICell cell = row.CreateCell(0);
  218. cell.CellStyle = cellStyle;
  219. cell.SetCellValue(rowIndex + 1);
  220. IDrawing drawing = sheet.CreateDrawingPatriarch();
  221. IClientAnchor anchor = drawing.CreateAnchor(0, 0, 0, 0, 1, rowIndex + 2, 2, rowIndex + 3);
  222. CreateChart(drawing, sheet, anchor, rowIndex + 2);
  223. }
  224. private void CreateChart(IDrawing drawing, ISheet sheet, IClientAnchor anchor, int rowid)
  225. {
  226. if (!(drawing.CreateChart(anchor) is XSSFChart chart))
  227. return;
  228. var data = chart.ChartDataFactory.CreateLineChartData<double, double>();
  229. IChartAxis bottomAxis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom);
  230. IValueAxis leftAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left);
  231. leftAxis.Crosses = AxisCrosses.AutoZero;
  232. leftAxis.IsVisible = true;
  233. bottomAxis.IsVisible = true;
  234. IChartDataSource<double> ys = DataSources.FromNumericCellRange(
  235. sheet, new CellRangeAddress(rowid, rowid, 1, 2001));
  236. double[] doubles = Enumerable.Range(0, 2000).Select(i => (double)i).ToArray();
  237. IChartDataSource<double> xs = DataSources.FromArray(doubles);
  238. data.AddSeries(xs, ys);
  239. }
  240. }
  241. }