Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.00 KB | None | 0 0
  1. using NPOI.HSSF.Record.Crypto;
  2. using NPOI.HSSF.UserModel;
  3. using NPOI.SS.UserModel;
  4. using NPOI.SS.Util;
  5. using NPOI.XSSF.UserModel;
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.Data;
  10. using System.Diagnostics;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Web;
  14.  
  15. /// <summary>
  16. /// Class1 的摘要描述
  17. /// </summary>
  18. public class BDiseaseExportExcelUtil
  19. {
  20. public HSSFWorkbook workbook;
  21. private ISheet worksheet;
  22. //private DataTable data;
  23. private string fileName;
  24. //private ArrayList tableHeader;
  25.  
  26. public int columnSize;
  27. public int rowCount = 0;
  28.  
  29. public ICellStyle headerCenterStyle;
  30. public ICellStyle headerLeftStyle;
  31. public ICellStyle headerRightStyle;
  32. public ICellStyle tableHeaderStyle;
  33. public ICellStyle tableDataStyle;
  34.  
  35. public BDiseaseExportExcelUtil(string fileName, int columnSize)
  36. {
  37.  
  38. //FileStream fs = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath("~/template.xls"), FileMode.Open, FileAccess.ReadWrite);
  39. //workbook = new HSSFWorkbook(fs);
  40. workbook = new HSSFWorkbook();
  41. //fs.Close();
  42.  
  43. this.fileName = fileName;
  44. //this.tableHeader = tableHeader;
  45. //this.data = data;
  46. this.columnSize = columnSize;
  47.  
  48. IFont headerFont = workbook.CreateFont();
  49. headerFont.Boldweight = (short)FontBoldWeight.Bold;
  50. headerFont.FontHeightInPoints = 12;
  51.  
  52. IFont dataFont = workbook.CreateFont();
  53. dataFont.FontHeightInPoints = 12;
  54.  
  55. headerCenterStyle = workbook.CreateCellStyle();
  56. headerCenterStyle.Alignment = HorizontalAlignment.Center;
  57. headerCenterStyle.SetFont(headerFont);
  58.  
  59. headerLeftStyle = workbook.CreateCellStyle();
  60. headerLeftStyle.SetFont(headerFont);
  61.  
  62. headerRightStyle = workbook.CreateCellStyle();
  63. headerRightStyle.Alignment = HorizontalAlignment.Right;
  64. headerRightStyle.SetFont(headerFont);
  65.  
  66. tableHeaderStyle = workbook.CreateCellStyle();
  67. tableHeaderStyle.WrapText = true;
  68. tableHeaderStyle.Alignment = HorizontalAlignment.Center;
  69. tableHeaderStyle.VerticalAlignment = VerticalAlignment.Center;
  70. tableHeaderStyle.FillPattern = NPOI.SS.UserModel.FillPattern.SolidForeground;
  71. tableHeaderStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index;
  72. tableHeaderStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  73. tableHeaderStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  74. tableHeaderStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  75. tableHeaderStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  76. tableHeaderStyle.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
  77. tableHeaderStyle.LeftBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
  78. tableHeaderStyle.RightBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
  79. tableHeaderStyle.TopBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
  80. tableHeaderStyle.SetFont(headerFont);
  81.  
  82. tableDataStyle = workbook.CreateCellStyle();
  83. tableDataStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  84. tableDataStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  85. tableDataStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  86. tableDataStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  87. tableDataStyle.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
  88. tableDataStyle.LeftBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
  89. tableDataStyle.RightBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
  90. tableDataStyle.TopBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
  91. tableDataStyle.SetFont(dataFont);
  92.  
  93. worksheet = workbook.CreateSheet(fileName);
  94.  
  95. //指定直式或橫式 true=橫式 false=直式
  96. worksheet.PrintSetup.Landscape = true;
  97. //指定紙張大小 A3=8, A4=9, Letter=1
  98. worksheet.PrintSetup.PaperSize = 9;
  99. //設定邊距置中
  100. worksheet.HorizontallyCenter = true;
  101. //sheet.VerticallyCenter = true;
  102. //設定一頁寬
  103. worksheet.Autobreaks = true;
  104. worksheet.PrintSetup.FitHeight = (short)0;
  105. worksheet.PrintSetup.FitWidth = (short)1;
  106.  
  107. //Water Print
  108. worksheet.Header.Center = "&50 &K808080 \n\n\n &D &T " + AuthServer.GetLoginUser().UserName;
  109. worksheet.ProtectSheet(AuthServer.GetLoginUser().LoginName);
  110.  
  111. }
  112.  
  113. public void CopyRow(HSSFWorkbook workbook, ISheet worksheet, int sourceRowNum, int destinationRowNum)
  114. {
  115. // Get the source / new row
  116. IRow newRow = worksheet.GetRow(destinationRowNum);
  117. IRow sourceRow = worksheet.GetRow(sourceRowNum);
  118.  
  119. // If the row exist in destination, push down all rows by 1 else create a new row
  120. if (newRow != null)
  121. {
  122. worksheet.ShiftRows(destinationRowNum, worksheet.LastRowNum, 1);
  123. }
  124. else
  125. {
  126. newRow = worksheet.CreateRow(destinationRowNum);
  127. }
  128.  
  129. // Loop through source columns to add to new row
  130. for (int i = 0; i < sourceRow.LastCellNum; i++)
  131. {
  132. // Grab a copy of the old/new cell
  133. ICell oldCell = sourceRow.GetCell(i);
  134. ICell newCell = newRow.CreateCell(i);
  135.  
  136. // If the old cell is null jump to next cell
  137. if (oldCell == null)
  138. {
  139. newCell = null;
  140. continue;
  141. }
  142.  
  143. // Copy style from old cell and apply to new cell
  144. ICellStyle newCellStyle = workbook.CreateCellStyle();
  145. newCellStyle.CloneStyleFrom(oldCell.CellStyle);
  146. newCell.CellStyle = newCellStyle;
  147.  
  148. // If there is a cell comment, copy
  149. if (newCell.CellComment != null) newCell.CellComment = oldCell.CellComment;
  150.  
  151. // If there is a cell hyperlink, copy
  152. if (oldCell.Hyperlink != null) newCell.Hyperlink = oldCell.Hyperlink;
  153.  
  154. // Set the cell data type
  155. newCell.SetCellType(oldCell.CellType);
  156.  
  157. // Set the cell data value
  158. switch (oldCell.CellType)
  159. {
  160. case CellType.Blank:
  161. newCell.SetCellValue(oldCell.StringCellValue);
  162. break;
  163. case CellType.Boolean:
  164. newCell.SetCellValue(oldCell.BooleanCellValue);
  165. break;
  166. case CellType.Error:
  167. newCell.SetCellErrorValue(oldCell.ErrorCellValue);
  168. break;
  169. case CellType.Formula:
  170. newCell.SetCellFormula(oldCell.CellFormula);
  171. break;
  172. case CellType.Numeric:
  173. newCell.SetCellValue(oldCell.NumericCellValue);
  174. break;
  175. case CellType.String:
  176. newCell.SetCellValue(oldCell.RichStringCellValue);
  177. break;
  178. case CellType.Unknown:
  179. newCell.SetCellValue(oldCell.StringCellValue);
  180. break;
  181. }
  182.  
  183. }
  184.  
  185. // If there are are any merged regions in the source row, copy to new row
  186. for (int i = 0; i < worksheet.NumMergedRegions; i++)
  187. {
  188. CellRangeAddress cellRangeAddress = worksheet.GetMergedRegion(i);
  189. if (cellRangeAddress.FirstRow == sourceRow.RowNum)
  190. {
  191. CellRangeAddress newCellRangeAddress = new CellRangeAddress(newRow.RowNum,
  192. (newRow.RowNum +
  193. (cellRangeAddress.FirstRow -
  194. cellRangeAddress.LastRow)),
  195. cellRangeAddress.FirstColumn,
  196. cellRangeAddress.LastColumn);
  197. worksheet.AddMergedRegion(newCellRangeAddress);
  198. }
  199. }
  200. }
  201.  
  202. public void insertReportHeaderCenter(string cellText)
  203. {
  204. ICell cell = worksheet.CreateRow(rowCount).CreateCell(0);
  205. cell.CellStyle = headerCenterStyle;
  206. cell.SetCellValue(cellText);
  207. worksheet.AddMergedRegion(new CellRangeAddress(rowCount, rowCount, 0, columnSize));
  208. rowCount++;
  209. }
  210. public void insertReportHeaderLeftRight(string leftText, string rightText)
  211. {
  212. ICell cell = worksheet.CreateRow(rowCount).CreateCell(0);
  213. cell.CellStyle = headerLeftStyle;
  214. cell.SetCellValue(leftText);
  215. worksheet.AddMergedRegion(new CellRangeAddress(rowCount, rowCount, 0, columnSize / 2));
  216.  
  217. cell = worksheet.GetRow(rowCount).CreateCell(columnSize / 2 + 1);
  218. cell.CellStyle = headerRightStyle;
  219. cell.SetCellValue(rightText);
  220. worksheet.AddMergedRegion(new CellRangeAddress(rowCount, rowCount, columnSize / 2 + 1, columnSize));
  221.  
  222. rowCount++;
  223. }
  224.  
  225. public void insertReportHeaderCenter(string cellText, int rowCount)
  226. {
  227. ICell cell = worksheet.CreateRow(rowCount).CreateCell(0);
  228. cell.CellStyle = headerCenterStyle;
  229. cell.SetCellValue(cellText);
  230. worksheet.AddMergedRegion(new CellRangeAddress(rowCount, rowCount, 0, columnSize));
  231. }
  232.  
  233. public void insertReportHeaderLeftRight(string leftText, string rightText, int rowCount)
  234. {
  235. ICell cell = worksheet.CreateRow(rowCount).CreateCell(0);
  236. cell.CellStyle = headerLeftStyle;
  237. cell.SetCellValue(leftText);
  238. worksheet.AddMergedRegion(new CellRangeAddress(rowCount, rowCount, 0, columnSize / 2));
  239.  
  240. cell = worksheet.GetRow(rowCount).CreateCell(columnSize / 2 + 1);
  241. cell.CellStyle = headerRightStyle;
  242. cell.SetCellValue(rightText);
  243. worksheet.AddMergedRegion(new CellRangeAddress(rowCount, rowCount, columnSize / 2 + 1, columnSize));
  244. }
  245.  
  246.  
  247. public void insertReportHeaderLeft(string cellText)
  248. {
  249. ICell cell = worksheet.CreateRow(rowCount).CreateCell(0);
  250. cell.CellStyle = headerLeftStyle;
  251. cell.SetCellValue(cellText);
  252. worksheet.AddMergedRegion(new CellRangeAddress(rowCount, rowCount, 0, columnSize / 2));
  253. rowCount++;
  254. }
  255. public void insertReportHeaderRight(string cellText)
  256. {
  257. ICell cell = worksheet.CreateRow(rowCount).CreateCell(columnSize / 2 + 1);
  258. cell.CellStyle = headerRightStyle;
  259. cell.SetCellValue(cellText);
  260. worksheet.AddMergedRegion(new CellRangeAddress(rowCount, rowCount, columnSize / 2 + 1, columnSize));
  261. rowCount++;
  262. }
  263.  
  264. public static string CalcDivide(Double a, Double b)
  265. {
  266. if (a == 0 || b == 0)
  267. {
  268. return "0%";
  269. }
  270. else
  271. {
  272. return Math.Round(a / b * 100, 2, MidpointRounding.AwayFromZero) + "%";
  273. }
  274. }
  275.  
  276. public static string CalcDivide(string x, string y)
  277. {
  278. Double a = Convert.ToDouble(x);
  279. Double b = Convert.ToDouble(y);
  280. if (a == 0 || b == 0)
  281. {
  282. return "0%";
  283. }
  284. else
  285. {
  286. return Math.Round(a / b * 100, 2, MidpointRounding.AwayFromZero) + "%";
  287. }
  288. }
  289.  
  290. public static string CalcDivide(object x, object y)
  291. {
  292. Double a = Convert.ToDouble(x);
  293. Double b = Convert.ToDouble(y);
  294. if (a == 0 || b == 0)
  295. {
  296. return "0%";
  297. }
  298. else
  299. {
  300. return Math.Round(a / b * 100, 2, MidpointRounding.AwayFromZero) + "%";
  301. }
  302. }
  303.  
  304. public static string CalcDivideNonPercent(object x, object y)
  305. {
  306. if (x == null || y == null) return "0";
  307. if (x == DBNull.Value || y == DBNull.Value) return "0";
  308. Double a = Convert.ToDouble(x);
  309. Double b = Convert.ToDouble(y);
  310. if (a == 0 || b == 0)
  311. {
  312. return "0";
  313. }
  314. else
  315. {
  316. return Math.Round(a / b * 100, 2, MidpointRounding.AwayFromZero).ToString();
  317. }
  318. }
  319.  
  320.  
  321. public static string CalcSum(object a, object b)
  322. {
  323. int x = Convert.ToInt32(a);
  324. int y = Convert.ToInt32(b);
  325. return Convert.ToString(x + y);
  326. }
  327.  
  328. public static string CalcSum(string a, string b)
  329. {
  330. int x = Convert.ToInt32(a);
  331. int y = Convert.ToInt32(b);
  332. return Convert.ToString(x + y);
  333. }
  334.  
  335. /*
  336. public MemoryStream export()
  337. {
  338. #region Table Header
  339.  
  340. foreach (List<KeyValuePair<string, int[]>> tableHeaderList in tableHeader)
  341. {
  342. IRow tableHeaderRow = worksheet.CreateRow(rowCount);
  343. int initCellPosition = 0;
  344. for(int i = 0; i< tableHeaderList.Count; i++)
  345. {
  346. KeyValuePair<string, int[]> pair = tableHeaderList.ElementAt(i);
  347. int[] range = pair.Value;
  348. ICell cell = tableHeaderRow.CreateCell(i + initCellPosition);
  349. cell.SetCellValue(pair.Key);
  350. worksheet.AddMergedRegion(new CellRangeAddress(rowCount, rowCount + range[0], i + initCellPosition, i + initCellPosition + range[1]));
  351. cell.CellStyle = tableHeaderStyle;
  352. initCellPosition = range[1];
  353. }
  354. rowCount++;
  355.  
  356. }
  357. #endregion
  358.  
  359. #region rowdata
  360. for (int i = 0; i < data.Rows.Count; i++)
  361. {
  362. IRow row = worksheet.CreateRow(++rowCount);
  363. for (int j = 0; j < data.Columns.Count; j++)
  364. {
  365. row.CreateCell(j).SetCellValue(data.Rows[i][j].ToString());
  366. }
  367. }
  368. #endregion
  369.  
  370. MemoryStream ms = new MemoryStream();
  371. workbook.Write(ms);
  372. workbook = null;
  373. return ms;
  374.  
  375. }
  376. */
  377. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement