Sin descripción

NPOIHelper.cs 100KB

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