Export_NPOI_Excel.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. 
  2. //nopi 相关引用
  3. using NPOI.SS.UserModel;
  4. using NPOI.XSSF.UserModel;
  5. using OTSIncAReportGraph.Class;
  6. //using OTSINTERFACE;
  7. using System.Drawing;
  8. using System.IO;
  9. namespace OTSIncAReportApp.OTSTemplateDesigner
  10. {
  11. class Export_NPOI_Excel
  12. {
  13. #region 全局变量
  14. public OTSReport_Export m_otsreport_export;
  15. IWorkbook workbook = new XSSFWorkbook(); //工作薄,用于创建.xlsx office2007开始以后的
  16. ISheet sheet;
  17. public string m_kzm = ".xlsx";
  18. public int m_Excel_Row_CurrentNumber = 1; //当前插入到的位置
  19. #endregion
  20. #region 构造函数
  21. public Export_NPOI_Excel(OTSReport_Export in_export)
  22. {
  23. m_otsreport_export = in_export;
  24. }
  25. #endregion
  26. #region 创建保存
  27. /// <summary>
  28. /// 创建Excel文件
  29. /// </summary>
  30. /// <param name="dir"></param>
  31. /// <param name="fileName"></param>
  32. /// <returns></returns>
  33. public bool CreateExcelFile(string filePath)
  34. {
  35. //创建word文件
  36. FileStream fs = File.Create(filePath);
  37. fs.Close();
  38. return true;
  39. }
  40. public bool SaveExcelFile(string filepath)
  41. {
  42. //打开前,先保存word
  43. FileStream file = new FileStream(filepath, FileMode.Create);
  44. workbook.Write(file);
  45. file.Close();
  46. return true;
  47. }
  48. #endregion
  49. #region Excel相关操作
  50. /// <summary>
  51. /// 向Excel中插入工作表,sheet
  52. /// </summary>
  53. /// <param name="sheetname"></param>
  54. /// <returns></returns>
  55. public bool InseretSheet(string sheetname)
  56. {
  57. sheet = workbook.CreateSheet(sheetname);//创建工作表
  58. return true;
  59. }
  60. /// <summary>
  61. /// 向Excel中插入表格的名称
  62. /// </summary>
  63. /// <param name="in_tablename"></param>
  64. private void InsertExcelTableName(string in_tablename, int spancell)
  65. {
  66. ICellStyle cellstyle = workbook.CreateCellStyle();
  67. cellstyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
  68. cellstyle.VerticalAlignment = VerticalAlignment.Center;
  69. IFont font = workbook.CreateFont();//设置字体大小和颜色
  70. font.FontName = "黑体";
  71. font.FontHeightInPoints = 13;
  72. cellstyle.SetFont(font);
  73. ICell sheettitle = sheet.CreateRow(m_Excel_Row_CurrentNumber).CreateCell(0);
  74. sheettitle.SetCellValue(in_tablename);
  75. sheettitle.CellStyle = cellstyle;
  76. sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(m_Excel_Row_CurrentNumber, m_Excel_Row_CurrentNumber, 0, spancell));
  77. m_Excel_Row_CurrentNumber = m_Excel_Row_CurrentNumber + 1;//插入完表名后,全局增量增长值+1
  78. }
  79. /// <summary>
  80. /// 获取表格基础样式
  81. /// </summary>
  82. /// <returns></returns>
  83. private ICellStyle GetTableStyle()
  84. {
  85. //创建表格边框样式风格
  86. ICellStyle cellStyle = workbook.CreateCellStyle();
  87. cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
  88. cellStyle.VerticalAlignment = VerticalAlignment.Center;
  89. //设置上4边
  90. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  91. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  92. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  93. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  94. //设置字符大小和颜色
  95. IFont font = workbook.CreateFont();
  96. font.FontName = "黑体";
  97. font.FontHeightInPoints = 10;
  98. cellStyle.SetFont(font);
  99. return cellStyle;
  100. }
  101. /// <summary>
  102. /// 在Excel指定的单元格内,插入图片
  103. /// </summary>
  104. /// <param name="in_bp"></param>
  105. /// <param name="cell"></param>
  106. /// <param name="row"></param>
  107. private void AddPictureInExcel(Bitmap in_bp, int cell, int row)
  108. {
  109. byte[] bytes = DrawFunction.ImageConvertToBytes(in_bp);
  110. //第二步,将图片添加到workbook中,指定图片的格式,返回图片所在workbook->paicture数组中的索引的地址,从1开始
  111. int pictureIdx = workbook.AddPicture(bytes, NPOI.SS.UserModel.PictureType.JPEG);
  112. //第三步,在sheet中创建画布
  113. IDrawing patriarch = sheet.GetRow(m_Excel_Row_CurrentNumber + row).GetCell(cell).Sheet.CreateDrawingPatriarch();
  114. //第四步,设置锚点,(在起始单元格的X坐标0-1023,Y的坐标0-255,在终止单元格的X坐标0-1023,Y的坐标0-255,起始单元格行数,列数,终止单元格行数,列数)
  115. IClientAnchor anchor = patriarch.CreateAnchor(1000, 200, 1000, 200, cell, m_Excel_Row_CurrentNumber + row, cell + 1, m_Excel_Row_CurrentNumber + row + 1);//终止比开始位置大1,会自动缩放到一个单元格内的
  116. //第五步,创建图片
  117. IPicture pict = patriarch.CreatePicture(anchor, pictureIdx);
  118. }
  119. /// <summary>
  120. /// 向指定的位置插入图片,为颗粒列表使用
  121. /// </summary>
  122. /// <param name="cell"></param>
  123. /// <param name="row"></param>
  124. private void AddPictureInExcel_ForParticleList(Bitmap in_bp, int cell, int row)
  125. {
  126. //先对图片同比缩放
  127. Bitmap ls_bp = new Bitmap(64, 20);
  128. Graphics g = Graphics.FromImage(ls_bp);
  129. g.Clear(Color.FromArgb(230, 230, 230));
  130. g.DrawImage(in_bp, new Rectangle(17, 0, 30, 17), new Rectangle(0, 0, in_bp.Width, in_bp.Height), GraphicsUnit.Pixel);
  131. byte[] bytes = DrawFunction.ImageConvertToBytes(ls_bp);
  132. //第二步,将图片添加到workbook中,指定图片的格式,返回图片所在workbook->paicture数组中的索引的地址,从1开始
  133. int pictureIdx = workbook.AddPicture(bytes, NPOI.SS.UserModel.PictureType.JPEG);
  134. //第三步,在sheet中创建画布
  135. IDrawing patriarch = sheet.GetRow(m_Excel_Row_CurrentNumber + row).GetCell(cell).Sheet.CreateDrawingPatriarch();
  136. //第四步,设置锚点,(在起始单元格的X坐标0-1023,Y的坐标0-255,在终止单元格的X坐标0-1023,Y的坐标0-255,起始单元格行数,列数,终止单元格行数,列数)
  137. //IClientAnchor anchor = patriarch.CreateAnchor(1000, 200, 1000, 200, cell, m_Excel_Row_CurrentNumber + row, cell + 1, m_Excel_Row_CurrentNumber + row + 1);//终止比开始位置大1,会自动缩放到一个单元格内的
  138. IClientAnchor anchor = sheet.GetRow(m_Excel_Row_CurrentNumber + row).GetCell(cell).Sheet.Workbook.GetCreationHelper().CreateClientAnchor();
  139. anchor.AnchorType = AnchorType.MoveDontResize;
  140. int dx1 = 200;
  141. int dy1 = 50;
  142. //在单元格上的位置,不能居中就要这样弄了
  143. anchor.Col1 = cell;
  144. anchor.Row1 = m_Excel_Row_CurrentNumber + row;
  145. anchor.Col2 = cell + 1;
  146. anchor.Row2 = m_Excel_Row_CurrentNumber + row + 1;
  147. anchor.Dx1 = dx1;
  148. anchor.Dy1 = dy1;
  149. //第五步,创建图片
  150. IPicture pict = patriarch.CreatePicture(anchor, pictureIdx);
  151. //不对图片进行拉伸
  152. pict.Resize();
  153. in_bp.Dispose();
  154. ls_bp.Dispose();
  155. }
  156. /// <summary>
  157. /// 向Excel中添加页眉页脚
  158. /// </summary>
  159. public void AddPageHeaderFooter()
  160. {
  161. //添加页眉页脚文字
  162. sheet.RepeatingRows = new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 5);
  163. sheet.Header.Center = "Opton OTS";
  164. sheet.Footer.Center = "Opton OTS";
  165. }
  166. /// <summary>
  167. /// 根据系统中的颜色列表,获取NPOI中的颜色索引值
  168. /// </summary>
  169. /// <param name="in_c"></param>
  170. /// <returns></returns>
  171. private short GetNPOIColorIndexBySystemColor(Color in_c)
  172. {
  173. if (in_c == Color.Azure)
  174. return NPOI.HSSF.Util.HSSFColor.LightTurquoise.Index;
  175. if (in_c == Color.Beige)
  176. return NPOI.HSSF.Util.HSSFColor.LemonChiffon.Index;
  177. if (in_c == Color.PaleGreen)
  178. return NPOI.HSSF.Util.HSSFColor.Lime.Index;
  179. if (in_c == Color.FloralWhite)
  180. return NPOI.HSSF.Util.HSSFColor.Automatic.Index;
  181. if (in_c == Color.Gainsboro)
  182. return NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index;
  183. if (in_c == Color.LightCyan)
  184. return NPOI.HSSF.Util.HSSFColor.LightTurquoise.Index;
  185. if (in_c == Color.PowderBlue)
  186. return NPOI.HSSF.Util.HSSFColor.Aqua.Index;
  187. return 9;
  188. }
  189. #endregion
  190. }
  191. }