Няма описание

NPOIHelper.cs 56KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  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. IRow irow2 = sheet.CreateRow(1);
  712. ICell cell2 = irow2.CreateCell(0);
  713. ICellStyle style13 = workbook.CreateCellStyle();
  714. style13.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  715. style13.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  716. style13.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  717. style13.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  718. style13.BorderDiagonalLineStyle = BorderStyle.Thin;
  719. style13.BorderDiagonal = BorderDiagonal.Backward;
  720. style13.BorderDiagonalColor = IndexedColors.Black .Index;
  721. string sb = " 数量\n日期";
  722. // StringBuilder sb = new StringBuilder();
  723. // sb.AppendLine("数量".PadLeft(15));//该行不足长度10在左侧填空格
  724. // sb.AppendLine("日期".PadRight(15));//该行不足长度10在右侧填空格
  725. cell2.SetCellValue(sb);
  726. cell2.CellStyle = cellStylebt;
  727. style13.WrapText = true;
  728. irow2.GetCell(0).CellStyle = style13;
  729. sheet.AddMergedRegion(new CellRangeAddress(1, 2, 0,0 ));
  730. ICell cell6 = irow2.CreateCell(1);
  731. cell6.SetCellValue("投诉产品");
  732. cell6.CellStyle = cellStylebt;
  733. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 1, 1));
  734. for (int i=0;i<5;i ++)
  735. {
  736. string msg = "";
  737. switch (i)
  738. {
  739. case 0:
  740. msg = datetime.AddMonths(-11).Month + "-" + datetime.AddMonths(-3).Month + "月份日期";
  741. break;
  742. case 1:
  743. msg = datetime.AddMonths(-2).Month + "月份日期";
  744. break;
  745. case 2:
  746. msg = datetime.AddMonths(-1).Month + "月份日期";
  747. break;
  748. case 3:
  749. msg = datetime.Month + "月份日期";
  750. break;
  751. case 4:
  752. msg = "不清楚日期";
  753. break;
  754. }
  755. ICell cell3 = irow2.CreateCell(i *2+ 2 );
  756. cell3.SetCellValue(msg);
  757. cell3.CellStyle = cellStylebt;
  758. ICell cell4 = irow2.CreateCell(i * 2 + 3);
  759. cell4.CellStyle = cellStylebt;
  760. sheet.AddMergedRegion(new CellRangeAddress(1, 1, i * 2 + 2, i * 2 + 3));
  761. }
  762. IRow irow3 = sheet.CreateRow(2);
  763. for (int i = 0; i < 11; i++)
  764. {
  765. string msg = "";
  766. if (i ==0)
  767. {
  768. msg = "数量";
  769. }
  770. else
  771. {
  772. if (i % 2 == 0)
  773. {
  774. msg = "占比";
  775. }
  776. else
  777. {
  778. msg = "数量";
  779. }
  780. }
  781. ICell cell4 = irow3.CreateCell(i+1);
  782. cell4.SetCellValue(msg);
  783. cell4.CellStyle = cellStylebt;
  784. }
  785. List<Date> dt1 = product.dates;
  786. if (dt1!=null )
  787. {
  788. for (int i=0;i < dt1.Count;i ++)
  789. {
  790. IRow irow4 = sheet.CreateRow(3 + i);
  791. string msg = "";
  792. for (int j = 0; j < 12; j ++)
  793. {
  794. switch (j )
  795. {
  796. case 0:
  797. msg = dt1[i].name;
  798. break;
  799. case 1:
  800. msg = dt1[i].total.ToString();
  801. break;
  802. case 2:
  803. msg = dt1[i].MonthCount1 .ToString ();
  804. break;
  805. case 3:
  806. msg = dt1[i].MonthCountmix1 .ToString();
  807. break;
  808. case 4:
  809. msg = dt1[i].MonthCount2.ToString();
  810. break;
  811. case 5:
  812. msg = dt1[i].MonthCountmix2.ToString();
  813. break;
  814. case 6:
  815. msg = dt1[i].MonthCount3.ToString();
  816. break;
  817. case 7:
  818. msg = dt1[i].MonthCountmix3.ToString();
  819. break;
  820. case 8:
  821. msg = dt1[i].MonthCount4.ToString();
  822. break;
  823. case 9:
  824. msg = dt1[i].MonthCountmix4.ToString();
  825. break;
  826. case 10:
  827. msg = dt1[i].MonthCount5.ToString();
  828. break;
  829. case 11:
  830. msg = dt1[i].MonthCountmix5.ToString();
  831. break;
  832. }
  833. ICell cell5 = irow4.CreateCell(j);
  834. cell5.SetCellValue(msg);
  835. cell5.CellStyle = cellStylebt;
  836. }
  837. }
  838. }
  839. int t = dt1.Count + 2;int count = 0;
  840. List<Factory> Factory1 = product.factory;
  841. List<Factory> Factory2 = product.problem;
  842. List<Factory> Factory3 = product.product;
  843. int factory = 0, problem = 0, productcode = 0;
  844. if (Factory1 != null)
  845. factory = (Factory1.Count / 2) + (0 == Factory1.Count % 2 ? 0 : 1);
  846. if (Factory2 != null)
  847. problem = Factory2.Count;
  848. if (Factory3 != null)
  849. productcode = Factory3.Count;
  850. bool istrue = true; int a = 0, total = problem + productcode;
  851. if (factory>(total))
  852. {
  853. count = factory;
  854. a = factory - total;
  855. }
  856. else
  857. {
  858. istrue = false;
  859. count = total;
  860. }
  861. IRow irow5 = sheet.CreateRow(t+1);
  862. ICell cell7 = irow5.CreateCell(0);
  863. cell7.SetCellValue("工\n厂\n投\n诉\n占\n比\n");
  864. cell7.CellStyle = cellStylebt;
  865. cell7.CellStyle.WrapText=true ;
  866. sheet.AddMergedRegion(new CellRangeAddress(t+1, t+ count, 0, 0));
  867. ICell cell10 = irow5.CreateCell(7);
  868. cell10.SetCellValue("各\n质\n量\n问\n题\n占\n比\n");
  869. cell10.CellStyle = cellStylebt;
  870. cell10.CellStyle.WrapText = true;
  871. int structure = t + problem;
  872. if (istrue)
  873. {
  874. if (a >1)
  875. {
  876. structure = t + problem + a / 2;
  877. sheet.AddMergedRegion(new CellRangeAddress(t+1, t + problem + a / 2, 7, 7));
  878. }
  879. else
  880. {
  881. structure = t + problem;
  882. sheet.AddMergedRegion(new CellRangeAddress(t+1, t + problem, 7, 7));
  883. }
  884. }
  885. else
  886. sheet.AddMergedRegion(new CellRangeAddress(t+1, t + problem, 7, 7));
  887. IRow irow7 = sheet.CreateRow(structure+1);
  888. ICell cell11 = irow7.CreateCell(7);
  889. cell11.SetCellValue("结\n构\n占\n比\n");
  890. cell11.CellStyle = cellStylebt;
  891. cell11.CellStyle.WrapText = true;
  892. if (istrue)
  893. {
  894. if (a > 1)
  895. sheet.AddMergedRegion(new CellRangeAddress(structure + 1, structure+ productcode+a/2, 7, 7));
  896. else
  897. sheet.AddMergedRegion(new CellRangeAddress(structure + 1, structure + productcode+a, 7, 7));
  898. }
  899. else
  900. sheet.AddMergedRegion(new CellRangeAddress(structure + 1, structure + productcode, 7, 7));
  901. if (Factory1 != null)
  902. {
  903. for (int i = 0; i < count; i++)
  904. {
  905. if (i ==0 || t + i== structure)
  906. {
  907. for (int j = 0; j < 6; j++)
  908. {
  909. string msg = ReturnMsg(j ,i ,Factory1 );
  910. if (i == 0)
  911. {
  912. ICell cell8 = irow5.CreateCell(j + 1);
  913. cell8.SetCellValue(msg);
  914. cell8.CellStyle = cellStylebt;
  915. }
  916. else
  917. {
  918. ICell cell9 = irow7.CreateCell(j + 1);
  919. cell9.SetCellValue(msg);
  920. cell9.CellStyle = cellStylebt;
  921. }
  922. }
  923. if (i ==0)
  924. {
  925. for (int z = 0; z < 3; z++)
  926. {
  927. switch (z)
  928. {
  929. case 0:
  930. ICell cell12 = irow5.CreateCell(8);
  931. cell12.SetCellValue(Factory2[0].name );
  932. cell12.CellStyle = cellStylebt;
  933. sheet.AddMergedRegion(new CellRangeAddress(t+1, t+1 , 8, 9));
  934. ICell cell22 = irow5.CreateCell(9);
  935. cell22.CellStyle = cellStylebt;
  936. break;
  937. case 1:
  938. ICell cell13 = irow5.CreateCell(10);
  939. cell13.SetCellValue(Factory2[0].number .ToString ());
  940. cell13.CellStyle = cellStylebt;
  941. break;
  942. case 2:
  943. ICell cell14 = irow5.CreateCell(11);
  944. cell14.SetCellValue(Factory2[0].proportion);
  945. cell14.CellStyle = cellStylebt;
  946. break;
  947. }
  948. }
  949. }
  950. else
  951. {
  952. for (int z = 0; z < 3; z++)
  953. {
  954. switch (z)
  955. {
  956. case 0:
  957. ICell cell15 = irow7.CreateCell(8);
  958. cell15.SetCellValue(Factory3[0].name);
  959. cell15.CellStyle = cellStylebt;
  960. sheet.AddMergedRegion(new CellRangeAddress(structure+1, structure+1, 8, 9));
  961. ICell cell31 = irow7.CreateCell(9);
  962. cell15.CellStyle = cellStylebt;
  963. break;
  964. case 1:
  965. ICell cell16 = irow7.CreateCell(10);
  966. cell16.SetCellValue(Factory3[0].number.ToString());
  967. cell16.CellStyle = cellStylebt;
  968. break;
  969. case 2:
  970. ICell cell17= irow7.CreateCell(11);
  971. cell17.SetCellValue(Factory3[0].proportion);
  972. cell17.CellStyle = cellStylebt;
  973. break;
  974. }
  975. }
  976. }
  977. }
  978. else
  979. {
  980. IRow irow6 = sheet.CreateRow(t+1 + i);
  981. if (i*2 < Factory1.Count)
  982. {
  983. for (int j = 0; j < 6; j++)
  984. {
  985. string msg = ReturnMsg(j, i, Factory1);
  986. ICell cell18 = irow6.CreateCell(j + 1);
  987. cell18.SetCellValue(msg);
  988. cell18.CellStyle = cellStylebt;
  989. }
  990. }
  991. int b = 0;
  992. if (a >1)
  993. {
  994. b= (a / 2) + (0 == a % 2 ? 0 : 1);
  995. }
  996. if (i < Factory2.Count -1)
  997. {
  998. for (int z = 0; z < 3; z++)
  999. {
  1000. switch (z)
  1001. {
  1002. case 0:
  1003. ICell cell8 = irow6.CreateCell(8);
  1004. cell8.SetCellValue(Factory2[i].name);
  1005. cell8.CellStyle = cellStylebt;
  1006. sheet.AddMergedRegion(new CellRangeAddress(t+1 + i, t+1 + i, 8, 9));
  1007. ICell cell22 = irow6.CreateCell(9);
  1008. cell22.CellStyle = cellStylebt;
  1009. break;
  1010. case 1:
  1011. ICell cell9 = irow6.CreateCell(10);
  1012. cell9.SetCellValue(Factory2[i].number.ToString());
  1013. cell9.CellStyle = cellStylebt;
  1014. break;
  1015. case 2:
  1016. ICell cell12 = irow6.CreateCell(11);
  1017. cell12.SetCellValue(Factory2[i].proportion);
  1018. cell12.CellStyle = cellStylebt;
  1019. break;
  1020. }
  1021. }
  1022. }
  1023. else if (i == Factory2.Count-1 )
  1024. {
  1025. for (int z = 0; z < 3; z++)
  1026. {
  1027. string msg = "";
  1028. switch (z)
  1029. {
  1030. case 0:
  1031. ICell cell8 = irow6.CreateCell(8);
  1032. cell8.SetCellValue(Factory2[i].name);
  1033. cell8.CellStyle = cellStylebt;
  1034. sheet.AddMergedRegion(new CellRangeAddress(t +1+ i, t+1 + i+b , 8, 9));
  1035. break;
  1036. case 1:
  1037. ICell cell9 = irow6.CreateCell(10);
  1038. cell9.SetCellValue(Factory2[i].number.ToString());
  1039. cell9.CellStyle = cellStylebt;
  1040. sheet.AddMergedRegion(new CellRangeAddress(t+1 + i, t+1 + i + b, 10, 10));
  1041. break;
  1042. case 2:
  1043. ICell cell12 = irow6.CreateCell(11);
  1044. cell12.SetCellValue(Factory2[i].proportion);
  1045. cell12.CellStyle = cellStylebt;
  1046. sheet.AddMergedRegion(new CellRangeAddress(t+1 + i, t+1 + i + b, 11, 11));
  1047. break;
  1048. }
  1049. }
  1050. }
  1051. if (i <( Factory3.Count + Factory2.Count+b -1)&& i>= Factory2.Count + b)
  1052. {
  1053. for (int z = 0; z < 3; z++)
  1054. {
  1055. switch (z)
  1056. {
  1057. case 0:
  1058. ICell cell8 = irow6.CreateCell(8);
  1059. cell8.SetCellValue(Factory3[i- Factory2.Count -b ].name);
  1060. cell8.CellStyle = cellStylebt;
  1061. sheet.AddMergedRegion(new CellRangeAddress(t +1+ i, t +1+ i, 8, 9));
  1062. ICell cel27 = irow6.CreateCell(9);
  1063. cel27.CellStyle = cellStylebt;
  1064. break;
  1065. case 1:
  1066. ICell cell9 = irow6.CreateCell(10);
  1067. cell9.SetCellValue(Factory3[i - Factory2.Count - b ].number.ToString());
  1068. cell9.CellStyle = cellStylebt;
  1069. break;
  1070. case 2:
  1071. ICell cell12 = irow6.CreateCell(11);
  1072. cell12.SetCellValue(Factory3[i - Factory2.Count - b ].proportion);
  1073. cell12.CellStyle = cellStylebt;
  1074. break;
  1075. }
  1076. }
  1077. }
  1078. else if ( i== Factory2.Count + b+ Factory3.Count-1 )
  1079. {
  1080. for (int z = 0; z < 3; z++)
  1081. {
  1082. switch (z)
  1083. {
  1084. case 0:
  1085. ICell cell8 = irow6.CreateCell(8);
  1086. cell8.SetCellValue(Factory3[i - Factory2.Count - b].name);
  1087. cell8.CellStyle = cellStylebt;
  1088. sheet.AddMergedRegion(new CellRangeAddress(t +1+ i, t+1 + i + b, 8, 9));
  1089. break;
  1090. case 1:
  1091. ICell cell9 = irow6.CreateCell(10);
  1092. cell9.SetCellValue(Factory3[i - Factory2.Count - b].number.ToString());
  1093. cell9.CellStyle = cellStylebt;
  1094. sheet.AddMergedRegion(new CellRangeAddress(t + 1 + i, t + 1 + i + b, 10, 10));
  1095. break;
  1096. case 2:
  1097. ICell cell12 = irow6.CreateCell(11);
  1098. cell12.SetCellValue(Factory3[i - Factory2.Count - b].proportion);
  1099. cell12.CellStyle = cellStylebt;
  1100. sheet.AddMergedRegion(new CellRangeAddress(t + 1 + i, t + 1 + i + b, 11, 11));
  1101. break;
  1102. }
  1103. }
  1104. }
  1105. if ( i == Factory2.Count)
  1106. {
  1107. ICell cell120 = irow6.CreateCell(7);
  1108. cell120.CellStyle = cellStylebt;
  1109. ICell cell124 = irow6.CreateCell(9);
  1110. cell124.CellStyle = cellStylebt;
  1111. ICell cell125 = irow6.CreateCell(10);
  1112. cell125.CellStyle = cellStylebt;
  1113. ICell cell126 = irow6.CreateCell(11);
  1114. cell126.CellStyle = cellStylebt;
  1115. }
  1116. if (i ==count -1)
  1117. {
  1118. ICell cell119 = irow6.CreateCell(0);
  1119. cell119.CellStyle = cellStylebt;
  1120. ICell cell121 = irow6.CreateCell(7);
  1121. cell121.CellStyle = cellStylebt;
  1122. ICell cell132 = irow6.CreateCell(8);
  1123. cell132.CellStyle = cellStylebt;
  1124. ICell cell128 = irow6.CreateCell(9);
  1125. cell128.CellStyle = cellStylebt;
  1126. ICell cell129 = irow6.CreateCell(10);
  1127. cell129.CellStyle = cellStylebt;
  1128. ICell cell130 = irow6.CreateCell(11);
  1129. cell130.CellStyle = cellStylebt;
  1130. }
  1131. }
  1132. }
  1133. }
  1134. #endregion
  1135. //自适应列宽度
  1136. for (int i = 0; i < 12; i++)
  1137. {
  1138. // sheet.AutoSizeColumn(i);
  1139. sheet.SetColumnWidth(i, 15 * 256);
  1140. }
  1141. using (MemoryStream ms = new MemoryStream())
  1142. {
  1143. workbook.Write(ms);
  1144. HttpContext curContext = HttpContext.Current;
  1145. // 设置编码和附件格式
  1146. curContext.Response.ContentType = "application/vnd.ms-excel";
  1147. curContext.Response.ContentEncoding = Encoding.UTF8;
  1148. curContext.Response.Charset = "";
  1149. curContext.Response.AppendHeader("Content-Disposition",
  1150. "attachment;filename=" + HttpUtility.UrlEncode("投诉产品日期分布表" + ".xls", Encoding.UTF8));
  1151. curContext.Response.BinaryWrite(ms.GetBuffer());
  1152. workbook = null;
  1153. ms.Close();
  1154. ms.Dispose();
  1155. curContext.Response.End();
  1156. }
  1157. return "";
  1158. }
  1159. catch
  1160. {
  1161. return "导出失败!";
  1162. }
  1163. }
  1164. }
  1165. }