Nenhuma Descrição

NPOIHelper.cs 92KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031
  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
  214. {
  215. return "导出失败!";
  216. }
  217. }
  218. /// <summary>
  219. /// 合并单元格
  220. /// </summary>
  221. /// <param name="sheet">要合并单元格所在的sheet</param>
  222. /// <param name="rowstart">开始行的索引</param>
  223. /// <param name="rowend">结束行的索引</param>
  224. /// <param name="colstart">开始列的索引</param>
  225. /// <param name="colend">结束列的索引</param>
  226. public static void SetCellRangeAddress(ISheet sheet, int rowstart, int rowend, int colstart, int colend)
  227. {
  228. CellRangeAddress cellRangeAddress = new CellRangeAddress(rowstart, rowend, colstart, colend);
  229. sheet.AddMergedRegion(cellRangeAddress);
  230. }
  231. /// <summary>
  232. /// 工单类型弹出下载框导出excel
  233. /// </summary>
  234. /// <param name="Name"></param>
  235. /// <param name="dt"></param>
  236. /// <param name="typeclass">仪器或试剂</param>
  237. /// <returns></returns>
  238. public string ExportToExcelForGDLX(string Name, DataTable dt, string typeclass, List<string> colnames, List<int> erows, List<string> secolnames)
  239. {
  240. try
  241. {
  242. HSSFWorkbook workbook = new HSSFWorkbook();
  243. ISheet sheet = workbook.CreateSheet("Sheet1");
  244. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  245. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  246. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  247. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  248. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  249. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  250. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  251. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  252. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  253. //字体
  254. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  255. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  256. headerfont.FontHeightInPoints = 12;
  257. HeadercellStyle.SetFont(headerfont);
  258. //用column name 作为列名
  259. int icolIndex = 0;
  260. IRow headerRow = sheet.CreateRow(0);
  261. for (int i = 0; i < colnames.Count; i++)
  262. {
  263. ICell cell = headerRow.CreateCell(i);
  264. cell.SetCellValue(colnames[i]);
  265. cell.CellStyle = HeadercellStyle;
  266. //SetCellRangeAddress(sheet, 0, 0, erows[i * 2], erows[i * 2 + 1]);
  267. }
  268. for (int k = 0; k < erows.Count / 2; k++)
  269. {
  270. SetCellRangeAddress(sheet, 0, 0, erows[k * 2], erows[k * 2 + 1]);
  271. }
  272. //添加第二行标题
  273. IRow SecRow = sheet.CreateRow(1);
  274. for (int i = 0; i < secolnames.Count; i++)
  275. {
  276. ICell cell = SecRow.CreateCell(i);
  277. cell.SetCellValue(secolnames[i].ToString());
  278. }
  279. ICellStyle cellStyle = workbook.CreateCellStyle();
  280. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  281. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  282. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  283. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  284. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  285. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  286. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  287. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  288. cellStyle.SetFont(cellfont);
  289. //建立内容行
  290. int iRowIndex = 0;
  291. foreach (DataRow dr in dt.Rows)
  292. {
  293. int iCellIndex = 0;
  294. IRow irow = sheet.CreateRow(iRowIndex + 2);
  295. for (int i = 0; i < dt.Columns.Count; i++)
  296. {
  297. string strsj = string.Empty;
  298. if (dr[i] != null)
  299. {
  300. strsj = dr[i].ToString();
  301. }
  302. ICell cell = irow.CreateCell(iCellIndex);
  303. cell.SetCellValue(strsj);
  304. cell.CellStyle = cellStyle;
  305. iCellIndex++;
  306. }
  307. iRowIndex++;
  308. }
  309. //自适应列宽度
  310. for (int i = 0; i < icolIndex; i++)
  311. {
  312. sheet.AutoSizeColumn(i);
  313. }
  314. using (MemoryStream ms = new MemoryStream())
  315. {
  316. workbook.Write(ms);
  317. HttpContext curContext = HttpContext.Current;
  318. // 设置编码和附件格式
  319. curContext.Response.ContentType = "application/vnd.ms-excel";
  320. curContext.Response.ContentEncoding = Encoding.UTF8;
  321. curContext.Response.Charset = "";
  322. curContext.Response.AppendHeader("Content-Disposition",
  323. "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
  324. curContext.Response.BinaryWrite(ms.GetBuffer());
  325. workbook = null;
  326. ms.Close();
  327. ms.Dispose();
  328. curContext.Response.End();
  329. }
  330. return "";
  331. }
  332. catch (Exception e)
  333. {
  334. return "导出失败!" + e.Message;
  335. }
  336. }
  337. /// <summary>
  338. /// 导入excel转换为datatable
  339. /// </summary>
  340. /// <param name="upfile"></param>
  341. /// <param name="headrow"></param>
  342. /// <returns></returns>
  343. public DataTable ExcelToTable(HttpPostedFile upfile, int headrow)
  344. {
  345. DataTable dt = new DataTable();
  346. IWorkbook workbook = null;
  347. Stream stream = upfile.InputStream;
  348. string suffix = upfile.FileName.Substring(upfile.FileName.LastIndexOf(".") + 1).ToLower();
  349. if (suffix == "xlsx") // 2007版本
  350. {
  351. workbook = new XSSFWorkbook(stream);
  352. }
  353. else if (suffix == "xls") // 2003版本
  354. {
  355. workbook = new HSSFWorkbook(stream);
  356. }
  357. //获取excel的第一个sheet
  358. ISheet sheet = workbook.GetSheetAt(0);
  359. //获取sheet的第一行
  360. IRow headerRow = sheet.GetRow(headrow);
  361. //一行最后一个方格的编号 即总的列数
  362. int cellCount = headerRow.LastCellNum;
  363. //最后一列的标号 即总的行数
  364. int rowCount = sheet.LastRowNum;
  365. //列名
  366. for (int i = 0; i < cellCount; i++)
  367. {
  368. dt.Columns.Add(headerRow.GetCell(i).ToString());
  369. }
  370. for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++)
  371. {
  372. DataRow dr = dt.NewRow();
  373. IRow row = sheet.GetRow(i);
  374. for (int j = row.FirstCellNum; j < cellCount; j++)
  375. {
  376. if (row.GetCell(j) != null)
  377. {
  378. dr[j] = row.GetCell(j).ToString();
  379. }
  380. }
  381. dt.Rows.Add(dr);
  382. }
  383. sheet = null;
  384. workbook = null;
  385. return dt;
  386. }
  387. /// <summary>
  388. /// 导入excel转换为datatable
  389. /// </summary>
  390. /// <param name="upfile"></param>
  391. /// <param name="headrow"></param>
  392. /// <returns></returns>
  393. public DataTable ExcelToTable(string fileName, int headrow)
  394. {
  395. DataTable dt = new DataTable();
  396. IWorkbook workbook = null;
  397. Stream stream = new FileStream(fileName, FileMode.OpenOrCreate);
  398. string suffix = fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower();
  399. if (suffix == "xlsx") // 2007版本
  400. {
  401. workbook = new XSSFWorkbook(stream);
  402. }
  403. else if (suffix == "xls") // 2003版本
  404. {
  405. workbook = new HSSFWorkbook(stream);
  406. }
  407. //获取excel的第一个sheet
  408. ISheet sheet = workbook.GetSheetAt(0);
  409. //获取sheet的第一行
  410. IRow headerRow = sheet.GetRow(headrow);
  411. //一行最后一个方格的编号 即总的列数
  412. int cellCount = headerRow.LastCellNum;
  413. //最后一列的标号 即总的行数
  414. int rowCount = sheet.LastRowNum;
  415. //列名
  416. for (int i = 0; i < cellCount; i++)
  417. {
  418. dt.Columns.Add(headerRow.GetCell(i).ToString());
  419. }
  420. for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++)
  421. {
  422. DataRow dr = dt.NewRow();
  423. IRow row = sheet.GetRow(i);
  424. for (int j = row.FirstCellNum; j < cellCount; j++)
  425. {
  426. if (row.GetCell(j) != null)
  427. {
  428. dr[j] = row.GetCell(j).ToString();
  429. }
  430. }
  431. dt.Rows.Add(dr);
  432. }
  433. sheet = null;
  434. workbook = null;
  435. return dt;
  436. }
  437. /// <summary>
  438. /// 简报导出
  439. /// </summary>
  440. /// <param name="ds"></param>
  441. /// <returns></returns>
  442. public string MarketExportToExcel(string stime,string etime,Market market )
  443. {
  444. try
  445. {
  446. HSSFWorkbook workbook = new HSSFWorkbook();
  447. ISheet sheet = workbook.CreateSheet("Sheet1");
  448. ICellStyle cellStyle = workbook.CreateCellStyle();
  449. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  450. //cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  451. //cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  452. //cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  453. //cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  454. //cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  455. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  456. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  457. cellStyle.SetFont(cellfont);
  458. ICellStyle cellStylebt = workbook.CreateCellStyle();
  459. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  460. cellfontbt.Boldweight = (short)FontBoldWeight.Bold;
  461. cellStylebt.SetFont(cellfontbt);
  462. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  463. cellStylebt.Alignment = HorizontalAlignment.Center;
  464. IRow irow1 = sheet.CreateRow(1);
  465. ICell cell1 = irow1.CreateCell(0);
  466. cell1.SetCellValue("时间:" + stime + " 至 " + etime);
  467. cell1.CellStyle = cellStylebt;
  468. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 0, 7));
  469. //((HSSFSheet)sheet).SetEnclosedBorderOfRegion(new CellRangeAddress(1, 1, 0, 7), BorderStyle.Thin, NPOI.HSSF.Util.HSSFColor.Black.Index);
  470. #region 产品投诉情况
  471. List<Ification> dt1 = market.product ;
  472. IRow irow2 = sheet.CreateRow(2);
  473. ICell cell2 = irow2.CreateCell(0);
  474. cell2.SetCellValue("产品投诉情况");
  475. cell2.CellStyle = cellStylebt;
  476. sheet.AddMergedRegion(new CellRangeAddress(2, 2, 0, 7));
  477. int dtrow = 3;
  478. if (dt1.Count >0)
  479. {
  480. int dt = 0;
  481. int a = dt1.Count / 3;
  482. float b = dt1.Count % 3;
  483. if (b >0)
  484. a = a + 1;
  485. dtrow += a;
  486. List<IRow> irow3list = new List<IRow>();
  487. for (int i =0;i <a;i++)
  488. {
  489. IRow irow3 = sheet.CreateRow(3 + i);
  490. irow3list.Add(irow3);
  491. }
  492. int td = 0;
  493. foreach (var it in dt1)
  494. {
  495. dt++;
  496. if (dt <4)
  497. {
  498. ICell cell101 = irow3list[td].CreateCell(3 * dt - 3);
  499. cell101.SetCellValue(it.name );
  500. cell101.CellStyle = cellStylebt;
  501. ICell cell102 = irow3list[td].CreateCell(3 * dt - 2);
  502. cell102.SetCellValue(it .number );
  503. cell102.CellStyle = cellStylebt;
  504. ICell cell103= irow3list[td].CreateCell(3 * dt-1 );
  505. cell103.SetCellValue(it.Proportion );
  506. cell103.CellStyle = cellStylebt;
  507. }
  508. else
  509. {
  510. dt = 1;
  511. td++;
  512. ICell cell101 = irow3list[td].CreateCell(3 * dt - 3);
  513. cell101.SetCellValue(it.name);
  514. cell101.CellStyle = cellStylebt;
  515. ICell cell102 = irow3list[td].CreateCell(3 * dt - 2);
  516. cell102.SetCellValue(it.number);
  517. cell102.CellStyle = cellStylebt;
  518. ICell cell103 = irow3list[td].CreateCell(3 * dt-1);
  519. cell103.SetCellValue(it.Proportion);
  520. cell103.CellStyle = cellStylebt;
  521. }
  522. }
  523. }
  524. #endregion
  525. #region 服务投诉情况
  526. Ification dt2 = market.service;
  527. dtrow++;
  528. IRow irow4 = sheet.CreateRow(dtrow);
  529. ICell cell4 = irow4.CreateCell(0);
  530. cell4.SetCellValue("服务投诉情况");
  531. cell4.CellStyle = cellStylebt;
  532. sheet.AddMergedRegion(new CellRangeAddress(dtrow, dtrow, 0, 7));
  533. if (dt2!=null )
  534. {
  535. dtrow++;
  536. IRow irow5 = sheet.CreateRow(dtrow);
  537. ICell cell51 = irow5.CreateCell(0);
  538. cell51.SetCellValue(dt2.name);
  539. cell51.CellStyle = cellStylebt;
  540. ICell cell52 = irow5.CreateCell(1);
  541. cell52.SetCellValue(dt2.number);
  542. cell52.CellStyle = cellStylebt;
  543. ICell cell53 = irow5.CreateCell(2);
  544. cell53.SetCellValue(dt2.Proportion);
  545. cell53.CellStyle = cellStylebt;
  546. }
  547. #endregion
  548. #region 涉媒投诉
  549. Ification dt3 = market.sediainvolved;
  550. dtrow++;
  551. IRow irow9 = sheet.CreateRow(dtrow);
  552. ICell cell9 = irow9.CreateCell(0);
  553. cell9.SetCellValue("涉媒投诉");
  554. cell9.CellStyle = cellStylebt;
  555. sheet.AddMergedRegion(new CellRangeAddress(dtrow, dtrow, 0, 7));
  556. if (dt3!=null )
  557. {
  558. dtrow++;
  559. IRow irow10 = sheet.CreateRow(dtrow);
  560. ICell cell53 = irow10.CreateCell(0);
  561. cell53.SetCellValue(dt3.name);
  562. cell53.CellStyle = cellStylebt;
  563. ICell cell54 = irow10.CreateCell(1);
  564. cell54.SetCellValue(dt3.number);
  565. cell54.CellStyle = cellStylebt;
  566. ICell cell55 = irow10.CreateCell(2);
  567. cell55.SetCellValue(dt3.Proportion);
  568. cell55.CellStyle = cellStylebt;
  569. }
  570. #endregion
  571. #region 市场抽检
  572. Ification dt4 = market.spotcheck;
  573. dtrow++;
  574. IRow irow11 = sheet.CreateRow(dtrow);
  575. ICell cell12 = irow11.CreateCell(0);
  576. cell12.SetCellValue("市场抽检");
  577. cell12.CellStyle = cellStylebt;
  578. sheet.AddMergedRegion(new CellRangeAddress(dtrow, dtrow, 0, 7));
  579. if (dt4!=null )
  580. {
  581. dtrow++;
  582. IRow irow12 = sheet.CreateRow(dtrow);
  583. ICell cell55 = irow12.CreateCell(0);
  584. cell55.SetCellValue(dt4.name);
  585. cell55.CellStyle = cellStylebt;
  586. ICell cell56 = irow12.CreateCell(1);
  587. cell56.SetCellValue(dt4.number);
  588. cell56.CellStyle = cellStylebt;
  589. ICell cell57 = irow12.CreateCell(2);
  590. cell57.SetCellValue(dt4.Proportion);
  591. cell57.CellStyle = cellStylebt;
  592. }
  593. #endregion
  594. #region 其他信息
  595. Ification dt5 = market.other;
  596. dtrow++;
  597. IRow irow13 = sheet.CreateRow(dtrow);
  598. ICell cell13 = irow13.CreateCell(0);
  599. cell13.SetCellValue("其他信息");
  600. cell13.CellStyle = cellStylebt;
  601. sheet.AddMergedRegion(new CellRangeAddress(dtrow, dtrow, 0, 7));
  602. if (dt5!=null )
  603. {
  604. dtrow++;
  605. IRow irow15 = sheet.CreateRow(dtrow);
  606. ICell cell57 = irow15.CreateCell(0);
  607. cell57.SetCellValue(dt4.name);
  608. cell57.CellStyle = cellStylebt;
  609. ICell cell58 = irow15.CreateCell(1);
  610. cell58.SetCellValue(dt4.number);
  611. cell58.CellStyle = cellStylebt;
  612. ICell cell59 = irow15.CreateCell(2);
  613. cell59.SetCellValue(dt5.Proportion);
  614. cell59.CellStyle = cellStylebt;
  615. }
  616. #endregion
  617. //自适应列宽度
  618. for (int i = 0; i < 8; i++)
  619. {
  620. // sheet.AutoSizeColumn(i);
  621. sheet.SetColumnWidth(i, 12 * 256);
  622. }
  623. using (MemoryStream ms = new MemoryStream())
  624. {
  625. workbook.Write(ms);
  626. HttpContext curContext = HttpContext.Current;
  627. // 设置编码和附件格式
  628. curContext.Response.ContentType = "application/vnd.ms-excel";
  629. curContext.Response.ContentEncoding = Encoding.UTF8;
  630. curContext.Response.Charset = "";
  631. curContext.Response.AppendHeader("Content-Disposition",
  632. "attachment;filename=" + HttpUtility.UrlEncode("市场信息简报" + ".xls", Encoding.UTF8));
  633. curContext.Response.BinaryWrite(ms.GetBuffer());
  634. workbook = null;
  635. ms.Close();
  636. ms.Dispose();
  637. curContext.Response.End();
  638. }
  639. return "";
  640. }
  641. catch
  642. {
  643. return "导出失败!";
  644. }
  645. }
  646. private void Columnwidth(int number, ISheet ffSheet)
  647. {
  648. for (int columnNum = 0; columnNum <= number; columnNum++)
  649. {
  650. int columnWidth = ffSheet.GetColumnWidth(columnNum) / 256;//获取当前列宽度
  651. for (int rowNum = 1; rowNum <= ffSheet.LastRowNum; rowNum++)//在这一列上循环行
  652. {
  653. IRow currentRow = ffSheet.GetRow(rowNum);
  654. ICell currentCell = currentRow.GetCell(columnNum);
  655. int length = Encoding.UTF8.GetBytes(currentCell.ToString()).Length;//获取当前单元格的内容宽度
  656. if (columnWidth < length + 1)
  657. {
  658. columnWidth = length + 1;
  659. }//若当前单元格内容宽度大于列宽,则调整列宽为当前单元格宽度,后面的+1是我人为的将宽度增加一个字符
  660. }
  661. }
  662. }
  663. private string ReturnMsg(int j, int i, List<Factory> Factory1)
  664. {
  665. string msg = "";
  666. switch (j)
  667. {
  668. case 0:
  669. msg = Factory1[i * 2].name;
  670. break;
  671. case 1:
  672. msg = Factory1[i * 2].number.ToString();
  673. break;
  674. case 2:
  675. msg = Factory1[i * 2].proportion;
  676. break;
  677. case 3:
  678. msg = Factory1[i * 2 + 1].name;
  679. break;
  680. case 4:
  681. msg = Factory1[i * 2 + 1].number.ToString();
  682. break;
  683. case 5:
  684. msg = Factory1[i * 2 + 1].proportion;
  685. break;
  686. }
  687. return msg;
  688. }
  689. /// <summary>
  690. /// 投诉产品日期分布表
  691. /// </summary>
  692. /// <param name="ds"></param>
  693. /// <returns></returns>
  694. public string DistributionToExcel(DateTime datetime, Product product)
  695. {
  696. try
  697. {
  698. HSSFWorkbook workbook = new HSSFWorkbook();
  699. ISheet sheet = workbook.CreateSheet("Sheet1");
  700. ICellStyle cellStyle = workbook.CreateCellStyle();
  701. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  702. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  703. cellStyle.SetFont(cellfont);
  704. ICellStyle cellStylebt = workbook.CreateCellStyle();
  705. cellStylebt.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  706. cellStylebt.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  707. cellStylebt.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  708. cellStylebt.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  709. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  710. cellfontbt.Boldweight = (short)FontBoldWeight.Bold;
  711. cellStylebt.SetFont(cellfontbt);
  712. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  713. cellStylebt.Alignment = HorizontalAlignment.Center;
  714. IRow irow1 = sheet.CreateRow(0);
  715. ICell cell1 = irow1.CreateCell(0);
  716. cell1.SetCellValue(datetime.Year +"年"+datetime .Month +"月份投诉产品日期分布情况表");
  717. cell1.CellStyle = cellStylebt;
  718. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 13));
  719. #region 产品投诉情况
  720. ICellStyle cellStylebt1 = workbook.CreateCellStyle();
  721. cellStylebt1.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  722. cellStylebt1.SetFont(cellfontbt);
  723. cellStylebt1.VerticalAlignment = VerticalAlignment.Center;
  724. cellStylebt1.Alignment = HorizontalAlignment.Center;
  725. IRow irow2 = sheet.CreateRow(1);
  726. ICell cell2 = irow2.CreateCell(0);
  727. ICellStyle style13 = workbook.CreateCellStyle();
  728. style13.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  729. style13.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  730. style13.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  731. style13.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  732. style13.BorderDiagonalLineStyle = BorderStyle.Thin;
  733. style13.BorderDiagonal = BorderDiagonal.Backward;
  734. style13.BorderDiagonalColor = IndexedColors.Black .Index;
  735. string sb = " 数量\n日期";
  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,int Tobeassignment)
  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 + "大区处理率最低。目前待分派"+ Tobeassignment+"条");
  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. /// <summary>
  1669. /// NPOI导出EXCEL
  1670. /// </summary>
  1671. /// <param name="dt">数据源</param>
  1672. /// <param name="title">导出文件的名称</param>
  1673. /// <param name="array">列宽数组</param>
  1674. public string NpoiExcel(DataTable table,string F_ParamValue)
  1675. {
  1676. try {
  1677. NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
  1678. NPOI.SS.UserModel.ISheet sheet = book.CreateSheet("Sheet1");
  1679. NPOI.SS.UserModel.IRow headerrow = sheet.CreateRow(0);
  1680. headerrow.Height = 30 * 20;
  1681. ICellStyle style = book.CreateCellStyle();
  1682. style.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1683. style.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1684. style.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1685. style.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1686. style.WrapText = true;
  1687. style.Alignment = HorizontalAlignment.Center;
  1688. style.VerticalAlignment = VerticalAlignment.Center;
  1689. List<Workorderpicture> workorderpictures = new List<Workorderpicture>();
  1690. foreach (DataRow dr in table.Rows)
  1691. {
  1692. Workorderpicture workorderpicture = new Workorderpicture();
  1693. workorderpicture.picture = new List<string>();
  1694. var FileUrl = GetFileData(dr["F_DealFile"].ToString (), F_ParamValue);
  1695. workorderpicture.workorder = dr["F_WorkOrderId"].ToString();
  1696. foreach (var dt in FileUrl )
  1697. {
  1698. workorderpicture.picture.Add(dt);
  1699. }
  1700. workorderpictures.Add(workorderpicture);
  1701. }
  1702. var count = workorderpictures.Select(x => x.picture.Count).Max();
  1703. //建立内容行
  1704. int iRowIndex = 0;
  1705. foreach (var it in workorderpictures)
  1706. {
  1707. int iCellIndex = 0;
  1708. IRow irow = sheet.CreateRow(iRowIndex );
  1709. for (int i = 0; i < count; i++)
  1710. {
  1711. string strsj = string.Empty;
  1712. if (iCellIndex == 0)
  1713. {
  1714. ICell cell = irow.CreateCell(iCellIndex);
  1715. cell.SetCellValue(it.workorder);
  1716. cell.CellStyle = style;
  1717. iCellIndex++;
  1718. }
  1719. else
  1720. {
  1721. if (it .picture .Count <= count)
  1722. {
  1723. ICell cell = irow.CreateCell(iCellIndex);
  1724. // cell.SetCellValue(it.workorder);
  1725. if (iCellIndex<= it.picture.Count)
  1726. {
  1727. bool n= AddPieChartAsync(book, sheet, it.picture[iCellIndex - 1], iRowIndex, iCellIndex, 1000);
  1728. if (!n )
  1729. cell.SetCellValue("图片不存在!");
  1730. }
  1731. else
  1732. cell.SetCellValue("");
  1733. cell.CellStyle = style;
  1734. iCellIndex++;
  1735. }
  1736. }
  1737. }
  1738. iRowIndex++;
  1739. }
  1740. //自适应列宽度
  1741. for (int i = 0; i < count; i++)
  1742. {
  1743. sheet.AutoSizeColumn(800);
  1744. }
  1745. using (MemoryStream ms = new MemoryStream())
  1746. {
  1747. book.Write(ms);
  1748. HttpContext curContext = HttpContext.Current;
  1749. // 设置编码和附件格式
  1750. curContext.Response.ContentType = "application/vnd.ms-excel";
  1751. curContext.Response.ContentEncoding = Encoding.UTF8;
  1752. curContext.Response.Charset = "";
  1753. curContext.Response.AppendHeader("Content-Disposition",
  1754. "attachment;filename=" + HttpUtility.UrlEncode("工单列表" + ".xls", Encoding.UTF8));
  1755. curContext.Response.BinaryWrite(ms.GetBuffer());
  1756. book = null;
  1757. ms.Close();
  1758. ms.Dispose();
  1759. curContext.Response.End();
  1760. }
  1761. return "";
  1762. }
  1763. catch (Exception e)
  1764. {
  1765. return "导出失败!" + e.Message;
  1766. }
  1767. }
  1768. public class Workorderpicture
  1769. {
  1770. public string workorder;
  1771. public List<string> picture;
  1772. }
  1773. public List<string> GetFileData(string ids, string prefix)
  1774. {
  1775. List<string> F_Url = new List<string>();
  1776. DataTable dt = new DataTable();
  1777. if (!string.IsNullOrEmpty(ids))
  1778. {
  1779. dt = DbHelperSQL.Query("select * from T_Sys_Accessories where F_Id in (" + ids + ")").Tables[0];
  1780. foreach (DataRow dr in dt.Rows)
  1781. {
  1782. string Url = prefix + dr["F_Url"].ToString();
  1783. F_Url.Add(Url);
  1784. }
  1785. }
  1786. return F_Url;
  1787. }
  1788. /// <summary>
  1789. /// 向sheet插入图片
  1790. /// </summary>
  1791. /// <param name="workbook">工作辅</param>
  1792. /// <param name="sheet">sheet页</param>
  1793. /// <param name="fileurl">图片地址</param>
  1794. /// <param name="row">当前行</param>
  1795. /// <param name="col">当前列</param>
  1796. /// <param name="RowHeight">行高</param>
  1797. public bool AddPieChartAsync(HSSFWorkbook workbook, ISheet sheet, string fileurl, int row, int col, short RowHeight)
  1798. {
  1799. WebClient myWebClient = new WebClient();
  1800. myWebClient.Credentials = CredentialCache.DefaultCredentials;
  1801. myWebClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
  1802. try
  1803. {
  1804. byte[] bytes = myWebClient.DownloadData(fileurl);
  1805. if (bytes != null)
  1806. {
  1807. int pictureIdx = workbook.AddPicture(bytes, NPOI.SS.UserModel.PictureType.JPEG);
  1808. HSSFPatriarch patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
  1809. HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 20, 20, col, row, col + 1, row + 1);
  1810. //##处理照片位置,【图片左上角为(col, row)第row+1行col+1列,右下角为( col +1, row +1)第 col +1+1行row +1+1列,宽为100,高为50
  1811. HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);
  1812. var index = sheet.GetRow(row) ?? sheet.CreateRow(row);
  1813. index.Height = RowHeight;
  1814. //pict.Resize(0);//这句话一定不要,这是用图片原始大小来显示
  1815. }
  1816. return true;
  1817. }
  1818. catch
  1819. {
  1820. return false ;
  1821. }
  1822. }
  1823. }
  1824. }