高新区管委会,以5.0标准版为基准,从双汇项目拷贝

NPOIHelper.cs 85KB

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