ReportBaseX.cs 18 KB

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