ReportBaseX.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. using System;
  2. using System.IO;
  3. using System.Web;
  4. using NPOI.HPSF;
  5. using NPOI.HSSF.UserModel;
  6. using NPOI.SS.UserModel;
  7. using NPOI.XSSF.Streaming;
  8. using NPOI.XSSF.UserModel;
  9. namespace SiteCore
  10. {
  11. public class NpoiMemoryStream : MemoryStream
  12. {
  13. public NpoiMemoryStream()
  14. {
  15. AllowClose = true;
  16. }
  17. public bool AllowClose { get; set; }
  18. public override void Close()
  19. {
  20. if (AllowClose)
  21. base.Close();
  22. }
  23. }
  24. /// <summary>
  25. /// ReportBase 的摘要说明
  26. /// </summary>
  27. public class ReportBaseX : BasePage
  28. {
  29. public ReportBaseX()
  30. {
  31. //
  32. // TODO: 在此处添加构造函数逻辑
  33. //
  34. }
  35. public string excelName = "";
  36. public int AIdx = 65;
  37. public SXSSFWorkbook xssfworkbook;
  38. public ISheet iSheet;
  39. #region custom_style
  40. ICellStyle headStyle = null;
  41. ICellStyle moneyStyle = null;
  42. private ICellStyle GetHeadStyle()
  43. {
  44. if (headStyle == null)
  45. {
  46. headStyle = xssfworkbook.CreateCellStyle();
  47. IFont font = xssfworkbook.CreateFont();
  48. font.FontName = "宋体";
  49. font.FontHeightInPoints = 9;
  50. font.Boldweight = 700;
  51. headStyle.SetFont(font);
  52. headStyle.Alignment = HorizontalAlignment.Center;
  53. headStyle.VerticalAlignment = VerticalAlignment.Center;
  54. }
  55. return headStyle;
  56. }
  57. private ICellStyle GetMoneyStyle()
  58. {
  59. if (moneyStyle == null)
  60. {
  61. moneyStyle = xssfworkbook.CreateCellStyle();
  62. IDataFormat format = xssfworkbook.CreateDataFormat();
  63. //moneyStyle.DataFormat = format.GetFormat("_(* #,##0.00_);_(* (#,##0.00);_(* \" - \"??_);_(@_)");
  64. moneyStyle.DataFormat = format.GetFormat("#,##0.0##");
  65. }
  66. return moneyStyle;
  67. }
  68. #endregion
  69. #region Excel自定义方法
  70. public string getExcelChar(int idx)
  71. {
  72. string perv = "";
  73. if (idx > 26)
  74. {
  75. idx = idx - 26;
  76. perv = "A";
  77. }
  78. return perv + Convert.ToChar(idx + AIdx).ToString();
  79. }
  80. //创建标题头
  81. public void createHeadCell(IRow row, int cIdx, string key)
  82. {
  83. ICell cell = row.CreateCell(cIdx);
  84. cell.CellStyle = GetHeadStyle();
  85. cell.SetCellValue(key);
  86. }
  87. //竖着求和
  88. public void setVSumCell(int rIdx, int cIdx, int s, int e)
  89. {
  90. string c = Convert.ToChar(cIdx + AIdx).ToString();
  91. if (iSheet.GetRow(rIdx).GetCell(cIdx) == null)
  92. iSheet.GetRow(rIdx).CreateCell(cIdx);
  93. setFormulaCell(rIdx, cIdx, string.Format("SUM({0}{1}:{0}{2})", c, s, e));
  94. }
  95. //横着求和
  96. public void setHSumCell(int rIdx, int cIdx, int s, int e)
  97. {
  98. string sc = Convert.ToChar((s - 1) + AIdx).ToString();
  99. string ec = Convert.ToChar((e - 1) + AIdx).ToString();
  100. setFormulaCell(rIdx, cIdx, string.Format("SUM({0}{1}:{2}{1})", sc, rIdx + 1, ec));
  101. }
  102. //竖着相乘
  103. public void setVMultCell(int rIdx, int cIdx, int s, int e)
  104. {
  105. string c = Convert.ToChar(cIdx + AIdx).ToString();
  106. setFormulaCell(rIdx, cIdx, string.Format("{0}{1}*{0}{2}", c, s, e));
  107. }
  108. //横着相乘
  109. public void setHMultCell(int rIdx, int cIdx, int s, int e)
  110. {
  111. string sc = Convert.ToChar((s - 1) + AIdx).ToString();
  112. string ec = Convert.ToChar((e - 1) + AIdx).ToString();
  113. setFormulaCell(rIdx, cIdx, string.Format("{0}{1}*{2}{1}", sc, rIdx + 1, ec));
  114. }
  115. public void setFormulaCell(int rIdx, int cIdx, string v)
  116. {
  117. iSheet.GetRow(rIdx).GetCell(cIdx).SetCellFormula(v);
  118. }
  119. public void setStringCell(int rIdx, int cIdx, object v)
  120. {
  121. if (v == null) return;
  122. if (iSheet.GetRow(rIdx).GetCell(cIdx) == null)
  123. iSheet.GetRow(rIdx).CreateCell(cIdx).SetCellValue(v.ToString());
  124. else
  125. iSheet.GetRow(rIdx).GetCell(cIdx).SetCellValue(v.ToString());
  126. }
  127. public void setMoneyCell(int rIdx, int cIdx, object v)
  128. {
  129. ICell cell = iSheet.GetRow(rIdx).GetCell(cIdx);
  130. cell.CellStyle = GetMoneyStyle();
  131. cell.SetCellValue(Convert.ToDouble(v));
  132. }
  133. public void createMoneyCell(IRow row, int cIdx, object v)
  134. {
  135. ICell cell = row.CreateCell(cIdx);
  136. cell.CellStyle = GetMoneyStyle();
  137. cell.SetCellValue(Convert.ToDouble(v));
  138. }
  139. public void setMoneyCell(IRow row, int cIdx, object v)
  140. {
  141. ICell cell = row.GetCell(cIdx);
  142. cell.CellStyle = GetMoneyStyle();
  143. cell.SetCellValue(Convert.ToDouble(v));
  144. }
  145. public void setRichTextCell(int rIdx, int cIdx, IRichTextString v)
  146. {
  147. iSheet.GetRow(rIdx).GetCell(cIdx).SetCellValue(v);
  148. }
  149. public void setDoubleCell(IRow row, int cIdx, object v)
  150. {
  151. if (v.ToString() == "" || v.ToString() == "0") return;
  152. row.GetCell(cIdx).SetCellValue(Convert.ToDouble(v));
  153. }
  154. public void createDoublCell(IRow row, int cIdx, object v)
  155. {
  156. if (v.ToString() == "" || v.ToString() == "0") return;
  157. row.CreateCell(cIdx).SetCellValue(Convert.ToDouble(v));
  158. }
  159. public void setDoubleCell(int rIdx, int cIdx, object v)
  160. {
  161. if (v.ToString() == "" || v.ToString() == "0") return;
  162. if (iSheet.GetRow(rIdx).GetCell(cIdx) == null)
  163. iSheet.GetRow(rIdx).CreateCell(cIdx).SetCellValue(Convert.ToDouble(v));
  164. else
  165. iSheet.GetRow(rIdx).GetCell(cIdx).SetCellValue(Convert.ToDouble(v));
  166. }
  167. public void setDoubleCellWithZero(int rIdx, int cIdx, object v)
  168. {
  169. if (v.ToString() == "") return;
  170. iSheet.GetRow(rIdx).GetCell(cIdx).SetCellValue(Convert.ToDouble(v));
  171. }
  172. public string getStringCell(int rIdx, int cIdx)
  173. {
  174. return iSheet.GetRow(rIdx).GetCell(cIdx).StringCellValue;
  175. }
  176. public IRichTextString GetRichStringCell(int rIdx, int cIdx)
  177. {
  178. return iSheet.GetRow(rIdx).GetCell(cIdx).RichStringCellValue;
  179. }
  180. public double getDoubleCell(int rIdx, int cIdx)
  181. {
  182. return iSheet.GetRow(rIdx).GetCell(cIdx).NumericCellValue;
  183. }
  184. //复制行
  185. public void CopyRowCells(int lastIdx, int sIdx)
  186. {
  187. for (int i = 0; i < lastIdx; i++)
  188. {
  189. iSheet.CopyRow(i, sIdx++);
  190. IRow row = iSheet.GetRow(i);
  191. if (row != null)
  192. {
  193. IRow copyRow = iSheet.GetRow(sIdx - 1);
  194. if (copyRow != null) copyRow.Height = row.Height;
  195. }
  196. }
  197. }
  198. //删除行
  199. public void removeRow(int rowIndex)
  200. {
  201. int lastRowNum = iSheet.LastRowNum;
  202. if (rowIndex >= 0 && rowIndex < lastRowNum)
  203. {
  204. iSheet.ShiftRows(rowIndex + 1, lastRowNum, -1);
  205. }
  206. if (rowIndex == lastRowNum)
  207. {
  208. IRow removingRow = iSheet.GetRow(rowIndex);
  209. if (removingRow != null)
  210. {
  211. iSheet.RemoveRow(removingRow);
  212. }
  213. }
  214. }
  215. public void insertNoStyleRow(ISheet sheet, int rowIdx, int rowNum, IRow sourceRow)
  216. {
  217. //批量移动行
  218. sheet.ShiftRows(rowIdx, sheet.LastRowNum, rowNum, true, false);
  219. //对批量移动后空出的空行插,创建相应的行,并以插入行的上一行为格式源
  220. for (int i = rowIdx; i < rowIdx + rowNum - 1; i++)
  221. {
  222. IRow targetRow = null;
  223. ICell sourceCell = null;
  224. ICell targetCell = null;
  225. targetRow = sheet.CreateRow(i + 1);
  226. for (int m = sourceRow.FirstCellNum; m < sourceRow.LastCellNum; m++)
  227. {
  228. sourceCell = sourceRow.GetCell(m);
  229. if (sourceCell == null)
  230. continue;
  231. targetCell = targetRow.CreateCell(m);
  232. //targetCell.Encoding = sourceCell.Encoding;
  233. //targetCell.CellStyle = sourceCell.CellStyle;
  234. //targetCell.SetCellType(sourceCell.CellType);
  235. }
  236. //CopyRow(sourceRow, targetRow);
  237. //Util.CopyRow(sheet, sourceRow, targetRow);
  238. }
  239. IRow firstTargetRow = sheet.GetRow(rowIdx);
  240. ICell firstSourceCell = null;
  241. ICell firstTargetCell = null;
  242. for (int m = sourceRow.FirstCellNum; m < sourceRow.LastCellNum; m++)
  243. {
  244. firstSourceCell = sourceRow.GetCell(m);
  245. if (firstSourceCell == null)
  246. continue;
  247. firstTargetCell = firstTargetRow.CreateCell(m);
  248. //firstTargetCell.Encoding = firstSourceCell.Encoding;
  249. //firstTargetCell.CellStyle = firstSourceCell.CellStyle;
  250. //firstTargetCell.SetCellType(firstSourceCell.CellType);
  251. }
  252. }
  253. public void insertSimpleRow(ISheet sheet, int rowIdx, int rowNum, IRow sourceRow)
  254. {
  255. //批量移动行
  256. sheet.ShiftRows(rowIdx, sheet.LastRowNum, rowNum, true, false);
  257. }
  258. //插入行
  259. public void insertRow(ISheet sheet, int rowIdx, int rowNum, IRow sourceRow)
  260. {
  261. //批量移动行
  262. sheet.ShiftRows(rowIdx, sheet.LastRowNum, rowNum, true, false);
  263. //对批量移动后空出的空行插,创建相应的行,并以插入行的上一行为格式源
  264. for (int i = rowIdx; i < rowIdx + rowNum; i++)
  265. {
  266. IRow targetRow = null;
  267. ICell sourceCell = null;
  268. ICell targetCell = null;
  269. targetRow = sheet.CreateRow(i);
  270. targetRow.Height = sourceRow.Height;
  271. //targetRow.RowStyle = sourceRow.RowStyle;
  272. int c = sourceRow.Cells.Count;
  273. for (int m = sourceRow.FirstCellNum; m < sourceRow.LastCellNum; m++)
  274. {
  275. sourceCell = sourceRow.GetCell(m);
  276. if (sourceCell == null)
  277. continue;
  278. targetCell = targetRow.CreateCell(m);
  279. //targetCell.Encoding = sourceCell.Encoding;
  280. targetCell.CellStyle = sourceCell.CellStyle;
  281. targetCell.SetCellType(sourceCell.CellType);
  282. //targetCell.CellFormula = sourceCell.CellFormula;
  283. }
  284. }
  285. }
  286. ////复制行
  287. public void CopyRange(int fromRowIndex, int fromColIndex, int toRowIndex, int toColIndex, bool onlyData, bool copyComment)
  288. {
  289. IRow sourceRow = xssfworkbook.GetSheetAt(xssfworkbook.ActiveSheetIndex).GetRow(fromRowIndex);
  290. ICell sourceCell = sourceRow.GetCell(fromColIndex);
  291. if (sourceRow != null && sourceCell != null)
  292. {
  293. IRow changingRow = null;
  294. ICell changingCell = null;
  295. changingRow = xssfworkbook.GetSheetAt(xssfworkbook.ActiveSheetIndex).GetRow(toRowIndex);
  296. if (changingRow == null)
  297. changingRow = xssfworkbook.GetSheetAt(xssfworkbook.ActiveSheetIndex).CreateRow(toRowIndex);
  298. changingCell = changingRow.GetCell(toColIndex);
  299. if (changingCell == null)
  300. changingCell = changingRow.CreateCell(toColIndex);
  301. if (onlyData)//仅数据
  302. {
  303. //对单元格的值赋值
  304. changingCell.SetCellValue(sourceCell.StringCellValue);
  305. }
  306. else //非仅数据
  307. {
  308. //单元格的编码
  309. //changingCell.Encoding = sourceCell.Encoding;
  310. //单元格的格式
  311. changingCell.CellStyle = sourceCell.CellStyle;
  312. //单元格的公式
  313. //if (sourceCell.CellFormula == "")
  314. // changingCell.SetCellValue(sourceCell.StringCellValue);
  315. //else
  316. // changingCell.SetCellFormula(sourceCell.CellFormula);
  317. //对单元格的批注赋值
  318. if (copyComment)
  319. {
  320. if (sourceCell.CellComment != null)
  321. {
  322. IDrawing patr = xssfworkbook.GetSheetAt(xssfworkbook.ActiveSheetIndex).CreateDrawingPatriarch();
  323. IClientAnchor ica = new HSSFClientAnchor(0, 0, 0, 0, toColIndex, toRowIndex, toColIndex + 1, toRowIndex + 1);
  324. IComment comment = patr.CreateCellComment(ica);
  325. comment.String = new HSSFRichTextString(sourceCell.CellComment.String.ToString());
  326. comment.Author = sourceCell.CellComment.Author;
  327. changingCell.CellComment = comment;
  328. }
  329. }
  330. }
  331. }
  332. }
  333. #endregion
  334. #region excel操作
  335. public void createExcel()
  336. {
  337. //FileStream file = new FileStream(fname, FileMode.Open, FileAccess.Read);
  338. xssfworkbook = new SXSSFWorkbook();
  339. //DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
  340. //dsi.Company = SiteName;
  341. //xssfworkbook.DocumentSummaryInformation = dsi;
  342. //SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
  343. //si.Subject = excelName;
  344. //xssfworkbook.SummaryInformation = si;
  345. xssfworkbook.CreateSheet("Sheet1");
  346. }
  347. public void DownLoadFile()
  348. {
  349. using (MemoryStream ms = new MemoryStream())
  350. {
  351. xssfworkbook.Write(ms);
  352. Response.Clear();
  353. Response.Buffer = true;
  354. //Response.Charset = "GB2312";
  355. Response.Charset = "UTF-8";
  356. //Response.AppendHeader("Content-Disposition", "attachment;filename=" + excelName + ".xls");
  357. Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(excelName + ".xlsx", System.Text.Encoding.UTF8));
  358. Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
  359. Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
  360. ms.WriteTo(Response.OutputStream);
  361. xssfworkbook = null;
  362. iSheet = null;
  363. }
  364. //Response.Write(ExportTable(data, columns));
  365. //Response.End();
  366. }
  367. public void DownLoadXLSXFile()
  368. {
  369. using (MemoryStream ms = new MemoryStream())
  370. {
  371. xssfworkbook.Write(ms);
  372. Response.Clear();
  373. Response.Buffer = true;
  374. //Response.Charset = "GB2312";
  375. Response.Charset = "UTF-8";
  376. //Response.AppendHeader("Content-Disposition", "attachment;filename=" + excelName + ".xls");
  377. Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(excelName + ".xlsx", System.Text.Encoding.UTF8));
  378. Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
  379. Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
  380. ms.WriteTo(Response.OutputStream);
  381. xssfworkbook = null;
  382. iSheet = null;
  383. }
  384. //Response.Write(ExportTable(data, columns));
  385. //Response.End();
  386. }
  387. public void WriteToFile(string exFile)
  388. {
  389. //Write the stream data of workbook to the root directory
  390. if (exFile == "") return;
  391. using (FileStream file = new FileStream(exFile, FileMode.OpenOrCreate))
  392. {
  393. xssfworkbook.Write(file);
  394. xssfworkbook = null;
  395. iSheet = null;
  396. }
  397. }
  398. #endregion
  399. }
  400. }