Нет описания

NPOIHelper.cs 92KB

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