ReportBase.cs 17 KB

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