Sin descripción

NPOIHelper.cs 55KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  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 = cellStyle;
  492. ICell cell102 = irow3list[td].CreateCell(3 * dt - 2);
  493. cell102.SetCellValue(it .number );
  494. cell102.CellStyle = cellStyle;
  495. ICell cell103= irow3list[td].CreateCell(3 * dt-1 );
  496. cell103.SetCellValue(it.Proportion );
  497. cell103.CellStyle = cellStyle;
  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 = cellStyle;
  506. ICell cell102 = irow3list[td].CreateCell(3 * dt - 2);
  507. cell102.SetCellValue(it.number);
  508. cell102.CellStyle = cellStyle;
  509. ICell cell103 = irow3list[td].CreateCell(3 * dt-1);
  510. cell103.SetCellValue(it.Proportion);
  511. cell103.CellStyle = cellStyle;
  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 = cellStyle;
  531. ICell cell52 = irow5.CreateCell(1);
  532. cell52.SetCellValue(dt2.number);
  533. cell52.CellStyle = cellStyle;
  534. ICell cell53 = irow5.CreateCell(2);
  535. cell53.SetCellValue(dt2.Proportion);
  536. cell53.CellStyle = cellStyle;
  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 = cellStyle;
  554. ICell cell54 = irow10.CreateCell(1);
  555. cell54.SetCellValue(dt3.number);
  556. cell54.CellStyle = cellStyle;
  557. ICell cell55 = irow10.CreateCell(2);
  558. cell55.SetCellValue(dt3.Proportion);
  559. cell55.CellStyle = cellStyle;
  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 = cellStyle;
  577. ICell cell56 = irow12.CreateCell(1);
  578. cell56.SetCellValue(dt4.number);
  579. cell56.CellStyle = cellStyle;
  580. ICell cell57 = irow12.CreateCell(2);
  581. cell57.SetCellValue(dt4.Proportion);
  582. cell57.CellStyle = cellStyle;
  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 = cellStyle;
  600. ICell cell58 = irow15.CreateCell(1);
  601. cell58.SetCellValue(dt4.number);
  602. cell58.CellStyle = cellStyle;
  603. ICell cell59 = irow15.CreateCell(2);
  604. cell59.SetCellValue(dt5.Proportion);
  605. cell59.CellStyle = cellStyle;
  606. }
  607. #endregion
  608. //自适应列宽度
  609. for (int i = 0; i < 8; i++)
  610. {
  611. sheet.AutoSizeColumn(i);
  612. }
  613. using (MemoryStream ms = new MemoryStream())
  614. {
  615. workbook.Write(ms);
  616. HttpContext curContext = HttpContext.Current;
  617. // 设置编码和附件格式
  618. curContext.Response.ContentType = "application/vnd.ms-excel";
  619. curContext.Response.ContentEncoding = Encoding.UTF8;
  620. curContext.Response.Charset = "";
  621. curContext.Response.AppendHeader("Content-Disposition",
  622. "attachment;filename=" + HttpUtility.UrlEncode("市场信息简报" + ".xls", Encoding.UTF8));
  623. curContext.Response.BinaryWrite(ms.GetBuffer());
  624. workbook = null;
  625. ms.Close();
  626. ms.Dispose();
  627. curContext.Response.End();
  628. }
  629. return "";
  630. }
  631. catch
  632. {
  633. return "导出失败!";
  634. }
  635. }
  636. private void Columnwidth(int number, ISheet ffSheet)
  637. {
  638. for (int columnNum = 0; columnNum <= number; columnNum++)
  639. {
  640. int columnWidth = ffSheet.GetColumnWidth(columnNum) / 256;//获取当前列宽度
  641. for (int rowNum = 1; rowNum <= ffSheet.LastRowNum; rowNum++)//在这一列上循环行
  642. {
  643. IRow currentRow = ffSheet.GetRow(rowNum);
  644. ICell currentCell = currentRow.GetCell(columnNum);
  645. int length = Encoding.UTF8.GetBytes(currentCell.ToString()).Length;//获取当前单元格的内容宽度
  646. if (columnWidth < length + 1)
  647. {
  648. columnWidth = length + 1;
  649. }//若当前单元格内容宽度大于列宽,则调整列宽为当前单元格宽度,后面的+1是我人为的将宽度增加一个字符
  650. }
  651. }
  652. }
  653. /// <summary>
  654. /// 投诉产品日期分布表
  655. /// </summary>
  656. /// <param name="ds"></param>
  657. /// <returns></returns>
  658. public string DistributionToExcel(DateTime datetime, Product product)
  659. {
  660. try
  661. {
  662. HSSFWorkbook workbook = new HSSFWorkbook();
  663. ISheet sheet = workbook.CreateSheet("Sheet1");
  664. ICellStyle cellStyle = workbook.CreateCellStyle();
  665. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  666. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  667. cellStyle.SetFont(cellfont);
  668. ICellStyle cellStylebt = workbook.CreateCellStyle();
  669. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  670. cellfontbt.Boldweight = (short)FontBoldWeight.Bold;
  671. cellStylebt.SetFont(cellfontbt);
  672. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  673. cellStylebt.Alignment = HorizontalAlignment.Center;
  674. IRow irow1 = sheet.CreateRow(0);
  675. ICell cell1 = irow1.CreateCell(0);
  676. cell1.SetCellValue(datetime.Year +"年"+datetime .Month +"月份投诉产品日期分布情况表");
  677. cell1.CellStyle = cellStylebt;
  678. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 13));
  679. #region 产品投诉情况
  680. IRow irow2 = sheet.CreateRow(1);
  681. ICell cell2 = irow2.CreateCell(0);
  682. ICellStyle style13 = workbook.CreateCellStyle();
  683. style13.BorderDiagonalLineStyle = BorderStyle.Thin;
  684. style13.BorderDiagonal = BorderDiagonal.Backward;
  685. style13.BorderDiagonalColor = IndexedColors.Black .Index;
  686. StringBuilder sb = new StringBuilder();
  687. sb.AppendLine("数量".PadLeft(15));//该行不足长度10在左侧填空格
  688. sb.AppendLine("日期".PadRight(15));//该行不足长度10在右侧填空格
  689. cell2.SetCellValue(sb.ToString ());
  690. cell2.CellStyle = cellStylebt;
  691. irow2.GetCell(0).CellStyle = style13;
  692. sheet.AddMergedRegion(new CellRangeAddress(1, 2, 0,0 ));
  693. ICell cell6 = irow2.CreateCell(1);
  694. cell6.SetCellValue("投诉产品");
  695. cell6.CellStyle = cellStylebt;
  696. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 1, 1));
  697. for (int i=0;i<5;i ++)
  698. {
  699. string msg = "";
  700. switch (i)
  701. {
  702. case 0:
  703. msg = datetime.AddMonths(-11).Month + "-" + datetime.AddMonths(-4).Month + "月份日期";
  704. break;
  705. case 1:
  706. msg = datetime.AddMonths(-3).Month + "月份日期";
  707. break;
  708. case 2:
  709. msg = datetime.AddMonths(-2).Month + "月份日期";
  710. break;
  711. case 3:
  712. msg = datetime.AddMonths(-1).Month + "月份日期";
  713. break;
  714. case 4:
  715. msg = "不清楚日期";
  716. break;
  717. }
  718. ICell cell3 = irow2.CreateCell(i *2+ 2 );
  719. cell3.SetCellValue(msg);
  720. cell3.CellStyle = cellStylebt;
  721. sheet.AddMergedRegion(new CellRangeAddress(1, 1, i * 2 + 2, i * 2 + 3));
  722. }
  723. IRow irow3 = sheet.CreateRow(2);
  724. for (int i = 0; i < 11; i++)
  725. {
  726. string msg = "";
  727. if (i ==0)
  728. {
  729. msg = "数量";
  730. }
  731. else
  732. {
  733. if (i % 2 == 0)
  734. {
  735. msg = "占比";
  736. }
  737. else
  738. {
  739. msg = "数量";
  740. }
  741. }
  742. ICell cell4 = irow3.CreateCell(i+1);
  743. cell4.SetCellValue(msg);
  744. cell4.CellStyle = cellStylebt;
  745. }
  746. List<Date> dt1 = product.dates;
  747. if (dt1!=null )
  748. {
  749. for (int i=0;i < dt1.Count;i ++)
  750. {
  751. IRow irow4 = sheet.CreateRow(3 + i);
  752. string msg = "";
  753. for (int j = 0; j < 12; j ++)
  754. {
  755. switch (j )
  756. {
  757. case 0:
  758. msg = dt1[i].name;
  759. break;
  760. case 1:
  761. msg = dt1[i].total.ToString();
  762. break;
  763. case 2:
  764. msg = dt1[i].MonthCount1 .ToString ();
  765. break;
  766. case 3:
  767. msg = dt1[i].MonthCountmix1 .ToString();
  768. break;
  769. case 4:
  770. msg = dt1[i].MonthCount2.ToString();
  771. break;
  772. case 5:
  773. msg = dt1[i].MonthCountmix2.ToString();
  774. break;
  775. case 6:
  776. msg = dt1[i].MonthCount3.ToString();
  777. break;
  778. case 7:
  779. msg = dt1[i].MonthCountmix3.ToString();
  780. break;
  781. case 8:
  782. msg = dt1[i].MonthCount4.ToString();
  783. break;
  784. case 9:
  785. msg = dt1[i].MonthCountmix4.ToString();
  786. break;
  787. case 10:
  788. msg = dt1[i].MonthCount5.ToString();
  789. break;
  790. case 11:
  791. msg = dt1[i].MonthCountmix5.ToString();
  792. break;
  793. }
  794. ICell cell5 = irow4.CreateCell(j);
  795. cell5.SetCellValue(msg);
  796. cell5.CellStyle = cellStylebt;
  797. }
  798. }
  799. }
  800. int t = dt1.Count + 3;int count = 0;
  801. List<Factory> Factory1 = product.factory;
  802. List<Factory> Factory2 = product.problem;
  803. List<Factory> Factory3 = product.product;
  804. int factory = 0, problem = 0, productcode = 0;
  805. if (Factory1 != null)
  806. factory = (Factory1.Count / 2) + (0 == Factory1.Count % 2 ? 0 : 1);
  807. if (Factory2 != null)
  808. problem = Factory2.Count;
  809. if (Factory3 != null)
  810. productcode = Factory3.Count;
  811. bool istrue = true; int a = 0, total = problem + productcode;
  812. if (factory>(total))
  813. {
  814. count = factory;
  815. a = factory - total;
  816. }
  817. else
  818. {
  819. istrue = false;
  820. count = total;
  821. }
  822. IRow irow5 = sheet.CreateRow(t);
  823. ICell cell7 = irow5.CreateCell(0);
  824. cell7.SetCellValue("工\n厂\n投\n诉\n占\n比\n");
  825. cell7.CellStyle = cellStylebt;
  826. sheet.AddMergedRegion(new CellRangeAddress(t, t+ count, 0, 0));
  827. ICell cell10 = irow5.CreateCell(7);
  828. cell10.SetCellValue("各\n质\n量\n问\n题\n占\n比\n");
  829. cell10.CellStyle = cellStylebt;
  830. int structure = 0;
  831. if (istrue)
  832. {
  833. if (a >1)
  834. {
  835. structure = t + problem + a / 2;
  836. sheet.AddMergedRegion(new CellRangeAddress(t, t + problem + a / 2, 7, 7));
  837. }
  838. else
  839. {
  840. structure = t + problem;
  841. sheet.AddMergedRegion(new CellRangeAddress(t, t + problem + 1, 7, 7));
  842. }
  843. }
  844. else
  845. sheet.AddMergedRegion(new CellRangeAddress(t, t + problem, 7, 7));
  846. IRow irow7 = sheet.CreateRow(structure);
  847. ICell cell11 = irow7.CreateCell(7);
  848. cell11.SetCellValue("结\n构\n占\n比\n");
  849. cell11.CellStyle = cellStylebt;
  850. if (istrue)
  851. {
  852. if (a > 1)
  853. sheet.AddMergedRegion(new CellRangeAddress(t + problem + a / 2+1, t + total + 1 +a/2, 7, 7));
  854. else
  855. sheet.AddMergedRegion(new CellRangeAddress(t + problem + 2, t + total + 2, 7, 7));
  856. }
  857. else
  858. sheet.AddMergedRegion(new CellRangeAddress(t + problem+1, t + 1 + total, 7, 7));
  859. if (Factory1 != null)
  860. {
  861. for (int i = 0; i < count; i++)
  862. {
  863. if (i ==0 || t + i== structure)
  864. {
  865. if (i < Factory1.Count )
  866. {
  867. for (int j = 0; j < 6; j++)
  868. {
  869. string msg = "";
  870. switch (j)
  871. {
  872. case 0:
  873. msg = Factory1[i * 2].name;
  874. break;
  875. case 1:
  876. msg = Factory1[i * 2].number.ToString();
  877. break;
  878. case 2:
  879. msg = Factory1[i * 2].proportion;
  880. break;
  881. case 3:
  882. msg = Factory1[i * 2 + 1].name;
  883. break;
  884. case 4:
  885. msg = Factory1[i * 2 + 1].number.ToString();
  886. break;
  887. case 5:
  888. msg = Factory1[i * 2 + 1].proportion;
  889. break;
  890. }
  891. if (i == 0)
  892. {
  893. ICell cell8 = irow5.CreateCell(j + 1);
  894. cell8.SetCellValue(msg);
  895. cell8.CellStyle = cellStylebt;
  896. }
  897. else
  898. {
  899. ICell cell8 = irow7.CreateCell(j + 1);
  900. cell8.SetCellValue(msg);
  901. cell8.CellStyle = cellStylebt;
  902. }
  903. }
  904. }
  905. if (i ==0)
  906. {
  907. for (int z = 0; z < 3; z++)
  908. {
  909. string msg = "";
  910. switch (z)
  911. {
  912. case 0:
  913. ICell cell8 = irow5.CreateCell(8);
  914. cell8.SetCellValue(Factory2[0].name );
  915. cell8.CellStyle = cellStylebt;
  916. sheet.AddMergedRegion(new CellRangeAddress(t, t , 8, 9));
  917. break;
  918. case 1:
  919. ICell cell9 = irow5.CreateCell(10);
  920. cell9.SetCellValue(Factory2[0].number .ToString ());
  921. cell9.CellStyle = cellStylebt;
  922. break;
  923. case 2:
  924. ICell cell12 = irow5.CreateCell(11);
  925. cell12.SetCellValue(Factory2[0].proportion);
  926. cell12.CellStyle = cellStylebt;
  927. break;
  928. }
  929. }
  930. }
  931. else if (t + i == structure)
  932. {
  933. for (int z = 0; z < 3; z++)
  934. {
  935. string msg = "";
  936. switch (z)
  937. {
  938. case 0:
  939. ICell cell8 = irow7.CreateCell(8);
  940. cell8.SetCellValue(Factory3[1].name);
  941. cell8.CellStyle = cellStylebt;
  942. sheet.AddMergedRegion(new CellRangeAddress(structure, structure, 8, 9));
  943. break;
  944. case 1:
  945. ICell cell9 = irow7.CreateCell(10);
  946. cell9.SetCellValue(Factory3[1].number.ToString());
  947. cell9.CellStyle = cellStylebt;
  948. break;
  949. case 2:
  950. ICell cell12 = irow7.CreateCell(11);
  951. cell12.SetCellValue(Factory3[1].proportion);
  952. cell12.CellStyle = cellStylebt;
  953. break;
  954. }
  955. }
  956. }
  957. }
  958. else
  959. {
  960. IRow irow6 = sheet.CreateRow(t + i);
  961. if (i < Factory1.Count)
  962. {
  963. for (int j = 0; j < 6; j++)
  964. {
  965. string msg = "";
  966. switch (j)
  967. {
  968. case 0:
  969. msg = Factory1[i * 2].name;
  970. break;
  971. case 1:
  972. msg = Factory1[i * 2].number.ToString();
  973. break;
  974. case 2:
  975. msg = Factory1[i * 2].proportion;
  976. break;
  977. case 3:
  978. msg = Factory1[i * 2 + 1].name;
  979. break;
  980. case 4:
  981. msg = Factory1[i * 2 + 1].number.ToString();
  982. break;
  983. case 5:
  984. msg = Factory1[i * 2 + 1].proportion;
  985. break;
  986. }
  987. ICell cell9 = irow6.CreateCell(j + 1);
  988. cell9.SetCellValue(msg);
  989. cell9.CellStyle = cellStylebt;
  990. }
  991. }
  992. if (istrue)
  993. {
  994. int b = 1;
  995. if (a >1)
  996. {
  997. b= (a / 2) + (0 == a % 2 ? 0 : 1);
  998. }
  999. if (i < Factory2.Count -1)
  1000. {
  1001. for (int z = 0; z < 3; z++)
  1002. {
  1003. string msg = "";
  1004. switch (z)
  1005. {
  1006. case 0:
  1007. ICell cell8 = irow6.CreateCell(8);
  1008. cell8.SetCellValue(Factory2[i].name);
  1009. cell8.CellStyle = cellStylebt;
  1010. sheet.AddMergedRegion(new CellRangeAddress(t + i, t + i, 8, 9));
  1011. break;
  1012. case 1:
  1013. ICell cell9 = irow6.CreateCell(10);
  1014. cell9.SetCellValue(Factory2[i].number.ToString());
  1015. cell9.CellStyle = cellStylebt;
  1016. break;
  1017. case 2:
  1018. ICell cell12 = irow6.CreateCell(11);
  1019. cell12.SetCellValue(Factory2[i].proportion);
  1020. cell12.CellStyle = cellStylebt;
  1021. break;
  1022. }
  1023. }
  1024. }
  1025. else if (i < Factory2.Count )
  1026. {
  1027. for (int z = 0; z < 3; z++)
  1028. {
  1029. string msg = "";
  1030. switch (z)
  1031. {
  1032. case 0:
  1033. ICell cell8 = irow7.CreateCell(8);
  1034. cell8.SetCellValue(Factory2[i].name);
  1035. cell8.CellStyle = cellStylebt;
  1036. sheet.AddMergedRegion(new CellRangeAddress(t + i, t + i+b , 8, 9));
  1037. break;
  1038. case 1:
  1039. ICell cell9 = irow7.CreateCell(10);
  1040. cell9.SetCellValue(Factory2[i].number.ToString());
  1041. cell9.CellStyle = cellStylebt;
  1042. sheet.AddMergedRegion(new CellRangeAddress(t + i, t + i + b, 10, 10));
  1043. break;
  1044. case 2:
  1045. ICell cell12 = irow7.CreateCell(11);
  1046. cell12.SetCellValue(Factory2[i].proportion);
  1047. cell12.CellStyle = cellStylebt;
  1048. sheet.AddMergedRegion(new CellRangeAddress(t + i, t + i + b, 11, 11));
  1049. break;
  1050. }
  1051. }
  1052. }
  1053. if (i <( Factory3.Count + Factory2.Count+b -1)&& i>= Factory2.Count + b)
  1054. {
  1055. for (int z = 0; z < 3; z++)
  1056. {
  1057. string msg = "";
  1058. switch (z)
  1059. {
  1060. case 0:
  1061. ICell cell8 = irow6.CreateCell(8);
  1062. cell8.SetCellValue(Factory3[i- Factory2.Count -b ].name);
  1063. cell8.CellStyle = cellStylebt;
  1064. sheet.AddMergedRegion(new CellRangeAddress(t + i, t + i, 8, 9));
  1065. break;
  1066. case 1:
  1067. ICell cell9 = irow6.CreateCell(10);
  1068. cell9.SetCellValue(Factory3[i - Factory2.Count - b].number.ToString());
  1069. cell9.CellStyle = cellStylebt;
  1070. break;
  1071. case 2:
  1072. ICell cell12 = irow6.CreateCell(11);
  1073. cell12.SetCellValue(Factory3[i - Factory2.Count - b].proportion);
  1074. cell12.CellStyle = cellStylebt;
  1075. break;
  1076. }
  1077. }
  1078. }
  1079. else if ( i >= Factory2.Count + b&& i < Factory2.Count + b+ Factory3.Count )
  1080. {
  1081. for (int z = 0; z < 3; z++)
  1082. {
  1083. string msg = "";
  1084. switch (z)
  1085. {
  1086. case 0:
  1087. ICell cell8 = irow7.CreateCell(8);
  1088. cell8.SetCellValue(Factory3[i - Factory2.Count - b].name);
  1089. cell8.CellStyle = cellStylebt;
  1090. sheet.AddMergedRegion(new CellRangeAddress(t + i, t + i + b, 8, 9));
  1091. break;
  1092. case 1:
  1093. ICell cell9 = irow7.CreateCell(10);
  1094. cell9.SetCellValue(Factory3[i - Factory2.Count - b].number.ToString());
  1095. cell9.CellStyle = cellStylebt;
  1096. sheet.AddMergedRegion(new CellRangeAddress(t + i, t + i + b, 10, 10));
  1097. break;
  1098. case 2:
  1099. ICell cell12 = irow7.CreateCell(11);
  1100. cell12.SetCellValue(Factory3[i - Factory2.Count - b].proportion);
  1101. cell12.CellStyle = cellStylebt;
  1102. sheet.AddMergedRegion(new CellRangeAddress(t + i, t + i + b, 11, 11));
  1103. break;
  1104. }
  1105. }
  1106. }
  1107. }
  1108. }
  1109. }
  1110. }
  1111. #endregion
  1112. //自适应列宽度
  1113. for (int i = 0; i < 12; i++)
  1114. {
  1115. // sheet.AutoSizeColumn(i);
  1116. sheet.SetColumnWidth(i, 15 * 256);
  1117. }
  1118. using (MemoryStream ms = new MemoryStream())
  1119. {
  1120. workbook.Write(ms);
  1121. HttpContext curContext = HttpContext.Current;
  1122. // 设置编码和附件格式
  1123. curContext.Response.ContentType = "application/vnd.ms-excel";
  1124. curContext.Response.ContentEncoding = Encoding.UTF8;
  1125. curContext.Response.Charset = "";
  1126. curContext.Response.AppendHeader("Content-Disposition",
  1127. "attachment;filename=" + HttpUtility.UrlEncode("投诉产品日期分布表" + ".xls", Encoding.UTF8));
  1128. curContext.Response.BinaryWrite(ms.GetBuffer());
  1129. workbook = null;
  1130. ms.Close();
  1131. ms.Dispose();
  1132. curContext.Response.End();
  1133. }
  1134. return "";
  1135. }
  1136. catch
  1137. {
  1138. return "导出失败!";
  1139. }
  1140. }
  1141. }
  1142. }