ZZDianXin_API - 郑州电信演示

NPOIHelper.cs 65KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  1. 
  2. using System;
  3. using System.Data;
  4. using System.IO;
  5. using System.Text;
  6. using System.Web;
  7. using NPOI.HSSF.UserModel;
  8. using NPOI.SS.UserModel;
  9. using NPOI.SS.Util;
  10. using NPOI.XSSF.UserModel;
  11. using CallCenterApi.DB;
  12. using System.Collections.Generic;
  13. namespace CallCenter.Utility
  14. {
  15. public class NPOIHelper
  16. {
  17. private string _title;
  18. private string _sheetName;
  19. private string _filePath;
  20. /// <summary>
  21. /// 导出到Excel
  22. /// </summary>
  23. /// <param name="table"></param>
  24. /// <returns></returns>
  25. public bool ToExcel(DataTable table, string[] columns = null)
  26. {
  27. FileStream fs = new FileStream(this._filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  28. IWorkbook workBook = new HSSFWorkbook();
  29. if (string.IsNullOrWhiteSpace(this._sheetName))
  30. {
  31. this._sheetName = "sheet1";
  32. }
  33. ISheet sheet = workBook.CreateSheet(this._sheetName);
  34. //处理表格标题
  35. IRow row = sheet.CreateRow(0);
  36. row.CreateCell(0).SetCellValue(this._title);
  37. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, table.Columns.Count - 1));
  38. row.Height = 500;
  39. ICellStyle cellStyle = workBook.CreateCellStyle();
  40. IFont font = workBook.CreateFont();
  41. font.FontName = "微软雅黑";
  42. font.FontHeightInPoints = 17;
  43. cellStyle.SetFont(font);
  44. cellStyle.VerticalAlignment = VerticalAlignment.Center;
  45. cellStyle.Alignment = HorizontalAlignment.Center;
  46. row.Cells[0].CellStyle = cellStyle;
  47. //处理表格列头
  48. row = sheet.CreateRow(1);
  49. if (columns == null)
  50. {
  51. for (int i = 0; i < table.Columns.Count; i++)
  52. {
  53. row.CreateCell(i).SetCellValue(table.Columns[i].ColumnName);
  54. row.Height = 350;
  55. sheet.AutoSizeColumn(i);
  56. }
  57. }
  58. else
  59. {
  60. for (int i = 0; i < columns.Length; i++)
  61. {
  62. row.CreateCell(i).SetCellValue(columns[i]);
  63. row.Height = 350;
  64. sheet.AutoSizeColumn(i);
  65. }
  66. }
  67. //处理数据内容
  68. for (int i = 0; i < table.Rows.Count; i++)
  69. {
  70. row = sheet.CreateRow(2 + i);
  71. row.Height = 250;
  72. for (int j = 0; j < table.Columns.Count; j++)
  73. {
  74. row.CreateCell(j).SetCellValue(table.Rows[i][j].ToString());
  75. sheet.SetColumnWidth(j, 256 * 15);
  76. }
  77. }
  78. //写入数据流
  79. workBook.Write(fs);
  80. fs.Flush();
  81. fs.Close();
  82. return true;
  83. }
  84. /// <summary>
  85. /// 导出到Excel
  86. /// </summary>
  87. /// <param name="table"></param>
  88. /// <param name="title"></param>
  89. /// <param name="sheetName">空字符串或null的话默认sheet1</param>
  90. /// <param name="columns">自定义表格列头,默认null</param>
  91. /// <returns></returns>
  92. public bool ToExcel(DataTable table, string title, string sheetName, string filePath, string[] columns = null)
  93. {
  94. this._title = title;
  95. this._sheetName = sheetName;
  96. this._filePath = filePath;
  97. return ToExcel(table, columns);
  98. }
  99. /// <summary>
  100. /// 弹出下载框导出excel
  101. /// </summary>
  102. /// <param name="Name"></param>
  103. /// <param name="dt"></param>
  104. /// <returns></returns>
  105. public string ExportToExcel(string Name, DataTable dt, string[] cols = null)
  106. {
  107. try
  108. {
  109. //if (dt.Rows.Count > 0)
  110. //{
  111. HSSFWorkbook workbook = new HSSFWorkbook();
  112. ISheet sheet = workbook.CreateSheet("Sheet1");
  113. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  114. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  115. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  116. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  117. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  118. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  119. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  120. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  121. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  122. //字体
  123. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  124. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  125. headerfont.FontHeightInPoints = 12;
  126. HeadercellStyle.SetFont(headerfont);
  127. //用column name 作为列名
  128. int icolIndex = 0;
  129. IRow headerRow = sheet.CreateRow(0);
  130. if (cols == null || (cols != null && cols.Length == 0))
  131. {
  132. foreach (DataColumn dc in dt.Columns)
  133. {
  134. ICell cell = headerRow.CreateCell(icolIndex);
  135. cell.SetCellValue(dc.ColumnName);
  136. cell.CellStyle = HeadercellStyle;
  137. icolIndex++;
  138. }
  139. }
  140. else
  141. {
  142. foreach (string dc in cols)
  143. {
  144. ICell cell = headerRow.CreateCell(icolIndex);
  145. cell.SetCellValue(dc);
  146. cell.CellStyle = HeadercellStyle;
  147. icolIndex++;
  148. }
  149. }
  150. ICellStyle cellStyle = workbook.CreateCellStyle();
  151. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  152. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  153. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  154. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  155. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  156. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  157. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  158. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  159. cellStyle.SetFont(cellfont);
  160. //建立内容行
  161. int iRowIndex = 0;
  162. foreach (DataRow dr in dt.Rows)
  163. {
  164. int iCellIndex = 0;
  165. IRow irow = sheet.CreateRow(iRowIndex + 1);
  166. for (int i = 0; i < dt.Columns.Count; i++)
  167. {
  168. string strsj = string.Empty;
  169. if (dr[i] != null)
  170. {
  171. strsj = dr[i].ToString();
  172. }
  173. ICell cell = irow.CreateCell(iCellIndex);
  174. cell.SetCellValue(strsj);
  175. cell.CellStyle = cellStyle;
  176. iCellIndex++;
  177. }
  178. iRowIndex++;
  179. }
  180. //自适应列宽度
  181. for (int i = 0; i < icolIndex; i++)
  182. {
  183. sheet.AutoSizeColumn(i);
  184. }
  185. using (MemoryStream ms = new MemoryStream())
  186. {
  187. workbook.Write(ms);
  188. HttpContext curContext = HttpContext.Current;
  189. // 设置编码和附件格式
  190. curContext.Response.ContentType = "application/vnd.ms-excel";
  191. curContext.Response.ContentEncoding = Encoding.UTF8;
  192. curContext.Response.Charset = "";
  193. curContext.Response.AppendHeader("Content-Disposition",
  194. "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
  195. curContext.Response.BinaryWrite(ms.GetBuffer());
  196. workbook = null;
  197. ms.Close();
  198. ms.Dispose();
  199. curContext.Response.End();
  200. }
  201. //}
  202. return "";
  203. }
  204. catch
  205. {
  206. return "导出失败!";
  207. }
  208. }
  209. /// <summary>
  210. /// 合并单元格
  211. /// </summary>
  212. /// <param name="sheet">要合并单元格所在的sheet</param>
  213. /// <param name="rowstart">开始行的索引</param>
  214. /// <param name="rowend">结束行的索引</param>
  215. /// <param name="colstart">开始列的索引</param>
  216. /// <param name="colend">结束列的索引</param>
  217. public static void SetCellRangeAddress(ISheet sheet, int rowstart, int rowend, int colstart, int colend)
  218. {
  219. CellRangeAddress cellRangeAddress = new CellRangeAddress(rowstart, rowend, colstart, colend);
  220. sheet.AddMergedRegion(cellRangeAddress);
  221. }
  222. /// <summary>
  223. /// 工单类型弹出下载框导出excel
  224. /// </summary>
  225. /// <param name="Name"></param>
  226. /// <param name="dt"></param>
  227. /// <param name="typeclass">仪器或试剂</param>
  228. /// <returns></returns>
  229. public string ExportToExcelForGDLX(string Name, DataTable dt, string typeclass, List<string> colnames, List<int> erows, List<string> secolnames)
  230. {
  231. try
  232. {
  233. HSSFWorkbook workbook = new HSSFWorkbook();
  234. ISheet sheet = workbook.CreateSheet("Sheet1");
  235. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  236. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  237. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  238. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  239. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  240. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  241. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  242. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  243. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  244. //字体
  245. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  246. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  247. headerfont.FontHeightInPoints = 12;
  248. HeadercellStyle.SetFont(headerfont);
  249. //用column name 作为列名
  250. int icolIndex = 0;
  251. IRow headerRow = sheet.CreateRow(0);
  252. for (int i = 0; i < colnames.Count; i++)
  253. {
  254. ICell cell = headerRow.CreateCell(i);
  255. cell.SetCellValue(colnames[i]);
  256. cell.CellStyle = HeadercellStyle;
  257. //SetCellRangeAddress(sheet, 0, 0, erows[i * 2], erows[i * 2 + 1]);
  258. }
  259. for (int k = 0; k < erows.Count / 2; k++)
  260. {
  261. SetCellRangeAddress(sheet, 0, 0, erows[k * 2], erows[k * 2 + 1]);
  262. }
  263. //添加第二行标题
  264. IRow SecRow = sheet.CreateRow(1);
  265. for (int i = 0; i < secolnames.Count; i++)
  266. {
  267. ICell cell = SecRow.CreateCell(i);
  268. cell.SetCellValue(secolnames[i].ToString());
  269. }
  270. ICellStyle cellStyle = workbook.CreateCellStyle();
  271. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  272. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  273. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  274. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  275. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  276. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  277. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  278. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  279. cellStyle.SetFont(cellfont);
  280. //建立内容行
  281. int iRowIndex = 0;
  282. foreach (DataRow dr in dt.Rows)
  283. {
  284. int iCellIndex = 0;
  285. IRow irow = sheet.CreateRow(iRowIndex + 2);
  286. for (int i = 0; i < dt.Columns.Count; i++)
  287. {
  288. string strsj = string.Empty;
  289. if (dr[i] != null)
  290. {
  291. strsj = dr[i].ToString();
  292. }
  293. ICell cell = irow.CreateCell(iCellIndex);
  294. cell.SetCellValue(strsj);
  295. cell.CellStyle = cellStyle;
  296. iCellIndex++;
  297. }
  298. iRowIndex++;
  299. }
  300. //自适应列宽度
  301. for (int i = 0; i < icolIndex; i++)
  302. {
  303. sheet.AutoSizeColumn(i);
  304. }
  305. using (MemoryStream ms = new MemoryStream())
  306. {
  307. workbook.Write(ms);
  308. HttpContext curContext = HttpContext.Current;
  309. // 设置编码和附件格式
  310. curContext.Response.ContentType = "application/vnd.ms-excel";
  311. curContext.Response.ContentEncoding = Encoding.UTF8;
  312. curContext.Response.Charset = "";
  313. curContext.Response.AppendHeader("Content-Disposition",
  314. "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
  315. curContext.Response.BinaryWrite(ms.GetBuffer());
  316. workbook = null;
  317. ms.Close();
  318. ms.Dispose();
  319. curContext.Response.End();
  320. }
  321. return "";
  322. }
  323. catch (Exception e)
  324. {
  325. return "导出失败!" + e.Message;
  326. }
  327. }
  328. /// <summary>
  329. /// 导入excel转换为datatable
  330. /// </summary>
  331. /// <param name="upfile"></param>
  332. /// <param name="headrow"></param>
  333. /// <returns></returns>
  334. public DataTable ExcelToTable(HttpPostedFile upfile, int headrow)
  335. {
  336. DataTable dt = new DataTable();
  337. IWorkbook workbook = null;
  338. Stream stream = upfile.InputStream;
  339. string suffix = upfile.FileName.Substring(upfile.FileName.LastIndexOf(".") + 1).ToLower();
  340. if (suffix == "xlsx") // 2007版本
  341. {
  342. workbook = new XSSFWorkbook(stream);
  343. }
  344. else if (suffix == "xls") // 2003版本
  345. {
  346. workbook = new HSSFWorkbook(stream);
  347. }
  348. //获取excel的第一个sheet
  349. ISheet sheet = workbook.GetSheetAt(0);
  350. //获取sheet的第一行
  351. IRow headerRow = sheet.GetRow(headrow);
  352. //一行最后一个方格的编号 即总的列数
  353. int cellCount = headerRow.LastCellNum;
  354. //最后一列的标号 即总的行数
  355. int rowCount = sheet.LastRowNum;
  356. //列名
  357. for (int i = 0; i < cellCount; i++)
  358. {
  359. dt.Columns.Add(headerRow.GetCell(i).ToString());
  360. }
  361. for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++)
  362. {
  363. DataRow dr = dt.NewRow();
  364. IRow row = sheet.GetRow(i);
  365. for (int j = row.FirstCellNum; j < cellCount; j++)
  366. {
  367. if (row.GetCell(j) != null)
  368. {
  369. dr[j] = row.GetCell(j).ToString();
  370. }
  371. }
  372. dt.Rows.Add(dr);
  373. }
  374. sheet = null;
  375. workbook = null;
  376. return dt;
  377. }
  378. /// <summary>
  379. /// 导入excel转换为datatable
  380. /// </summary>
  381. /// <param name="upfile"></param>
  382. /// <param name="headrow"></param>
  383. /// <returns></returns>
  384. public DataTable ExcelToTable(string fileName, int headrow)
  385. {
  386. DataTable dt = new DataTable();
  387. IWorkbook workbook = null;
  388. Stream stream = new FileStream(fileName, FileMode.OpenOrCreate);
  389. string suffix = fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower();
  390. if (suffix == "xlsx") // 2007版本
  391. {
  392. workbook = new XSSFWorkbook(stream);
  393. }
  394. else if (suffix == "xls") // 2003版本
  395. {
  396. workbook = new HSSFWorkbook(stream);
  397. }
  398. //获取excel的第一个sheet
  399. ISheet sheet = workbook.GetSheetAt(0);
  400. //获取sheet的第一行
  401. IRow headerRow = sheet.GetRow(headrow);
  402. //一行最后一个方格的编号 即总的列数
  403. int cellCount = headerRow.LastCellNum;
  404. //最后一列的标号 即总的行数
  405. int rowCount = sheet.LastRowNum;
  406. //列名
  407. for (int i = 0; i < cellCount; i++)
  408. {
  409. dt.Columns.Add(headerRow.GetCell(i).ToString());
  410. }
  411. for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++)
  412. {
  413. DataRow dr = dt.NewRow();
  414. IRow row = sheet.GetRow(i);
  415. for (int j = row.FirstCellNum; j < cellCount; j++)
  416. {
  417. if (row.GetCell(j) != null)
  418. {
  419. dr[j] = row.GetCell(j).ToString();
  420. }
  421. }
  422. dt.Rows.Add(dr);
  423. }
  424. sheet = null;
  425. workbook = null;
  426. return dt;
  427. }
  428. /// <summary>
  429. /// 简报导出
  430. /// </summary>
  431. /// <param name="ds"></param>
  432. /// <returns></returns>
  433. public string MarketExportToExcel(string stime,string etime,Market market )
  434. {
  435. try
  436. {
  437. HSSFWorkbook workbook = new HSSFWorkbook();
  438. ISheet sheet = workbook.CreateSheet("Sheet1");
  439. ICellStyle cellStyle = workbook.CreateCellStyle();
  440. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  441. //cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  442. //cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  443. //cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  444. //cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  445. //cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  446. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  447. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  448. cellStyle.SetFont(cellfont);
  449. ICellStyle cellStylebt = workbook.CreateCellStyle();
  450. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  451. cellfontbt.Boldweight = (short)FontBoldWeight.Bold;
  452. cellStylebt.SetFont(cellfontbt);
  453. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  454. cellStylebt.Alignment = HorizontalAlignment.Center;
  455. IRow irow1 = sheet.CreateRow(1);
  456. ICell cell1 = irow1.CreateCell(0);
  457. cell1.SetCellValue("时间:" + stime + " 至 " + etime);
  458. cell1.CellStyle = cellStylebt;
  459. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 0, 7));
  460. //((HSSFSheet)sheet).SetEnclosedBorderOfRegion(new CellRangeAddress(1, 1, 0, 7), BorderStyle.Thin, NPOI.HSSF.Util.HSSFColor.Black.Index);
  461. #region 产品投诉情况
  462. List<Ification> dt1 = market.product ;
  463. IRow irow2 = sheet.CreateRow(2);
  464. ICell cell2 = irow2.CreateCell(0);
  465. cell2.SetCellValue("产品投诉情况");
  466. cell2.CellStyle = cellStylebt;
  467. sheet.AddMergedRegion(new CellRangeAddress(2, 2, 0, 7));
  468. int dtrow = 3;
  469. if (dt1.Count >0)
  470. {
  471. int dt = 0;
  472. int a = dt1.Count / 3;
  473. float b = dt1.Count % 3;
  474. if (b >0)
  475. a = a + 1;
  476. dtrow += a;
  477. List<IRow> irow3list = new List<IRow>();
  478. for (int i =0;i <a;i++)
  479. {
  480. IRow irow3 = sheet.CreateRow(3 + i);
  481. irow3list.Add(irow3);
  482. }
  483. int td = 0;
  484. foreach (var it in dt1)
  485. {
  486. dt++;
  487. if (dt <4)
  488. {
  489. ICell cell101 = irow3list[td].CreateCell(3 * dt - 3);
  490. cell101.SetCellValue(it.name );
  491. cell101.CellStyle = cellStylebt;
  492. ICell cell102 = irow3list[td].CreateCell(3 * dt - 2);
  493. cell102.SetCellValue(it .number );
  494. cell102.CellStyle = cellStylebt;
  495. ICell cell103= irow3list[td].CreateCell(3 * dt-1 );
  496. cell103.SetCellValue(it.Proportion );
  497. cell103.CellStyle = cellStylebt;
  498. }
  499. else
  500. {
  501. dt = 1;
  502. td++;
  503. ICell cell101 = irow3list[td].CreateCell(3 * dt - 3);
  504. cell101.SetCellValue(it.name);
  505. cell101.CellStyle = cellStylebt;
  506. ICell cell102 = irow3list[td].CreateCell(3 * dt - 2);
  507. cell102.SetCellValue(it.number);
  508. cell102.CellStyle = cellStylebt;
  509. ICell cell103 = irow3list[td].CreateCell(3 * dt-1);
  510. cell103.SetCellValue(it.Proportion);
  511. cell103.CellStyle = cellStylebt;
  512. }
  513. }
  514. }
  515. #endregion
  516. #region 服务投诉情况
  517. Ification dt2 = market.service;
  518. dtrow++;
  519. IRow irow4 = sheet.CreateRow(dtrow);
  520. ICell cell4 = irow4.CreateCell(0);
  521. cell4.SetCellValue("服务投诉情况");
  522. cell4.CellStyle = cellStylebt;
  523. sheet.AddMergedRegion(new CellRangeAddress(dtrow, dtrow, 0, 7));
  524. if (dt2!=null )
  525. {
  526. dtrow++;
  527. IRow irow5 = sheet.CreateRow(dtrow);
  528. ICell cell51 = irow5.CreateCell(0);
  529. cell51.SetCellValue(dt2.name);
  530. cell51.CellStyle = cellStylebt;
  531. ICell cell52 = irow5.CreateCell(1);
  532. cell52.SetCellValue(dt2.number);
  533. cell52.CellStyle = cellStylebt;
  534. ICell cell53 = irow5.CreateCell(2);
  535. cell53.SetCellValue(dt2.Proportion);
  536. cell53.CellStyle = cellStylebt;
  537. }
  538. #endregion
  539. #region 涉媒投诉
  540. Ification dt3 = market.sediainvolved;
  541. dtrow++;
  542. IRow irow9 = sheet.CreateRow(dtrow);
  543. ICell cell9 = irow9.CreateCell(0);
  544. cell9.SetCellValue("涉媒投诉");
  545. cell9.CellStyle = cellStylebt;
  546. sheet.AddMergedRegion(new CellRangeAddress(dtrow, dtrow, 0, 7));
  547. if (dt3!=null )
  548. {
  549. dtrow++;
  550. IRow irow10 = sheet.CreateRow(dtrow);
  551. ICell cell53 = irow10.CreateCell(0);
  552. cell53.SetCellValue(dt3.name);
  553. cell53.CellStyle = cellStylebt;
  554. ICell cell54 = irow10.CreateCell(1);
  555. cell54.SetCellValue(dt3.number);
  556. cell54.CellStyle = cellStylebt;
  557. ICell cell55 = irow10.CreateCell(2);
  558. cell55.SetCellValue(dt3.Proportion);
  559. cell55.CellStyle = cellStylebt;
  560. }
  561. #endregion
  562. #region 市场抽检
  563. Ification dt4 = market.spotcheck;
  564. dtrow++;
  565. IRow irow11 = sheet.CreateRow(dtrow);
  566. ICell cell12 = irow11.CreateCell(0);
  567. cell12.SetCellValue("市场抽检");
  568. cell12.CellStyle = cellStylebt;
  569. sheet.AddMergedRegion(new CellRangeAddress(dtrow, dtrow, 0, 7));
  570. if (dt4!=null )
  571. {
  572. dtrow++;
  573. IRow irow12 = sheet.CreateRow(dtrow);
  574. ICell cell55 = irow12.CreateCell(0);
  575. cell55.SetCellValue(dt4.name);
  576. cell55.CellStyle = cellStylebt;
  577. ICell cell56 = irow12.CreateCell(1);
  578. cell56.SetCellValue(dt4.number);
  579. cell56.CellStyle = cellStylebt;
  580. ICell cell57 = irow12.CreateCell(2);
  581. cell57.SetCellValue(dt4.Proportion);
  582. cell57.CellStyle = cellStylebt;
  583. }
  584. #endregion
  585. #region 其他信息
  586. Ification dt5 = market.other;
  587. dtrow++;
  588. IRow irow13 = sheet.CreateRow(dtrow);
  589. ICell cell13 = irow13.CreateCell(0);
  590. cell13.SetCellValue("其他信息");
  591. cell13.CellStyle = cellStylebt;
  592. sheet.AddMergedRegion(new CellRangeAddress(dtrow, dtrow, 0, 7));
  593. if (dt5!=null )
  594. {
  595. dtrow++;
  596. IRow irow15 = sheet.CreateRow(dtrow);
  597. ICell cell57 = irow15.CreateCell(0);
  598. cell57.SetCellValue(dt4.name);
  599. cell57.CellStyle = cellStylebt;
  600. ICell cell58 = irow15.CreateCell(1);
  601. cell58.SetCellValue(dt4.number);
  602. cell58.CellStyle = cellStylebt;
  603. ICell cell59 = irow15.CreateCell(2);
  604. cell59.SetCellValue(dt5.Proportion);
  605. cell59.CellStyle = cellStylebt;
  606. }
  607. #endregion
  608. //自适应列宽度
  609. for (int i = 0; i < 8; i++)
  610. {
  611. // sheet.AutoSizeColumn(i);
  612. sheet.SetColumnWidth(i, 12 * 256);
  613. }
  614. using (MemoryStream ms = new MemoryStream())
  615. {
  616. workbook.Write(ms);
  617. HttpContext curContext = HttpContext.Current;
  618. // 设置编码和附件格式
  619. curContext.Response.ContentType = "application/vnd.ms-excel";
  620. curContext.Response.ContentEncoding = Encoding.UTF8;
  621. curContext.Response.Charset = "";
  622. curContext.Response.AppendHeader("Content-Disposition",
  623. "attachment;filename=" + HttpUtility.UrlEncode("市场信息简报" + ".xls", Encoding.UTF8));
  624. curContext.Response.BinaryWrite(ms.GetBuffer());
  625. workbook = null;
  626. ms.Close();
  627. ms.Dispose();
  628. curContext.Response.End();
  629. }
  630. return "";
  631. }
  632. catch
  633. {
  634. return "导出失败!";
  635. }
  636. }
  637. private void Columnwidth(int number, ISheet ffSheet)
  638. {
  639. for (int columnNum = 0; columnNum <= number; columnNum++)
  640. {
  641. int columnWidth = ffSheet.GetColumnWidth(columnNum) / 256;//获取当前列宽度
  642. for (int rowNum = 1; rowNum <= ffSheet.LastRowNum; rowNum++)//在这一列上循环行
  643. {
  644. IRow currentRow = ffSheet.GetRow(rowNum);
  645. ICell currentCell = currentRow.GetCell(columnNum);
  646. int length = Encoding.UTF8.GetBytes(currentCell.ToString()).Length;//获取当前单元格的内容宽度
  647. if (columnWidth < length + 1)
  648. {
  649. columnWidth = length + 1;
  650. }//若当前单元格内容宽度大于列宽,则调整列宽为当前单元格宽度,后面的+1是我人为的将宽度增加一个字符
  651. }
  652. }
  653. }
  654. private string ReturnMsg(int j, int i, List<Factory> Factory1)
  655. {
  656. string msg = "";
  657. switch (j)
  658. {
  659. case 0:
  660. msg = Factory1[i * 2].name;
  661. break;
  662. case 1:
  663. msg = Factory1[i * 2].number.ToString();
  664. break;
  665. case 2:
  666. msg = Factory1[i * 2].proportion;
  667. break;
  668. case 3:
  669. msg = Factory1[i * 2 + 1].name;
  670. break;
  671. case 4:
  672. msg = Factory1[i * 2 + 1].number.ToString();
  673. break;
  674. case 5:
  675. msg = Factory1[i * 2 + 1].proportion;
  676. break;
  677. }
  678. return msg;
  679. }
  680. /// <summary>
  681. /// 投诉产品日期分布表
  682. /// </summary>
  683. /// <param name="ds"></param>
  684. /// <returns></returns>
  685. public string DistributionToExcel(DateTime datetime, Product product)
  686. {
  687. try
  688. {
  689. HSSFWorkbook workbook = new HSSFWorkbook();
  690. ISheet sheet = workbook.CreateSheet("Sheet1");
  691. ICellStyle cellStyle = workbook.CreateCellStyle();
  692. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  693. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  694. cellStyle.SetFont(cellfont);
  695. ICellStyle cellStylebt = workbook.CreateCellStyle();
  696. cellStylebt.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  697. cellStylebt.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  698. cellStylebt.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  699. cellStylebt.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  700. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  701. cellfontbt.Boldweight = (short)FontBoldWeight.Bold;
  702. cellStylebt.SetFont(cellfontbt);
  703. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  704. cellStylebt.Alignment = HorizontalAlignment.Center;
  705. IRow irow1 = sheet.CreateRow(0);
  706. ICell cell1 = irow1.CreateCell(0);
  707. cell1.SetCellValue(datetime.Year +"年"+datetime .Month +"月份投诉产品日期分布情况表");
  708. cell1.CellStyle = cellStylebt;
  709. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 13));
  710. #region 产品投诉情况
  711. ICellStyle cellStylebt1 = workbook.CreateCellStyle();
  712. cellStylebt1.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  713. cellStylebt1.SetFont(cellfontbt);
  714. cellStylebt1.VerticalAlignment = VerticalAlignment.Center;
  715. cellStylebt1.Alignment = HorizontalAlignment.Center;
  716. IRow irow2 = sheet.CreateRow(1);
  717. ICell cell2 = irow2.CreateCell(0);
  718. ICellStyle style13 = workbook.CreateCellStyle();
  719. style13.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  720. style13.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  721. style13.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  722. style13.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  723. style13.BorderDiagonalLineStyle = BorderStyle.Thin;
  724. style13.BorderDiagonal = BorderDiagonal.Backward;
  725. style13.BorderDiagonalColor = IndexedColors.Black .Index;
  726. string sb = " 数量\n日期";
  727. // StringBuilder sb = new StringBuilder();
  728. // sb.AppendLine("数量".PadLeft(15));//该行不足长度10在左侧填空格
  729. // sb.AppendLine("日期".PadRight(15));//该行不足长度10在右侧填空格
  730. cell2.SetCellValue(sb);
  731. cell2.CellStyle = cellStylebt;
  732. style13.WrapText = true;
  733. irow2.GetCell(0).CellStyle = style13;
  734. sheet.AddMergedRegion(new CellRangeAddress(1, 2, 0,0 ));
  735. ICell cell6 = irow2.CreateCell(1);
  736. cell6.SetCellValue("投诉产品");
  737. cell6.CellStyle = cellStylebt;
  738. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 1, 1));
  739. for (int i=0;i<5;i ++)
  740. {
  741. string msg = "";
  742. switch (i)
  743. {
  744. case 0:
  745. msg = datetime.AddMonths(-11).Month + "-" + datetime.AddMonths(-3).Month + "月份日期";
  746. break;
  747. case 1:
  748. msg = datetime.AddMonths(-2).Month + "月份日期";
  749. break;
  750. case 2:
  751. msg = datetime.AddMonths(-1).Month + "月份日期";
  752. break;
  753. case 3:
  754. msg = datetime.Month + "月份日期";
  755. break;
  756. case 4:
  757. msg = "不清楚日期";
  758. break;
  759. }
  760. ICell cell3 = irow2.CreateCell(i *2+ 2 );
  761. cell3.SetCellValue(msg);
  762. cell3.CellStyle = cellStylebt;
  763. ICell cell4 = irow2.CreateCell(i * 2 + 3);
  764. cell4.CellStyle = cellStylebt;
  765. sheet.AddMergedRegion(new CellRangeAddress(1, 1, i * 2 + 2, i * 2 + 3));
  766. }
  767. IRow irow3 = sheet.CreateRow(2);
  768. for (int i = 0; i < 11; i++)
  769. {
  770. string msg = "";
  771. if (i ==0)
  772. {
  773. msg = "数量";
  774. }
  775. else
  776. {
  777. if (i % 2 == 0)
  778. {
  779. msg = "占比";
  780. }
  781. else
  782. {
  783. msg = "数量";
  784. }
  785. }
  786. ICell cell4 = irow3.CreateCell(i+1);
  787. cell4.SetCellValue(msg);
  788. cell4.CellStyle = cellStylebt;
  789. }
  790. List<Date> dt1 = product.dates;
  791. if (dt1!=null )
  792. {
  793. for (int i=0;i < dt1.Count;i ++)
  794. {
  795. IRow irow4 = sheet.CreateRow(3 + i);
  796. string msg = "";
  797. for (int j = 0; j < 12; j ++)
  798. {
  799. switch (j )
  800. {
  801. case 0:
  802. msg = dt1[i].name;
  803. break;
  804. case 1:
  805. msg = dt1[i].total.ToString();
  806. break;
  807. case 2:
  808. msg = dt1[i].MonthCount1 .ToString ();
  809. break;
  810. case 3:
  811. msg = dt1[i].MonthCountmix1 .ToString();
  812. break;
  813. case 4:
  814. msg = dt1[i].MonthCount2.ToString();
  815. break;
  816. case 5:
  817. msg = dt1[i].MonthCountmix2.ToString();
  818. break;
  819. case 6:
  820. msg = dt1[i].MonthCount3.ToString();
  821. break;
  822. case 7:
  823. msg = dt1[i].MonthCountmix3.ToString();
  824. break;
  825. case 8:
  826. msg = dt1[i].MonthCount4.ToString();
  827. break;
  828. case 9:
  829. msg = dt1[i].MonthCountmix4.ToString();
  830. break;
  831. case 10:
  832. msg = dt1[i].MonthCount5.ToString();
  833. break;
  834. case 11:
  835. msg = dt1[i].MonthCountmix5.ToString();
  836. break;
  837. }
  838. ICell cell5 = irow4.CreateCell(j);
  839. cell5.SetCellValue(msg);
  840. cell5.CellStyle = cellStylebt;
  841. }
  842. }
  843. }
  844. int t = dt1.Count + 2;int count = 0;
  845. List<Factory> Factory1 = product.factory;
  846. List<Factory> Factory2 = product.problem;
  847. List<Factory> Factory3 = product.product;
  848. int factory = 0, problem = 0, productcode = 0;
  849. if (Factory1 != null)
  850. factory = (Factory1.Count / 2) + (0 == Factory1.Count % 2 ? 0 : 1);
  851. if (Factory2 != null)
  852. problem = Factory2.Count;
  853. if (Factory3 != null)
  854. productcode = Factory3.Count;
  855. bool istrue = true; int a = 0, total = problem + productcode;
  856. if (factory>(total))
  857. {
  858. count = factory;
  859. a = factory - total;
  860. }
  861. else
  862. {
  863. istrue = false;
  864. count = total;
  865. }
  866. IRow irow5 = sheet.CreateRow(t+1);
  867. ICell cell7 = irow5.CreateCell(0);
  868. cell7.SetCellValue("工\n厂\n投\n诉\n占\n比\n");
  869. cell7.CellStyle = cellStylebt;
  870. cell7.CellStyle.WrapText=true ;
  871. sheet.AddMergedRegion(new CellRangeAddress(t+1, t+ count, 0, 0));
  872. ICell cell10 = irow5.CreateCell(7);
  873. cell10.SetCellValue("各\n质\n量\n问\n题\n占\n比\n");
  874. cell10.CellStyle = cellStylebt;
  875. cell10.CellStyle.WrapText = true;
  876. int structure = t + problem;
  877. if (istrue)
  878. {
  879. if (a >1)
  880. {
  881. structure = t + problem + a / 2;
  882. sheet.AddMergedRegion(new CellRangeAddress(t+1, t + problem + a / 2, 7, 7));
  883. }
  884. else
  885. {
  886. structure = t + problem;
  887. sheet.AddMergedRegion(new CellRangeAddress(t+1, t + problem, 7, 7));
  888. }
  889. }
  890. else
  891. sheet.AddMergedRegion(new CellRangeAddress(t+1, t + problem, 7, 7));
  892. IRow irow7 = sheet.CreateRow(structure+1);
  893. ICell cell11 = irow7.CreateCell(7);
  894. cell11.SetCellValue("结\n构\n占\n比\n");
  895. cell11.CellStyle = cellStylebt;
  896. cell11.CellStyle.WrapText = true;
  897. if (istrue)
  898. {
  899. if (a > 1)
  900. sheet.AddMergedRegion(new CellRangeAddress(structure + 1, structure+ productcode+a/2, 7, 7));
  901. else
  902. sheet.AddMergedRegion(new CellRangeAddress(structure + 1, structure + productcode+a, 7, 7));
  903. }
  904. else
  905. sheet.AddMergedRegion(new CellRangeAddress(structure + 1, structure + productcode, 7, 7));
  906. if (Factory1 != null)
  907. {
  908. for (int i = 0; i < count; i++)
  909. {
  910. if (i ==0 || t + i== structure)
  911. {
  912. if (Factory1.Count > i*2)
  913. {
  914. for (int j = 0; j < 6; j++)
  915. {
  916. string msg = ReturnMsg(j, i, Factory1);
  917. if (i == 0)
  918. {
  919. ICell cell8 = irow5.CreateCell(j + 1);
  920. cell8.SetCellValue(msg);
  921. cell8.CellStyle = cellStylebt;
  922. }
  923. else
  924. {
  925. ICell cell9 = irow7.CreateCell(j + 1);
  926. cell9.SetCellValue(msg);
  927. cell9.CellStyle = cellStylebt;
  928. }
  929. }
  930. }
  931. if (i ==0)
  932. {
  933. for (int z = 0; z < 3; z++)
  934. {
  935. switch (z)
  936. {
  937. case 0:
  938. ICell cell12 = irow5.CreateCell(8);
  939. cell12.SetCellValue(Factory2[0].name );
  940. cell12.CellStyle = cellStylebt;
  941. sheet.AddMergedRegion(new CellRangeAddress(t+1, t+1 , 8, 9));
  942. ICell cell22 = irow5.CreateCell(9);
  943. cell22.CellStyle = cellStylebt;
  944. break;
  945. case 1:
  946. ICell cell13 = irow5.CreateCell(10);
  947. cell13.SetCellValue(Factory2[0].number .ToString ());
  948. cell13.CellStyle = cellStylebt;
  949. break;
  950. case 2:
  951. ICell cell14 = irow5.CreateCell(11);
  952. cell14.SetCellValue(Factory2[0].proportion);
  953. cell14.CellStyle = cellStylebt;
  954. break;
  955. }
  956. }
  957. }
  958. else
  959. {
  960. ICell cell119 = irow7.CreateCell(0);
  961. cell119.CellStyle = cellStylebt;
  962. for (int z = 0; z < 3; z++)
  963. {
  964. switch (z)
  965. {
  966. case 0:
  967. ICell cell15 = irow7.CreateCell(8);
  968. cell15.SetCellValue(Factory3[0].name);
  969. cell15.CellStyle = cellStylebt;
  970. sheet.AddMergedRegion(new CellRangeAddress(structure+1, structure+1, 8, 9));
  971. ICell cell31 = irow7.CreateCell(9);
  972. cell15.CellStyle = cellStylebt;
  973. break;
  974. case 1:
  975. ICell cell16 = irow7.CreateCell(10);
  976. cell16.SetCellValue(Factory3[0].number.ToString());
  977. cell16.CellStyle = cellStylebt;
  978. break;
  979. case 2:
  980. ICell cell17= irow7.CreateCell(11);
  981. cell17.SetCellValue(Factory3[0].proportion);
  982. cell17.CellStyle = cellStylebt;
  983. break;
  984. }
  985. }
  986. }
  987. }
  988. else
  989. {
  990. IRow irow6 = sheet.CreateRow(t+1 + i);
  991. if (i*2 < Factory1.Count)
  992. {
  993. for (int j = 0; j < 6; j++)
  994. {
  995. string msg = ReturnMsg(j, i, Factory1);
  996. ICell cell18 = irow6.CreateCell(j + 1);
  997. cell18.SetCellValue(msg);
  998. cell18.CellStyle = cellStylebt;
  999. }
  1000. }
  1001. int b = 0;
  1002. if (a >1)
  1003. {
  1004. b= (a / 2) + (0 == a % 2 ? 0 : 1);
  1005. }
  1006. if (i < Factory2.Count -1)
  1007. {
  1008. for (int z = 0; z < 3; z++)
  1009. {
  1010. switch (z)
  1011. {
  1012. case 0:
  1013. ICell cell8 = irow6.CreateCell(8);
  1014. cell8.SetCellValue(Factory2[i].name);
  1015. cell8.CellStyle = cellStylebt;
  1016. sheet.AddMergedRegion(new CellRangeAddress(t+1 + i, t+1 + i, 8, 9));
  1017. ICell cell22 = irow6.CreateCell(9);
  1018. cell22.CellStyle = cellStylebt;
  1019. break;
  1020. case 1:
  1021. ICell cell9 = irow6.CreateCell(10);
  1022. cell9.SetCellValue(Factory2[i].number.ToString());
  1023. cell9.CellStyle = cellStylebt;
  1024. break;
  1025. case 2:
  1026. ICell cell12 = irow6.CreateCell(11);
  1027. cell12.SetCellValue(Factory2[i].proportion);
  1028. cell12.CellStyle = cellStylebt;
  1029. break;
  1030. }
  1031. }
  1032. }
  1033. else if (i == Factory2.Count-1 )
  1034. {
  1035. for (int z = 0; z < 3; z++)
  1036. {
  1037. string msg = "";
  1038. switch (z)
  1039. {
  1040. case 0:
  1041. ICell cell8 = irow6.CreateCell(8);
  1042. cell8.SetCellValue(Factory2[i].name);
  1043. cell8.CellStyle = cellStylebt;
  1044. sheet.AddMergedRegion(new CellRangeAddress(t +1+ i, t+1 + i+b , 8, 9));
  1045. break;
  1046. case 1:
  1047. ICell cell9 = irow6.CreateCell(10);
  1048. cell9.SetCellValue(Factory2[i].number.ToString());
  1049. cell9.CellStyle = cellStylebt;
  1050. sheet.AddMergedRegion(new CellRangeAddress(t+1 + i, t+1 + i + b, 10, 10));
  1051. break;
  1052. case 2:
  1053. ICell cell12 = irow6.CreateCell(11);
  1054. cell12.SetCellValue(Factory2[i].proportion);
  1055. cell12.CellStyle = cellStylebt;
  1056. sheet.AddMergedRegion(new CellRangeAddress(t+1 + i, t+1 + i + b, 11, 11));
  1057. break;
  1058. }
  1059. }
  1060. }
  1061. if (i <( Factory3.Count + Factory2.Count+b -1)&& i>= Factory2.Count + b)
  1062. {
  1063. for (int z = 0; z < 3; z++)
  1064. {
  1065. switch (z)
  1066. {
  1067. case 0:
  1068. ICell cell8 = irow6.CreateCell(8);
  1069. cell8.SetCellValue(Factory3[i- Factory2.Count -b ].name);
  1070. cell8.CellStyle = cellStylebt;
  1071. sheet.AddMergedRegion(new CellRangeAddress(t +1+ i, t +1+ i, 8, 9));
  1072. ICell cel27 = irow6.CreateCell(9);
  1073. cel27.CellStyle = cellStylebt;
  1074. break;
  1075. case 1:
  1076. ICell cell9 = irow6.CreateCell(10);
  1077. cell9.SetCellValue(Factory3[i - Factory2.Count - b ].number.ToString());
  1078. cell9.CellStyle = cellStylebt;
  1079. break;
  1080. case 2:
  1081. ICell cell12 = irow6.CreateCell(11);
  1082. cell12.SetCellValue(Factory3[i - Factory2.Count - b ].proportion);
  1083. cell12.CellStyle = cellStylebt;
  1084. break;
  1085. }
  1086. }
  1087. }
  1088. else if ( i== Factory2.Count + b+ Factory3.Count-1 )
  1089. {
  1090. for (int z = 0; z < 3; z++)
  1091. {
  1092. switch (z)
  1093. {
  1094. case 0:
  1095. ICell cell8 = irow6.CreateCell(8);
  1096. cell8.SetCellValue(Factory3[i - Factory2.Count - b].name);
  1097. cell8.CellStyle = cellStylebt;
  1098. sheet.AddMergedRegion(new CellRangeAddress(t +1+ i, t+1 + i + b, 8, 9));
  1099. break;
  1100. case 1:
  1101. ICell cell9 = irow6.CreateCell(10);
  1102. cell9.SetCellValue(Factory3[i - Factory2.Count - b].number.ToString());
  1103. cell9.CellStyle = cellStylebt;
  1104. sheet.AddMergedRegion(new CellRangeAddress(t + 1 + i, t + 1 + i + b, 10, 10));
  1105. break;
  1106. case 2:
  1107. ICell cell12 = irow6.CreateCell(11);
  1108. cell12.SetCellValue(Factory3[i - Factory2.Count - b].proportion);
  1109. cell12.CellStyle = cellStylebt;
  1110. sheet.AddMergedRegion(new CellRangeAddress(t + 1 + i, t + 1 + i + b, 11, 11));
  1111. break;
  1112. }
  1113. }
  1114. }
  1115. if ( i == Factory2.Count)
  1116. {
  1117. ICell cell120 = irow6.CreateCell(7);
  1118. cell120.CellStyle = cellStylebt;
  1119. ICell cell124 = irow6.CreateCell(9);
  1120. cell124.CellStyle = cellStylebt;
  1121. ICell cell125 = irow6.CreateCell(10);
  1122. cell125.CellStyle = cellStylebt;
  1123. ICell cell126 = irow6.CreateCell(11);
  1124. cell126.CellStyle = cellStylebt;
  1125. }
  1126. if (!istrue )
  1127. {
  1128. if (i * 2 >= Factory1.Count)
  1129. {
  1130. ICell cell119 = irow6.CreateCell(0);
  1131. cell119.CellStyle = cellStylebt;
  1132. }
  1133. if (i *2 >= Factory1.Count&i < Factory2.Count)
  1134. {
  1135. ICell cell119 = irow6.CreateCell(7);
  1136. cell119.CellStyle = cellStylebt;
  1137. }
  1138. if (i > Factory2.Count)
  1139. {
  1140. ICell cell119 = irow6.CreateCell(7);
  1141. cell119.CellStyle = cellStylebt;
  1142. }
  1143. if (i == count - 1)
  1144. {
  1145. ICell cell119 = irow6.CreateCell(1);
  1146. cell119.CellStyle = cellStylebt1;
  1147. ICell cell121 = irow6.CreateCell(2);
  1148. cell121.CellStyle = cellStylebt1;
  1149. ICell cell132 = irow6.CreateCell(3);
  1150. cell132.CellStyle = cellStylebt1;
  1151. ICell cell128 = irow6.CreateCell(4);
  1152. cell128.CellStyle = cellStylebt1;
  1153. ICell cell129 = irow6.CreateCell(5);
  1154. cell129.CellStyle = cellStylebt1;
  1155. ICell cell130 = irow6.CreateCell(6);
  1156. cell130.CellStyle = cellStylebt1;
  1157. ICell cell131 = irow6.CreateCell(9);
  1158. cell131.CellStyle = cellStylebt1;
  1159. }
  1160. }
  1161. else if (i == count - 1)
  1162. {
  1163. ICell cell119 = irow6.CreateCell(0);
  1164. cell119.CellStyle = cellStylebt;
  1165. ICell cell121 = irow6.CreateCell(7);
  1166. cell121.CellStyle = cellStylebt;
  1167. ICell cell132 = irow6.CreateCell(8);
  1168. cell132.CellStyle = cellStylebt;
  1169. ICell cell128 = irow6.CreateCell(9);
  1170. cell128.CellStyle = cellStylebt;
  1171. ICell cell129 = irow6.CreateCell(10);
  1172. cell129.CellStyle = cellStylebt;
  1173. ICell cell130 = irow6.CreateCell(11);
  1174. cell130.CellStyle = cellStylebt;
  1175. }
  1176. }
  1177. }
  1178. }
  1179. #endregion
  1180. //自适应列宽度
  1181. for (int i = 0; i < 12; i++)
  1182. {
  1183. // sheet.AutoSizeColumn(i);
  1184. sheet.SetColumnWidth(i, 15 * 256);
  1185. }
  1186. using (MemoryStream ms = new MemoryStream())
  1187. {
  1188. workbook.Write(ms);
  1189. HttpContext curContext = HttpContext.Current;
  1190. // 设置编码和附件格式
  1191. curContext.Response.ContentType = "application/vnd.ms-excel";
  1192. curContext.Response.ContentEncoding = Encoding.UTF8;
  1193. curContext.Response.Charset = "";
  1194. curContext.Response.AppendHeader("Content-Disposition",
  1195. "attachment;filename=" + HttpUtility.UrlEncode("投诉产品日期分布表" + ".xls", Encoding.UTF8));
  1196. curContext.Response.BinaryWrite(ms.GetBuffer());
  1197. workbook = null;
  1198. ms.Close();
  1199. ms.Dispose();
  1200. curContext.Response.End();
  1201. }
  1202. return "";
  1203. }
  1204. catch
  1205. {
  1206. return "导出失败!";
  1207. }
  1208. }
  1209. //2017-11-11数据第一行为标题
  1210. #region
  1211. /// <summary>
  1212. /// 弹出下载框导出excel(数据第一行为标题)
  1213. /// </summary>
  1214. /// <param name="Name"></param>
  1215. /// <param name="dt"></param>
  1216. /// <returns></returns>
  1217. public string ExportToExcel2(string Name, DataTable dt, string[] cols = null)
  1218. {
  1219. try
  1220. {
  1221. //if (dt.Rows.Count > 0)
  1222. //{
  1223. HSSFWorkbook workbook = new HSSFWorkbook();
  1224. ISheet sheet = workbook.CreateSheet("Sheet1");
  1225. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  1226. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1227. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1228. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1229. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1230. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  1231. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  1232. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  1233. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  1234. //字体
  1235. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  1236. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  1237. headerfont.FontHeightInPoints = 12;
  1238. HeadercellStyle.SetFont(headerfont);
  1239. //用column name 作为列名
  1240. int icolIndex = 0;
  1241. IRow headerRow = sheet.CreateRow(0);
  1242. if (cols == null || (cols != null && cols.Length == 0))
  1243. {
  1244. //foreach (DataColumn dc in dt.Columns)
  1245. //{
  1246. // ICell cell = headerRow.CreateCell(icolIndex);
  1247. // cell.SetCellValue(dc.ColumnName);
  1248. // cell.CellStyle = HeadercellStyle;
  1249. // icolIndex++;
  1250. //}
  1251. for (int i = 0; i < dt.Columns.Count; i++)
  1252. {
  1253. ICell cell = headerRow.CreateCell(icolIndex);
  1254. cell.SetCellValue(dt.Rows[0][i].ToString());
  1255. cell.CellStyle = HeadercellStyle;
  1256. icolIndex++;
  1257. }
  1258. }
  1259. else
  1260. {
  1261. foreach (string dc in cols)
  1262. {
  1263. ICell cell = headerRow.CreateCell(icolIndex);
  1264. cell.SetCellValue(dc);
  1265. cell.CellStyle = HeadercellStyle;
  1266. icolIndex++;
  1267. }
  1268. }
  1269. ICellStyle cellStyle = workbook.CreateCellStyle();
  1270. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  1271. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  1272. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1273. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1274. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1275. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1276. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  1277. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  1278. cellStyle.SetFont(cellfont);
  1279. //建立内容行
  1280. int iRowIndex = 0;
  1281. foreach (DataRow dr in dt.Rows)
  1282. {
  1283. if (iRowIndex > 0)
  1284. {
  1285. int iCellIndex = 0;
  1286. IRow irow = sheet.CreateRow(iRowIndex);
  1287. for (int i = 0; i < dt.Columns.Count; i++)
  1288. {
  1289. string strsj = string.Empty;
  1290. if (dr[i] != null)
  1291. {
  1292. strsj = dr[i].ToString();
  1293. }
  1294. ICell cell = irow.CreateCell(iCellIndex);
  1295. cell.SetCellValue(strsj);
  1296. cell.CellStyle = cellStyle;
  1297. iCellIndex++;
  1298. }
  1299. }
  1300. iRowIndex++;
  1301. }
  1302. //自适应列宽度
  1303. for (int i = 0; i < icolIndex; i++)
  1304. {
  1305. sheet.AutoSizeColumn(i);
  1306. }
  1307. using (MemoryStream ms = new MemoryStream())
  1308. {
  1309. workbook.Write(ms);
  1310. HttpContext curContext = HttpContext.Current;
  1311. // 设置编码和附件格式
  1312. curContext.Response.ContentType = "application/vnd.ms-excel";
  1313. curContext.Response.ContentEncoding = Encoding.UTF8;
  1314. curContext.Response.Charset = "";
  1315. curContext.Response.AppendHeader("Content-Disposition",
  1316. "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
  1317. curContext.Response.BinaryWrite(ms.GetBuffer());
  1318. workbook = null;
  1319. ms.Close();
  1320. ms.Dispose();
  1321. curContext.Response.End();
  1322. }
  1323. //}
  1324. return "";
  1325. }
  1326. catch
  1327. {
  1328. return "导出失败!";
  1329. }
  1330. }
  1331. #endregion
  1332. }
  1333. }