Brak opisu

NPOIHelper.cs 59KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  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. }
  1210. }