Keine Beschreibung

NPOIHelper.cs 62KB

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