Sin descripción

NPOIHelper.cs 92KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034
  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. // StringBuilder sb = new StringBuilder();
  737. // sb.AppendLine("数量".PadLeft(15));//该行不足长度10在左侧填空格
  738. // sb.AppendLine("日期".PadRight(15));//该行不足长度10在右侧填空格
  739. cell2.SetCellValue(sb);
  740. cell2.CellStyle = cellStylebt;
  741. style13.WrapText = true;
  742. irow2.GetCell(0).CellStyle = style13;
  743. sheet.AddMergedRegion(new CellRangeAddress(1, 2, 0,0 ));
  744. ICell cell6 = irow2.CreateCell(1);
  745. cell6.SetCellValue("投诉产品");
  746. cell6.CellStyle = cellStylebt;
  747. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 1, 1));
  748. for (int i=0;i<5;i ++)
  749. {
  750. string msg = "";
  751. switch (i)
  752. {
  753. case 0:
  754. msg = datetime.AddMonths(-11).Month + "-" + datetime.AddMonths(-3).Month + "月份日期";
  755. break;
  756. case 1:
  757. msg = datetime.AddMonths(-2).Month + "月份日期";
  758. break;
  759. case 2:
  760. msg = datetime.AddMonths(-1).Month + "月份日期";
  761. break;
  762. case 3:
  763. msg = datetime.Month + "月份日期";
  764. break;
  765. case 4:
  766. msg = "不清楚日期";
  767. break;
  768. }
  769. ICell cell3 = irow2.CreateCell(i *2+ 2 );
  770. cell3.SetCellValue(msg);
  771. cell3.CellStyle = cellStylebt;
  772. ICell cell4 = irow2.CreateCell(i * 2 + 3);
  773. cell4.CellStyle = cellStylebt;
  774. sheet.AddMergedRegion(new CellRangeAddress(1, 1, i * 2 + 2, i * 2 + 3));
  775. }
  776. IRow irow3 = sheet.CreateRow(2);
  777. for (int i = 0; i < 11; i++)
  778. {
  779. string msg = "";
  780. if (i ==0)
  781. {
  782. msg = "数量";
  783. }
  784. else
  785. {
  786. if (i % 2 == 0)
  787. {
  788. msg = "占比";
  789. }
  790. else
  791. {
  792. msg = "数量";
  793. }
  794. }
  795. ICell cell4 = irow3.CreateCell(i+1);
  796. cell4.SetCellValue(msg);
  797. cell4.CellStyle = cellStylebt;
  798. }
  799. List<Date> dt1 = product.dates;
  800. if (dt1!=null )
  801. {
  802. for (int i=0;i < dt1.Count;i ++)
  803. {
  804. IRow irow4 = sheet.CreateRow(3 + i);
  805. string msg = "";
  806. for (int j = 0; j < 12; j ++)
  807. {
  808. switch (j )
  809. {
  810. case 0:
  811. msg = dt1[i].name;
  812. break;
  813. case 1:
  814. msg = dt1[i].total.ToString();
  815. break;
  816. case 2:
  817. msg = dt1[i].MonthCount1 .ToString ();
  818. break;
  819. case 3:
  820. msg = dt1[i].MonthCountmix1 .ToString();
  821. break;
  822. case 4:
  823. msg = dt1[i].MonthCount2.ToString();
  824. break;
  825. case 5:
  826. msg = dt1[i].MonthCountmix2.ToString();
  827. break;
  828. case 6:
  829. msg = dt1[i].MonthCount3.ToString();
  830. break;
  831. case 7:
  832. msg = dt1[i].MonthCountmix3.ToString();
  833. break;
  834. case 8:
  835. msg = dt1[i].MonthCount4.ToString();
  836. break;
  837. case 9:
  838. msg = dt1[i].MonthCountmix4.ToString();
  839. break;
  840. case 10:
  841. msg = dt1[i].MonthCount5.ToString();
  842. break;
  843. case 11:
  844. msg = dt1[i].MonthCountmix5.ToString();
  845. break;
  846. }
  847. ICell cell5 = irow4.CreateCell(j);
  848. cell5.SetCellValue(msg);
  849. cell5.CellStyle = cellStylebt;
  850. }
  851. }
  852. }
  853. int t = dt1.Count + 2;int count = 0;
  854. List<Factory> Factory1 = product.factory;
  855. List<Factory> Factory2 = product.problem;
  856. List<Factory> Factory3 = product.product;
  857. int factory = 0, problem = 0, productcode = 0;
  858. if (Factory1 != null)
  859. factory = (Factory1.Count / 2) + (0 == Factory1.Count % 2 ? 0 : 1);
  860. if (Factory2 != null)
  861. problem = Factory2.Count;
  862. if (Factory3 != null)
  863. productcode = Factory3.Count;
  864. bool istrue = true; int a = 0, total = problem + productcode;
  865. if (factory>(total))
  866. {
  867. count = factory;
  868. a = factory - total;
  869. }
  870. else
  871. {
  872. istrue = false;
  873. count = total;
  874. }
  875. IRow irow5 = sheet.CreateRow(t+1);
  876. ICell cell7 = irow5.CreateCell(0);
  877. cell7.SetCellValue("工\n厂\n投\n诉\n占\n比\n");
  878. cell7.CellStyle = cellStylebt;
  879. cell7.CellStyle.WrapText=true ;
  880. sheet.AddMergedRegion(new CellRangeAddress(t+1, t+ count, 0, 0));
  881. ICell cell10 = irow5.CreateCell(7);
  882. cell10.SetCellValue("各\n质\n量\n问\n题\n占\n比\n");
  883. cell10.CellStyle = cellStylebt;
  884. cell10.CellStyle.WrapText = true;
  885. int structure = t + problem;
  886. if (istrue)
  887. {
  888. if (a >1)
  889. {
  890. structure = t + problem + a / 2;
  891. sheet.AddMergedRegion(new CellRangeAddress(t+1, t + problem + a / 2, 7, 7));
  892. }
  893. else
  894. {
  895. structure = t + problem;
  896. sheet.AddMergedRegion(new CellRangeAddress(t+1, t + problem, 7, 7));
  897. }
  898. }
  899. else
  900. sheet.AddMergedRegion(new CellRangeAddress(t+1, t + problem, 7, 7));
  901. IRow irow7 = sheet.CreateRow(structure+1);
  902. ICell cell11 = irow7.CreateCell(7);
  903. cell11.SetCellValue("结\n构\n占\n比\n");
  904. cell11.CellStyle = cellStylebt;
  905. cell11.CellStyle.WrapText = true;
  906. if (istrue)
  907. {
  908. if (a > 1)
  909. sheet.AddMergedRegion(new CellRangeAddress(structure + 1, structure+ productcode+a/2, 7, 7));
  910. else
  911. sheet.AddMergedRegion(new CellRangeAddress(structure + 1, structure + productcode+a, 7, 7));
  912. }
  913. else
  914. sheet.AddMergedRegion(new CellRangeAddress(structure + 1, structure + productcode, 7, 7));
  915. if (Factory1 != null)
  916. {
  917. for (int i = 0; i < count; i++)
  918. {
  919. if (i ==0 || t + i== structure)
  920. {
  921. if (Factory1.Count > i*2)
  922. {
  923. for (int j = 0; j < 6; j++)
  924. {
  925. string msg = ReturnMsg(j, i, Factory1);
  926. if (i == 0)
  927. {
  928. ICell cell8 = irow5.CreateCell(j + 1);
  929. cell8.SetCellValue(msg);
  930. cell8.CellStyle = cellStylebt;
  931. }
  932. else
  933. {
  934. ICell cell9 = irow7.CreateCell(j + 1);
  935. cell9.SetCellValue(msg);
  936. cell9.CellStyle = cellStylebt;
  937. }
  938. }
  939. }
  940. if (i ==0)
  941. {
  942. for (int z = 0; z < 3; z++)
  943. {
  944. switch (z)
  945. {
  946. case 0:
  947. ICell cell12 = irow5.CreateCell(8);
  948. cell12.SetCellValue(Factory2[0].name );
  949. cell12.CellStyle = cellStylebt;
  950. sheet.AddMergedRegion(new CellRangeAddress(t+1, t+1 , 8, 9));
  951. ICell cell22 = irow5.CreateCell(9);
  952. cell22.CellStyle = cellStylebt;
  953. break;
  954. case 1:
  955. ICell cell13 = irow5.CreateCell(10);
  956. cell13.SetCellValue(Factory2[0].number .ToString ());
  957. cell13.CellStyle = cellStylebt;
  958. break;
  959. case 2:
  960. ICell cell14 = irow5.CreateCell(11);
  961. cell14.SetCellValue(Factory2[0].proportion);
  962. cell14.CellStyle = cellStylebt;
  963. break;
  964. }
  965. }
  966. }
  967. else
  968. {
  969. ICell cell119 = irow7.CreateCell(0);
  970. cell119.CellStyle = cellStylebt;
  971. for (int z = 0; z < 3; z++)
  972. {
  973. switch (z)
  974. {
  975. case 0:
  976. ICell cell15 = irow7.CreateCell(8);
  977. cell15.SetCellValue(Factory3[0].name);
  978. cell15.CellStyle = cellStylebt;
  979. sheet.AddMergedRegion(new CellRangeAddress(structure+1, structure+1, 8, 9));
  980. ICell cell31 = irow7.CreateCell(9);
  981. cell15.CellStyle = cellStylebt;
  982. break;
  983. case 1:
  984. ICell cell16 = irow7.CreateCell(10);
  985. cell16.SetCellValue(Factory3[0].number.ToString());
  986. cell16.CellStyle = cellStylebt;
  987. break;
  988. case 2:
  989. ICell cell17= irow7.CreateCell(11);
  990. cell17.SetCellValue(Factory3[0].proportion);
  991. cell17.CellStyle = cellStylebt;
  992. break;
  993. }
  994. }
  995. }
  996. }
  997. else
  998. {
  999. IRow irow6 = sheet.CreateRow(t+1 + i);
  1000. if (Factory1.Count %2!=0)
  1001. {
  1002. if (i * 2+1 < Factory1.Count)
  1003. {
  1004. for (int j = 0; j < 6; j++)
  1005. {
  1006. string msg = ReturnMsg(j, i, Factory1);
  1007. ICell cell18 = irow6.CreateCell(j + 1);
  1008. cell18.SetCellValue(msg);
  1009. cell18.CellStyle = cellStylebt;
  1010. }
  1011. }
  1012. else if (i * 2 + 1 == Factory1.Count)
  1013. {
  1014. for (int j = 0; j < 3; j++)
  1015. {
  1016. string msg = ReturnMsg(j, i, Factory1);
  1017. ICell cell18 = irow6.CreateCell(j + 1);
  1018. cell18.SetCellValue(msg);
  1019. cell18.CellStyle = cellStylebt;
  1020. }
  1021. }
  1022. }
  1023. else
  1024. {
  1025. if (i * 2 < Factory1.Count)
  1026. {
  1027. for (int j = 0; j < 6; j++)
  1028. {
  1029. string msg = ReturnMsg(j, i, Factory1);
  1030. ICell cell18 = irow6.CreateCell(j + 1);
  1031. cell18.SetCellValue(msg);
  1032. cell18.CellStyle = cellStylebt;
  1033. }
  1034. }
  1035. }
  1036. int b = 0;
  1037. if (a >1)
  1038. {
  1039. b= (a / 2) + (0 == a % 2 ? 0 : 1);
  1040. }
  1041. if (i < Factory2.Count -1)
  1042. {
  1043. for (int z = 0; z < 3; z++)
  1044. {
  1045. switch (z)
  1046. {
  1047. case 0:
  1048. ICell cell8 = irow6.CreateCell(8);
  1049. cell8.SetCellValue(Factory2[i].name);
  1050. cell8.CellStyle = cellStylebt;
  1051. sheet.AddMergedRegion(new CellRangeAddress(t+1 + i, t+1 + i, 8, 9));
  1052. ICell cell22 = irow6.CreateCell(9);
  1053. cell22.CellStyle = cellStylebt;
  1054. break;
  1055. case 1:
  1056. ICell cell9 = irow6.CreateCell(10);
  1057. cell9.SetCellValue(Factory2[i].number.ToString());
  1058. cell9.CellStyle = cellStylebt;
  1059. break;
  1060. case 2:
  1061. ICell cell12 = irow6.CreateCell(11);
  1062. cell12.SetCellValue(Factory2[i].proportion);
  1063. cell12.CellStyle = cellStylebt;
  1064. break;
  1065. }
  1066. }
  1067. }
  1068. else if (i == Factory2.Count-1 )
  1069. {
  1070. for (int z = 0; z < 3; z++)
  1071. {
  1072. string msg = "";
  1073. switch (z)
  1074. {
  1075. case 0:
  1076. ICell cell8 = irow6.CreateCell(8);
  1077. cell8.SetCellValue(Factory2[i].name);
  1078. cell8.CellStyle = cellStylebt;
  1079. sheet.AddMergedRegion(new CellRangeAddress(t +1+ i, t+1 + i+b , 8, 9));
  1080. if (istrue )
  1081. {
  1082. ICell cell22 = irow6.CreateCell(9);
  1083. cell22.CellStyle = cellStylebt;
  1084. }
  1085. break;
  1086. case 1:
  1087. ICell cell9 = irow6.CreateCell(10);
  1088. cell9.SetCellValue(Factory2[i].number.ToString());
  1089. cell9.CellStyle = cellStylebt;
  1090. sheet.AddMergedRegion(new CellRangeAddress(t+1 + i, t+1 + i + b, 10, 10));
  1091. break;
  1092. case 2:
  1093. ICell cell12 = irow6.CreateCell(11);
  1094. cell12.SetCellValue(Factory2[i].proportion);
  1095. cell12.CellStyle = cellStylebt;
  1096. sheet.AddMergedRegion(new CellRangeAddress(t+1 + i, t+1 + i + b, 11, 11));
  1097. break;
  1098. }
  1099. }
  1100. }
  1101. if (i <( Factory3.Count + Factory2.Count+b -1)&& i>= Factory2.Count + b)
  1102. {
  1103. for (int z = 0; z < 3; z++)
  1104. {
  1105. switch (z)
  1106. {
  1107. case 0:
  1108. ICell cell8 = irow6.CreateCell(8);
  1109. cell8.SetCellValue(Factory3[i- Factory2.Count -b ].name);
  1110. cell8.CellStyle = cellStylebt;
  1111. sheet.AddMergedRegion(new CellRangeAddress(t +1+ i, t +1+ i, 8, 9));
  1112. ICell cel27 = irow6.CreateCell(9);
  1113. cel27.CellStyle = cellStylebt;
  1114. break;
  1115. case 1:
  1116. ICell cell9 = irow6.CreateCell(10);
  1117. cell9.SetCellValue(Factory3[i - Factory2.Count - b ].number.ToString());
  1118. cell9.CellStyle = cellStylebt;
  1119. break;
  1120. case 2:
  1121. ICell cell12 = irow6.CreateCell(11);
  1122. cell12.SetCellValue(Factory3[i - Factory2.Count - b ].proportion);
  1123. cell12.CellStyle = cellStylebt;
  1124. break;
  1125. }
  1126. }
  1127. }
  1128. else if ( i== Factory2.Count + b+ Factory3.Count-1 )
  1129. {
  1130. for (int z = 0; z < 3; z++)
  1131. {
  1132. switch (z)
  1133. {
  1134. case 0:
  1135. ICell cell8 = irow6.CreateCell(8);
  1136. cell8.SetCellValue(Factory3[i - Factory2.Count - b].name);
  1137. cell8.CellStyle = cellStylebt;
  1138. sheet.AddMergedRegion(new CellRangeAddress(t +1+ i, t+1 + i + b, 8, 9));
  1139. break;
  1140. case 1:
  1141. ICell cell9 = irow6.CreateCell(10);
  1142. cell9.SetCellValue(Factory3[i - Factory2.Count - b].number.ToString());
  1143. cell9.CellStyle = cellStylebt;
  1144. sheet.AddMergedRegion(new CellRangeAddress(t + 1 + i, t + 1 + i + b, 10, 10));
  1145. break;
  1146. case 2:
  1147. ICell cell12 = irow6.CreateCell(11);
  1148. cell12.SetCellValue(Factory3[i - Factory2.Count - b].proportion);
  1149. cell12.CellStyle = cellStylebt;
  1150. sheet.AddMergedRegion(new CellRangeAddress(t + 1 + i, t + 1 + i + b, 11, 11));
  1151. break;
  1152. }
  1153. }
  1154. }
  1155. if ( i == Factory2.Count)
  1156. {
  1157. ICell cell120 = irow6.CreateCell(7);
  1158. cell120.CellStyle = cellStylebt;
  1159. ICell cell124 = irow6.CreateCell(9);
  1160. cell124.CellStyle = cellStylebt;
  1161. ICell cell125 = irow6.CreateCell(10);
  1162. cell125.CellStyle = cellStylebt;
  1163. ICell cell126 = irow6.CreateCell(11);
  1164. cell126.CellStyle = cellStylebt;
  1165. }
  1166. if (!istrue )
  1167. {
  1168. if (i * 2>= Factory1.Count)
  1169. {
  1170. ICell cell119 = irow6.CreateCell(0);
  1171. cell119.CellStyle = cellStylebt;
  1172. }
  1173. if (i *2 +1>= Factory1.Count&i < Factory2.Count)
  1174. {
  1175. ICell cell119 = irow6.CreateCell(7);
  1176. cell119.CellStyle = cellStylebt;
  1177. }
  1178. if (i > Factory2.Count)
  1179. {
  1180. ICell cell119 = irow6.CreateCell(7);
  1181. cell119.CellStyle = cellStylebt;
  1182. }
  1183. if (i == count - 1)
  1184. {
  1185. ICell cell119 = irow6.CreateCell(1);
  1186. cell119.CellStyle = cellStylebt1;
  1187. ICell cell121 = irow6.CreateCell(2);
  1188. cell121.CellStyle = cellStylebt1;
  1189. ICell cell132 = irow6.CreateCell(3);
  1190. cell132.CellStyle = cellStylebt1;
  1191. ICell cell128 = irow6.CreateCell(4);
  1192. cell128.CellStyle = cellStylebt1;
  1193. ICell cell129 = irow6.CreateCell(5);
  1194. cell129.CellStyle = cellStylebt1;
  1195. ICell cell130 = irow6.CreateCell(6);
  1196. cell130.CellStyle = cellStylebt1;
  1197. ICell cell131 = irow6.CreateCell(9);
  1198. cell131.CellStyle = cellStylebt1;
  1199. }
  1200. }
  1201. else if (i == count - 1)
  1202. {
  1203. ICell cell119 = irow6.CreateCell(0);
  1204. cell119.CellStyle = cellStylebt;
  1205. ICell cell121 = irow6.CreateCell(7);
  1206. cell121.CellStyle = cellStylebt;
  1207. ICell cell122 = irow6.CreateCell(4);
  1208. cell122.CellStyle = cellStylebt;
  1209. ICell cell123 = irow6.CreateCell(5);
  1210. cell123.CellStyle = cellStylebt;
  1211. ICell cell124 = irow6.CreateCell(6);
  1212. cell124.CellStyle = cellStylebt;
  1213. sheet.AddMergedRegion(new CellRangeAddress(t + 1 + i, t + 1 + i, 4, 6));
  1214. ICell cell132 = irow6.CreateCell(8);
  1215. cell132.CellStyle = cellStylebt;
  1216. ICell cell128 = irow6.CreateCell(9);
  1217. cell128.CellStyle = cellStylebt;
  1218. ICell cell129 = irow6.CreateCell(10);
  1219. cell129.CellStyle = cellStylebt;
  1220. ICell cell130 = irow6.CreateCell(11);
  1221. cell130.CellStyle = cellStylebt;
  1222. // sheet.AddMergedRegion(new CellRangeAddress(t + 1 + i, t + 1 + i, 8, 11));
  1223. }
  1224. }
  1225. }
  1226. }
  1227. #endregion
  1228. //自适应列宽度
  1229. for (int i = 0; i < 12; i++)
  1230. {
  1231. // sheet.AutoSizeColumn(i);
  1232. sheet.SetColumnWidth(i, 15 * 256);
  1233. }
  1234. using (MemoryStream ms = new MemoryStream())
  1235. {
  1236. workbook.Write(ms);
  1237. HttpContext curContext = HttpContext.Current;
  1238. // 设置编码和附件格式
  1239. curContext.Response.ContentType = "application/vnd.ms-excel";
  1240. curContext.Response.ContentEncoding = Encoding.UTF8;
  1241. curContext.Response.Charset = "";
  1242. curContext.Response.AppendHeader("Content-Disposition",
  1243. "attachment;filename=" + HttpUtility.UrlEncode("投诉产品日期分布表" + ".xls", Encoding.UTF8));
  1244. curContext.Response.BinaryWrite(ms.GetBuffer());
  1245. workbook = null;
  1246. ms.Close();
  1247. ms.Dispose();
  1248. curContext.Response.End();
  1249. }
  1250. return "";
  1251. }
  1252. catch
  1253. {
  1254. return "导出失败!";
  1255. }
  1256. }
  1257. public string Complainthandling(DateTime stime, DateTime etime, List<Complaint> complaints,string maxarea,string minarea)
  1258. {
  1259. try
  1260. {
  1261. HSSFWorkbook workbook = new HSSFWorkbook();
  1262. ISheet sheet = workbook.CreateSheet("Sheet1");
  1263. ICellStyle cellStyle = workbook.CreateCellStyle();
  1264. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  1265. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  1266. cellfont.FontHeight = 300;
  1267. cellStyle.SetFont(cellfont);
  1268. ICellStyle cellStylebt = workbook.CreateCellStyle();
  1269. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  1270. cellStylebt.SetFont(cellfontbt);
  1271. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  1272. cellStylebt.Alignment = HorizontalAlignment.Center;
  1273. #region 标题
  1274. ICellStyle cellStylebt1 = workbook.CreateCellStyle();
  1275. NPOI.SS.UserModel.IFont cellfontbt1 = workbook.CreateFont();
  1276. cellfontbt1.Boldweight = (short)FontBoldWeight.Bold;
  1277. cellfontbt1.FontHeight = 500;
  1278. cellStylebt1.SetFont(cellfontbt1);
  1279. cellStylebt1.VerticalAlignment = VerticalAlignment.Center;
  1280. cellStylebt1.Alignment = HorizontalAlignment.Center;
  1281. IRow irow1 = sheet.CreateRow(0);
  1282. ICell cell1 = irow1.CreateCell(0);
  1283. cell1.SetCellValue("各大区投诉处理情况("+ stime.Month + "月"+ stime .Day + "-"+ etime .Day + "日)");
  1284. cell1.CellStyle = cellStylebt1;
  1285. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 10));
  1286. #endregion
  1287. #region 说明
  1288. ICellStyle cellStylebt2 = workbook.CreateCellStyle();
  1289. NPOI.SS.UserModel.IFont cellfontbt2 = workbook.CreateFont();
  1290. cellfontbt2.Boldweight = (short)FontBoldWeight.Bold;
  1291. cellfontbt2.FontHeight = 300;
  1292. cellStylebt2.SetFont(cellfontbt2);
  1293. cellStylebt2.WrapText = true;//设置换行这个要先设置
  1294. cellStylebt2.VerticalAlignment = VerticalAlignment.Center;
  1295. cellStylebt2.Alignment = HorizontalAlignment.Center;
  1296. IRow irow2 = sheet.CreateRow(1);
  1297. ICell cell2 = irow2.CreateCell(0);
  1298. cell2.SetCellValue(" " + stime.Month + "月" + stime.Day + "日-" + etime.Month + "月" + + etime.Day + "日共接400热线投诉"+ complaints [complaints.Count -1].complaintnumber + "起," +
  1299. "待分派投诉"+ 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 +"大区处理率最低。");
  1300. cell2.CellStyle = cellStylebt2;
  1301. sheet.AddMergedRegion(new CellRangeAddress(1, 5, 0, 10));
  1302. #endregion
  1303. ICellStyle cellStylebt3 = workbook.CreateCellStyle();
  1304. NPOI.SS.UserModel.IFont cellfontbt3 = workbook.CreateFont();
  1305. cellfontbt3.Boldweight = (short)FontBoldWeight.Bold;
  1306. cellfontbt3.FontHeight = 200;
  1307. cellStylebt3.SetFont(cellfontbt3);
  1308. cellStylebt3.VerticalAlignment = VerticalAlignment.Center;
  1309. cellStylebt3.Alignment = HorizontalAlignment.Center;
  1310. IRow irow3 = sheet.CreateRow(6);
  1311. string [] cols= { "序号", "大区", "投诉起数", "待分派投诉", "当日分派率", "待处理投诉", "处理中投诉", "已处理投诉", "处理率", "公关经理跟单率" };
  1312. int icolIndex = 0;
  1313. foreach (string dc in cols)
  1314. {
  1315. ICell cell = irow3.CreateCell(icolIndex);
  1316. cell.SetCellValue(dc);
  1317. cell.CellStyle = cellStylebt3;
  1318. icolIndex++;
  1319. }
  1320. for (int i=0;i < complaints.Count -1;i ++)
  1321. {
  1322. IRow irow4 = sheet.CreateRow(7+i );
  1323. for (int j=0; j < 10; j++)
  1324. {
  1325. switch (j )
  1326. {
  1327. case 0:
  1328. ICell cell = irow4.CreateCell(j);
  1329. cell.SetCellValue(complaints[i].serialnumbe);
  1330. cell.CellStyle = cellStylebt;
  1331. break;
  1332. case 1:
  1333. ICell cell4 = irow4.CreateCell(j);
  1334. cell4.SetCellValue(complaints[i].area);
  1335. cell4.CellStyle = cellStylebt;
  1336. break;
  1337. case 2:
  1338. ICell cel5 = irow4.CreateCell(j);
  1339. cel5.SetCellValue(complaints[i].complaintnumber);
  1340. cel5.CellStyle = cellStylebt;
  1341. break;
  1342. case 3:
  1343. ICell cel6 = irow4.CreateCell(j);
  1344. cel6.SetCellValue(complaints[i].assignmentnumber);
  1345. cel6.CellStyle = cellStylebt;
  1346. break;
  1347. case 4:
  1348. ICell cel7 = irow4.CreateCell(j);
  1349. cel7.SetCellValue(complaints[i].assignmentrate);
  1350. cel7.CellStyle = cellStylebt;
  1351. break;
  1352. case 5:
  1353. ICell cel8 = irow4.CreateCell(j);
  1354. cel8.SetCellValue(complaints[i].pendingnumber);
  1355. cel8.CellStyle = cellStylebt;
  1356. break;
  1357. case 6:
  1358. ICell cel9 = irow4.CreateCell(j);
  1359. cel9.SetCellValue(complaints[i].processingnumber);
  1360. cel9.CellStyle = cellStylebt;
  1361. break;
  1362. case 7:
  1363. ICell cell0 = irow4.CreateCell(j);
  1364. cell0.SetCellValue(complaints[i].processednumber);
  1365. cell0.CellStyle = cellStylebt;
  1366. break;
  1367. case 8:
  1368. ICell cell11 = irow4.CreateCell(j);
  1369. cell11.SetCellValue(complaints[i].processedrate);
  1370. cell11.CellStyle = cellStylebt;
  1371. break;
  1372. case 9:
  1373. ICell cell12 = irow4.CreateCell(j);
  1374. cell12.SetCellValue(complaints[i].documentaryrate);
  1375. cell12.CellStyle = cellStylebt;
  1376. break;
  1377. }
  1378. }
  1379. }
  1380. IRow irow5 = sheet.CreateRow(7+ complaints.Count - 1);
  1381. for (int j = 0; j < 9; j++)
  1382. {
  1383. switch (j)
  1384. {
  1385. case 0:
  1386. ICell cell = irow5.CreateCell(j);
  1387. cell.SetCellValue(complaints[complaints.Count -1].area );
  1388. cell.CellStyle = cellStylebt;
  1389. sheet.AddMergedRegion(new CellRangeAddress(7 + complaints.Count - 1, 7 + complaints.Count - 1, 0, 1));
  1390. break;
  1391. case 1:
  1392. ICell cel5 = irow5.CreateCell(j+1);
  1393. cel5.SetCellValue(complaints[complaints.Count - 1].complaintnumber);
  1394. cel5.CellStyle = cellStylebt;
  1395. break;
  1396. case 2:
  1397. ICell cel6 = irow5.CreateCell(j+1);
  1398. cel6.SetCellValue(complaints[complaints.Count - 1].assignmentnumber);
  1399. cel6.CellStyle = cellStylebt;
  1400. break;
  1401. case 3:
  1402. ICell cel7 = irow5.CreateCell(j+1);
  1403. cel7.SetCellValue(complaints[complaints.Count - 1].assignmentrate);
  1404. cel7.CellStyle = cellStylebt;
  1405. break;
  1406. case 4:
  1407. ICell cel8 = irow5.CreateCell(j+1);
  1408. cel8.SetCellValue(complaints[complaints.Count - 1].pendingnumber);
  1409. cel8.CellStyle = cellStylebt;
  1410. break;
  1411. case 5:
  1412. ICell cel9 = irow5.CreateCell(j+1);
  1413. cel9.SetCellValue(complaints[complaints.Count - 1].processingnumber);
  1414. cel9.CellStyle = cellStylebt;
  1415. break;
  1416. case 6:
  1417. ICell cell0 = irow5.CreateCell(j+1);
  1418. cell0.SetCellValue(complaints[complaints.Count - 1].processednumber);
  1419. cell0.CellStyle = cellStylebt;
  1420. break;
  1421. case 7:
  1422. ICell cell11 = irow5.CreateCell(j+1);
  1423. cell11.SetCellValue(complaints[complaints.Count - 1].processedrate);
  1424. cell11.CellStyle = cellStylebt;
  1425. break;
  1426. case 8:
  1427. ICell cell12 = irow5.CreateCell(j+1);
  1428. cell12.SetCellValue(complaints[complaints.Count - 1].documentaryrate);
  1429. cell12.CellStyle = cellStylebt;
  1430. break;
  1431. }
  1432. }
  1433. //自适应列宽度
  1434. for (int i = 0; i < 10; i++)
  1435. {
  1436. //
  1437. sheet.SetColumnWidth(i, 12 * 400);
  1438. }
  1439. using (MemoryStream ms = new MemoryStream())
  1440. {
  1441. workbook.Write(ms);
  1442. HttpContext curContext = HttpContext.Current;
  1443. // 设置编码和附件格式
  1444. curContext.Response.ContentType = "application/vnd.ms-excel";
  1445. curContext.Response.ContentEncoding = Encoding.UTF8;
  1446. curContext.Response.Charset = "";
  1447. curContext.Response.AppendHeader("Content-Disposition",
  1448. "attachment;filename=" + HttpUtility.UrlEncode("各大区投诉处理情况" + ".xls", Encoding.UTF8));
  1449. curContext.Response.BinaryWrite(ms.GetBuffer());
  1450. workbook = null;
  1451. ms.Close();
  1452. ms.Dispose();
  1453. curContext.Response.End();
  1454. }
  1455. return "";
  1456. }
  1457. catch
  1458. {
  1459. return "导出失败!";
  1460. }
  1461. }
  1462. public string AftersaleToExcel(DateTime stime, DateTime etime, List<Aftersale> complaints, string maxarea, string minarea,int pendingnumber)
  1463. {
  1464. try
  1465. {
  1466. HSSFWorkbook workbook = new HSSFWorkbook();
  1467. ISheet sheet = workbook.CreateSheet("Sheet1");
  1468. ICellStyle cellStyle = workbook.CreateCellStyle();
  1469. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  1470. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  1471. cellfont.FontHeight = 300;
  1472. cellStyle.SetFont(cellfont);
  1473. ICellStyle cellStylebt = workbook.CreateCellStyle();
  1474. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  1475. cellStylebt.SetFont(cellfontbt);
  1476. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  1477. cellStylebt.Alignment = HorizontalAlignment.Center;
  1478. #region 标题
  1479. ICellStyle cellStylebt1 = workbook.CreateCellStyle();
  1480. NPOI.SS.UserModel.IFont cellfontbt1 = workbook.CreateFont();
  1481. cellfontbt1.Boldweight = (short)FontBoldWeight.Bold;
  1482. cellfontbt1.FontHeight = 500;
  1483. cellStylebt1.SetFont(cellfontbt1);
  1484. cellStylebt1.VerticalAlignment = VerticalAlignment.Center;
  1485. cellStylebt1.Alignment = HorizontalAlignment.Center;
  1486. IRow irow1 = sheet.CreateRow(0);
  1487. ICell cell1 = irow1.CreateCell(0);
  1488. cell1.SetCellValue("售后APP工单进度统计表(" + stime.Month + "月" + stime.Day + "-" + etime.Month + "月" + etime.Day + "日)");
  1489. cell1.CellStyle = cellStylebt1;
  1490. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 11));
  1491. #endregion
  1492. #region 说明
  1493. ICellStyle cellStylebt2 = workbook.CreateCellStyle();
  1494. NPOI.SS.UserModel.IFont cellfontbt2 = workbook.CreateFont();
  1495. cellfontbt2.Boldweight = (short)FontBoldWeight.Bold;
  1496. cellfontbt2.FontHeight = 300;
  1497. cellStylebt2.SetFont(cellfontbt2);
  1498. cellStylebt2.WrapText = true;//设置换行这个要先设置
  1499. cellStylebt2.VerticalAlignment = VerticalAlignment.Center;
  1500. cellStylebt2.Alignment = HorizontalAlignment.Center;
  1501. IRow irow2 = sheet.CreateRow(1);
  1502. ICell cell2 = irow2.CreateCell(0);
  1503. cell2.SetCellValue(" "+ stime.Year +"年" + stime.Month + "月" + stime.Day + "日-" + etime.Month + "月" + +etime.Day + "日共有售后APP工单" + complaints[complaints.Count - 1].total + "条," +
  1504. "已完结工单" + complaints[complaints.Count - 1].processednumber + "条,完结率" + complaints[complaints.Count - 1].processedrate + ",待分派工单" + complaints[complaints.Count - 1].assignmentnumber + "条,待处理工单" + pendingnumber + "条。19个大区中," + maxarea + "大区处理率最高," + minarea + "大区处理率最低。");
  1505. cell2.CellStyle = cellStylebt2;
  1506. sheet.AddMergedRegion(new CellRangeAddress(1, 5, 0, 11));
  1507. #endregion
  1508. #region
  1509. ICellStyle cellStylebt3 = workbook.CreateCellStyle();
  1510. NPOI.SS.UserModel.IFont cellfontbt3 = workbook.CreateFont();
  1511. cellfontbt3.Boldweight = (short)FontBoldWeight.Bold;
  1512. cellfontbt3.FontHeight = 200;
  1513. cellStylebt3.WrapText = true;//设置换行这个要先设置
  1514. cellStylebt3.SetFont(cellfontbt3);
  1515. cellStylebt3.VerticalAlignment = VerticalAlignment.Center;
  1516. cellStylebt3.Alignment = HorizontalAlignment.Center;
  1517. IRow irow3 = sheet.CreateRow(6);
  1518. IRow irow6 = sheet.CreateRow(7);
  1519. ICell title1 = irow3.CreateCell(0);
  1520. title1.SetCellValue("序号");
  1521. title1.CellStyle = cellStylebt3;
  1522. sheet.AddMergedRegion(new CellRangeAddress(6, 7, 0, 0));
  1523. ICell title12 = irow3.CreateCell(1);
  1524. title12.SetCellValue("大区");
  1525. title12.CellStyle = cellStylebt3;
  1526. sheet.AddMergedRegion(new CellRangeAddress(6, 7, 1, 1));
  1527. ICell title13 = irow3.CreateCell(2);
  1528. title13.SetCellValue("工单总数\n(条)");
  1529. title13.CellStyle = cellStylebt3;
  1530. sheet.AddMergedRegion(new CellRangeAddress(6, 7, 2, 2));
  1531. ICell title14= irow3.CreateCell(3);
  1532. title14.SetCellValue("办事处经理当日分派情况");
  1533. title14.CellStyle = cellStylebt3;
  1534. sheet.AddMergedRegion(new CellRangeAddress(6, 6, 3, 4));
  1535. ICell title15 = irow6.CreateCell(3);
  1536. title15.SetCellValue("起数(条)");
  1537. title15.CellStyle = cellStylebt3;
  1538. ICell title16 = irow6.CreateCell(4);
  1539. title16.SetCellValue("分派率");
  1540. title16.CellStyle = cellStylebt3;
  1541. ICell title17= irow3.CreateCell(5);
  1542. title17.SetCellValue("业务员当日接单情况");
  1543. title17.CellStyle = cellStylebt3;
  1544. sheet.AddMergedRegion(new CellRangeAddress(6, 6, 5, 6));
  1545. ICell title18 = irow6.CreateCell(5);
  1546. title18.SetCellValue("起数(条)");
  1547. title18.CellStyle = cellStylebt3;
  1548. ICell title19 = irow6.CreateCell(6);
  1549. title19.SetCellValue("接单率");
  1550. title19.CellStyle = cellStylebt3;
  1551. ICell title110 = irow3.CreateCell(7);
  1552. title110.SetCellValue("处理中工单(条)");
  1553. title110.CellStyle = cellStylebt3;
  1554. sheet.AddMergedRegion(new CellRangeAddress(6, 7, 7, 7));
  1555. ICell title111 = irow3.CreateCell(8);
  1556. title111.SetCellValue("已完结工单起数(条)");
  1557. title111.CellStyle = cellStylebt3;
  1558. sheet.AddMergedRegion(new CellRangeAddress(6, 7, 8, 8));
  1559. ICell title112 = irow3.CreateCell(9);
  1560. title112.SetCellValue("完结率");
  1561. title112.CellStyle = cellStylebt3;
  1562. sheet.AddMergedRegion(new CellRangeAddress(6, 7, 9, 9));
  1563. ICell title113 = irow3.CreateCell(10);
  1564. title113.SetCellValue("公关经理催单率");
  1565. title113.CellStyle = cellStylebt3;
  1566. sheet.AddMergedRegion(new CellRangeAddress(6, 7, 10, 10));
  1567. ICell title114 = irow3.CreateCell(11);
  1568. title114.SetCellValue("公关经理");
  1569. title114.CellStyle = cellStylebt3;
  1570. sheet.AddMergedRegion(new CellRangeAddress(6, 7, 11, 11));
  1571. #endregion
  1572. for (int i = 0; i < complaints.Count ; i++)
  1573. {
  1574. IRow irow4 = sheet.CreateRow(8 + i);
  1575. for (int j = 0; j < 13; j++)
  1576. {
  1577. switch (j)
  1578. {
  1579. case 0:
  1580. ICell cell3 = irow4.CreateCell(j);
  1581. cell3.SetCellValue(complaints[i].serialnumbe);
  1582. cell3.CellStyle = cellStylebt;
  1583. break;
  1584. case 1:
  1585. ICell cell4 = irow4.CreateCell(j);
  1586. cell4.SetCellValue(complaints[i].area);
  1587. cell4.CellStyle = cellStylebt;
  1588. break;
  1589. case 2:
  1590. ICell cel5 = irow4.CreateCell(j);
  1591. cel5.SetCellValue(complaints[i].total);
  1592. cel5.CellStyle = cellStylebt;
  1593. break;
  1594. case 3:
  1595. ICell cel6 = irow4.CreateCell(j);
  1596. cel6.SetCellValue(complaints[i].assignmentnumber);
  1597. cel6.CellStyle = cellStylebt;
  1598. break;
  1599. case 4:
  1600. ICell cel7 = irow4.CreateCell(j);
  1601. cel7.SetCellValue(complaints[i].assignmentrate);
  1602. cel7.CellStyle = cellStylebt;
  1603. break;
  1604. case 5:
  1605. ICell cel8 = irow4.CreateCell(j);
  1606. cel8.SetCellValue(complaints[i].receiving);
  1607. cel8.CellStyle = cellStylebt;
  1608. break;
  1609. case 6:
  1610. ICell cel9 = irow4.CreateCell(j);
  1611. cel9.SetCellValue(complaints[i].receivingrate);
  1612. cel9.CellStyle = cellStylebt;
  1613. break;
  1614. case 7:
  1615. ICell cell0 = irow4.CreateCell(j);
  1616. cell0.SetCellValue(complaints[i].processingnumber);
  1617. cell0.CellStyle = cellStylebt;
  1618. break;
  1619. case 8:
  1620. ICell cell11 = irow4.CreateCell(j);
  1621. cell11.SetCellValue(complaints[i].processednumber);
  1622. cell11.CellStyle = cellStylebt;
  1623. break;
  1624. case 9:
  1625. ICell cell12 = irow4.CreateCell(j);
  1626. cell12.SetCellValue(complaints[i].processedrate);
  1627. cell12.CellStyle = cellStylebt;
  1628. break;
  1629. case 10:
  1630. ICell cell13 = irow4.CreateCell(j);
  1631. cell13.SetCellValue(complaints[i].reminderrate);
  1632. cell13.CellStyle = cellStylebt;
  1633. break;
  1634. case 11:
  1635. ICell cell14 = irow4.CreateCell(j);
  1636. cell14.SetCellValue(complaints[i].name);
  1637. cell14.CellStyle = cellStylebt;
  1638. break;
  1639. }
  1640. }
  1641. }
  1642. //自适应列宽度
  1643. for (int i = 0; i < 10; i++)
  1644. {
  1645. //
  1646. sheet.SetColumnWidth(i, 12 * 400);
  1647. }
  1648. using (MemoryStream ms = new MemoryStream())
  1649. {
  1650. workbook.Write(ms);
  1651. HttpContext curContext = HttpContext.Current;
  1652. // 设置编码和附件格式
  1653. curContext.Response.ContentType = "application/vnd.ms-excel";
  1654. curContext.Response.ContentEncoding = Encoding.UTF8;
  1655. curContext.Response.Charset = "";
  1656. curContext.Response.AppendHeader("Content-Disposition",
  1657. "attachment;filename=" + HttpUtility.UrlEncode("售后APP工单进度统计表" + ".xls", Encoding.UTF8));
  1658. curContext.Response.BinaryWrite(ms.GetBuffer());
  1659. workbook = null;
  1660. ms.Close();
  1661. ms.Dispose();
  1662. curContext.Response.End();
  1663. }
  1664. return "";
  1665. }
  1666. catch
  1667. {
  1668. return "导出失败!";
  1669. }
  1670. }
  1671. /// <summary>
  1672. /// NPOI导出EXCEL
  1673. /// </summary>
  1674. /// <param name="dt">数据源</param>
  1675. /// <param name="title">导出文件的名称</param>
  1676. /// <param name="array">列宽数组</param>
  1677. public string NpoiExcel(DataTable table,string F_ParamValue)
  1678. {
  1679. try {
  1680. NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
  1681. NPOI.SS.UserModel.ISheet sheet = book.CreateSheet("Sheet1");
  1682. NPOI.SS.UserModel.IRow headerrow = sheet.CreateRow(0);
  1683. headerrow.Height = 30 * 20;
  1684. ICellStyle style = book.CreateCellStyle();
  1685. style.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1686. style.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1687. style.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1688. style.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1689. style.WrapText = true;
  1690. style.Alignment = HorizontalAlignment.Center;
  1691. style.VerticalAlignment = VerticalAlignment.Center;
  1692. List<Workorderpicture> workorderpictures = new List<Workorderpicture>();
  1693. foreach (DataRow dr in table.Rows)
  1694. {
  1695. Workorderpicture workorderpicture = new Workorderpicture();
  1696. workorderpicture.picture = new List<string>();
  1697. var FileUrl = GetFileData(dr["F_DealFile"].ToString (), F_ParamValue);
  1698. workorderpicture.workorder = dr["F_WorkOrderId"].ToString();
  1699. foreach (var dt in FileUrl )
  1700. {
  1701. workorderpicture.picture.Add(dt);
  1702. }
  1703. workorderpictures.Add(workorderpicture);
  1704. }
  1705. var count = workorderpictures.Select(x => x.picture.Count).Max();
  1706. //建立内容行
  1707. int iRowIndex = 0;
  1708. foreach (var it in workorderpictures)
  1709. {
  1710. int iCellIndex = 0;
  1711. IRow irow = sheet.CreateRow(iRowIndex );
  1712. for (int i = 0; i < count; i++)
  1713. {
  1714. string strsj = string.Empty;
  1715. if (iCellIndex == 0)
  1716. {
  1717. ICell cell = irow.CreateCell(iCellIndex);
  1718. cell.SetCellValue(it.workorder);
  1719. cell.CellStyle = style;
  1720. iCellIndex++;
  1721. }
  1722. else
  1723. {
  1724. if (it .picture .Count <= count)
  1725. {
  1726. ICell cell = irow.CreateCell(iCellIndex);
  1727. // cell.SetCellValue(it.workorder);
  1728. if (iCellIndex<= it.picture.Count)
  1729. {
  1730. bool n= AddPieChartAsync(book, sheet, it.picture[iCellIndex - 1], iRowIndex, iCellIndex, 1000);
  1731. if (!n )
  1732. cell.SetCellValue("图片不存在!");
  1733. }
  1734. else
  1735. cell.SetCellValue("");
  1736. cell.CellStyle = style;
  1737. iCellIndex++;
  1738. }
  1739. }
  1740. }
  1741. iRowIndex++;
  1742. }
  1743. //自适应列宽度
  1744. for (int i = 0; i < count; i++)
  1745. {
  1746. sheet.AutoSizeColumn(800);
  1747. }
  1748. using (MemoryStream ms = new MemoryStream())
  1749. {
  1750. book.Write(ms);
  1751. HttpContext curContext = HttpContext.Current;
  1752. // 设置编码和附件格式
  1753. curContext.Response.ContentType = "application/vnd.ms-excel";
  1754. curContext.Response.ContentEncoding = Encoding.UTF8;
  1755. curContext.Response.Charset = "";
  1756. curContext.Response.AppendHeader("Content-Disposition",
  1757. "attachment;filename=" + HttpUtility.UrlEncode("工单列表" + ".xls", Encoding.UTF8));
  1758. curContext.Response.BinaryWrite(ms.GetBuffer());
  1759. book = null;
  1760. ms.Close();
  1761. ms.Dispose();
  1762. curContext.Response.End();
  1763. }
  1764. return "";
  1765. }
  1766. catch (Exception e)
  1767. {
  1768. return "导出失败!" + e.Message;
  1769. }
  1770. }
  1771. public class Workorderpicture
  1772. {
  1773. public string workorder;
  1774. public List<string> picture;
  1775. }
  1776. public List<string> GetFileData(string ids, string prefix)
  1777. {
  1778. List<string> F_Url = new List<string>();
  1779. DataTable dt = new DataTable();
  1780. if (!string.IsNullOrEmpty(ids))
  1781. {
  1782. dt = DbHelperSQL.Query("select * from T_Sys_Accessories where F_Id in (" + ids + ")").Tables[0];
  1783. foreach (DataRow dr in dt.Rows)
  1784. {
  1785. string Url = prefix + dr["F_Url"].ToString();
  1786. F_Url.Add(Url);
  1787. }
  1788. }
  1789. return F_Url;
  1790. }
  1791. /// <summary>
  1792. /// 向sheet插入图片
  1793. /// </summary>
  1794. /// <param name="workbook">工作辅</param>
  1795. /// <param name="sheet">sheet页</param>
  1796. /// <param name="fileurl">图片地址</param>
  1797. /// <param name="row">当前行</param>
  1798. /// <param name="col">当前列</param>
  1799. /// <param name="RowHeight">行高</param>
  1800. public bool AddPieChartAsync(HSSFWorkbook workbook, ISheet sheet, string fileurl, int row, int col, short RowHeight)
  1801. {
  1802. WebClient myWebClient = new WebClient();
  1803. myWebClient.Credentials = CredentialCache.DefaultCredentials;
  1804. myWebClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
  1805. try
  1806. {
  1807. byte[] bytes = myWebClient.DownloadData(fileurl);
  1808. if (bytes != null)
  1809. {
  1810. int pictureIdx = workbook.AddPicture(bytes, NPOI.SS.UserModel.PictureType.JPEG);
  1811. HSSFPatriarch patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
  1812. HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 20, 20, col, row, col + 1, row + 1);
  1813. //##处理照片位置,【图片左上角为(col, row)第row+1行col+1列,右下角为( col +1, row +1)第 col +1+1行row +1+1列,宽为100,高为50
  1814. HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);
  1815. var index = sheet.GetRow(row) ?? sheet.CreateRow(row);
  1816. index.Height = RowHeight;
  1817. //pict.Resize(0);//这句话一定不要,这是用图片原始大小来显示
  1818. }
  1819. return true;
  1820. }
  1821. catch
  1822. {
  1823. return false ;
  1824. }
  1825. }
  1826. }
  1827. }