Brak opisu

NPOIHelper.cs 134KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960
  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. SetMergedRegionBorders(sheet, new CellRangeAddress(t + 1, t + count, 0, 0), workbook);
  880. ICell cell10 = irow5.CreateCell(7);
  881. cell10.SetCellValue("各\n质\n量\n问\n题\n占\n比\n");
  882. cell10.CellStyle = cellStylebt;
  883. cell10.CellStyle.WrapText = true;
  884. int structure = t + problem;
  885. if (istrue)
  886. {
  887. if (a >1)
  888. {
  889. structure = t + problem + a / 2;
  890. sheet.AddMergedRegion(new CellRangeAddress(t+1, structure, 7, 7));
  891. }
  892. else
  893. {
  894. structure = t + problem;
  895. sheet.AddMergedRegion(new CellRangeAddress(t+1, structure, 7, 7));
  896. }
  897. }
  898. else
  899. {
  900. sheet.AddMergedRegion(new CellRangeAddress(t + 1, structure, 7, 7));
  901. }
  902. IRow irow7 = sheet.CreateRow(structure+1);
  903. ICell cell11 = irow7.CreateCell(7);
  904. cell11.SetCellValue("结\n构\n占\n比\n");
  905. cell11.CellStyle = cellStylebt;
  906. cell11.CellStyle.WrapText = true;
  907. sheet.AddMergedRegion(new CellRangeAddress(structure + 1, t + count, 7, 7));
  908. int index = 0;
  909. if (Factory1 != null)
  910. {
  911. for (int i = 0; i < count; i++)
  912. {
  913. if (i ==0 || t +1+ i== structure+1)
  914. {
  915. if (Factory1.Count > i*2)
  916. {
  917. for (int j = 0; j < 6; j++)
  918. {
  919. string msg = ReturnMsg(j, i, Factory1);
  920. if (i == 0)
  921. {
  922. ICell cell8 = irow5.CreateCell(j + 1);
  923. cell8.SetCellValue(msg);
  924. cell8.CellStyle = cellStylebt;
  925. }
  926. else
  927. {
  928. ICell cell9 = irow7.CreateCell(j + 1);
  929. cell9.SetCellValue(msg);
  930. cell9.CellStyle = cellStylebt;
  931. }
  932. }
  933. }
  934. if (i ==0)
  935. {
  936. for (int z = 0; z < 3; z++)
  937. {
  938. switch (z)
  939. {
  940. case 0:
  941. ICell cell12 = irow5.CreateCell(8);
  942. cell12.SetCellValue(Factory2[0].name );
  943. cell12.CellStyle = cellStylebt;
  944. sheet.AddMergedRegion(new CellRangeAddress(t+1, t+1 , 8, 9));
  945. ICell cell22 = irow5.CreateCell(9);
  946. cell22.CellStyle = cellStylebt;
  947. break;
  948. case 1:
  949. ICell cell13 = irow5.CreateCell(10);
  950. cell13.SetCellValue(Factory2[0].number .ToString ());
  951. cell13.CellStyle = cellStylebt;
  952. break;
  953. case 2:
  954. ICell cell14 = irow5.CreateCell(11);
  955. cell14.SetCellValue(Factory2[0].proportion);
  956. cell14.CellStyle = cellStylebt;
  957. break;
  958. }
  959. }
  960. }
  961. else
  962. {
  963. ICell cell119 = irow7.CreateCell(0);
  964. cell119.CellStyle = cellStylebt;
  965. for (int z = 0; z < 3; z++)
  966. {
  967. switch (z)
  968. {
  969. case 0:
  970. ICell cell15 = irow7.CreateCell(8);
  971. cell15.SetCellValue(Factory3[0].name);
  972. cell15.CellStyle = cellStylebt;
  973. sheet.AddMergedRegion(new CellRangeAddress(structure+1, structure+1, 8, 9));
  974. ICell cell31 = irow7.CreateCell(9);
  975. cell15.CellStyle = cellStylebt;
  976. break;
  977. case 1:
  978. ICell cell16 = irow7.CreateCell(10);
  979. cell16.SetCellValue(Factory3[0].number.ToString());
  980. cell16.CellStyle = cellStylebt;
  981. break;
  982. case 2:
  983. ICell cell17= irow7.CreateCell(11);
  984. cell17.SetCellValue(Factory3[0].proportion);
  985. cell17.CellStyle = cellStylebt;
  986. break;
  987. }
  988. }
  989. index++;
  990. }
  991. }
  992. else
  993. {
  994. IRow irow6 = sheet.CreateRow(t+1 + i);
  995. if (Factory1.Count %2!=0)
  996. {
  997. if (i * 2+1 < Factory1.Count)
  998. {
  999. for (int j = 0; j < 6; j++)
  1000. {
  1001. string msg = ReturnMsg(j, i, Factory1);
  1002. ICell cell18 = irow6.CreateCell(j + 1);
  1003. cell18.SetCellValue(msg);
  1004. cell18.CellStyle = cellStylebt;
  1005. }
  1006. }
  1007. else if (i * 2 + 1 == Factory1.Count)
  1008. {
  1009. for (int j = 0; j < 3; j++)
  1010. {
  1011. string msg = ReturnMsg(j, i, Factory1);
  1012. ICell cell18 = irow6.CreateCell(j + 1);
  1013. cell18.SetCellValue(msg);
  1014. cell18.CellStyle = cellStylebt;
  1015. }
  1016. }
  1017. }
  1018. else
  1019. {
  1020. if (i * 2 < Factory1.Count)
  1021. {
  1022. for (int j = 0; j < 6; j++)
  1023. {
  1024. string msg = ReturnMsg(j, i, Factory1);
  1025. ICell cell18 = irow6.CreateCell(j + 1);
  1026. cell18.SetCellValue(msg);
  1027. cell18.CellStyle = cellStylebt;
  1028. }
  1029. }
  1030. }
  1031. int b = 0;
  1032. if (a >1)
  1033. {
  1034. b= (a / 2) + (0 == a % 2 ? 0 : 1);
  1035. }
  1036. if (i < Factory2.Count -1)
  1037. {
  1038. for (int z = 0; z < 3; z++)
  1039. {
  1040. switch (z)
  1041. {
  1042. case 0:
  1043. ICell cell8 = irow6.CreateCell(8);
  1044. cell8.SetCellValue(Factory2[i].name);
  1045. cell8.CellStyle = cellStylebt;
  1046. sheet.AddMergedRegion(new CellRangeAddress(t+1 + i, t+1 + i, 8, 9));
  1047. ICell cell22 = irow6.CreateCell(9);
  1048. cell22.CellStyle = cellStylebt;
  1049. break;
  1050. case 1:
  1051. ICell cell9 = irow6.CreateCell(10);
  1052. cell9.SetCellValue(Factory2[i].number.ToString());
  1053. cell9.CellStyle = cellStylebt;
  1054. break;
  1055. case 2:
  1056. ICell cell12 = irow6.CreateCell(11);
  1057. cell12.SetCellValue(Factory2[i].proportion);
  1058. cell12.CellStyle = cellStylebt;
  1059. break;
  1060. }
  1061. }
  1062. }
  1063. else if (i == Factory2.Count-1 )
  1064. {
  1065. for (int z = 0; z < 3; z++)
  1066. {
  1067. string msg = "";
  1068. switch (z)
  1069. {
  1070. case 0:
  1071. ICell cell8 = irow6.CreateCell(8);
  1072. cell8.SetCellValue(Factory2[i].name);
  1073. cell8.CellStyle = cellStylebt;
  1074. sheet.AddMergedRegion(new CellRangeAddress(t +1+ i, structure, 8, 9));
  1075. if (istrue )
  1076. {
  1077. ICell cell22 = irow6.CreateCell(9);
  1078. cell22.CellStyle = cellStylebt;
  1079. }
  1080. break;
  1081. case 1:
  1082. ICell cell9 = irow6.CreateCell(10);
  1083. cell9.SetCellValue(Factory2[i].number.ToString());
  1084. cell9.CellStyle = cellStylebt;
  1085. sheet.AddMergedRegion(new CellRangeAddress(t+1 + i, structure, 10, 10));
  1086. break;
  1087. case 2:
  1088. ICell cell12 = irow6.CreateCell(11);
  1089. cell12.SetCellValue(Factory2[i].proportion);
  1090. cell12.CellStyle = cellStylebt;
  1091. sheet.AddMergedRegion(new CellRangeAddress(t+1 + i, structure, 11, 11));
  1092. break;
  1093. }
  1094. }
  1095. }
  1096. if (t +1+ i>structure + 1&& t + 1 + i < structure+ Factory3.Count)
  1097. {
  1098. for (int z = 0; z < 3; z++)
  1099. {
  1100. switch (z)
  1101. {
  1102. case 0:
  1103. ICell cell8 = irow6.CreateCell(8);
  1104. cell8.SetCellValue(Factory3[index].name);
  1105. cell8.CellStyle = cellStylebt;
  1106. sheet.AddMergedRegion(new CellRangeAddress(t+1+i, t + 1 + i, 8, 9));
  1107. ICell cel27 = irow6.CreateCell(9);
  1108. cel27.CellStyle = cellStylebt;
  1109. break;
  1110. case 1:
  1111. ICell cell9 = irow6.CreateCell(10);
  1112. cell9.SetCellValue(Factory3[index].number.ToString());
  1113. cell9.CellStyle = cellStylebt;
  1114. break;
  1115. case 2:
  1116. ICell cell12 = irow6.CreateCell(11);
  1117. cell12.SetCellValue(Factory3[index].proportion);
  1118. cell12.CellStyle = cellStylebt;
  1119. break;
  1120. }
  1121. }
  1122. index++;
  1123. }
  1124. else if (t + 1 + i == structure + Factory3.Count)
  1125. {
  1126. for (int z = 0; z < 3; z++)
  1127. {
  1128. switch (z)
  1129. {
  1130. case 0:
  1131. ICell cell8 = irow6.CreateCell(8);
  1132. cell8.SetCellValue(Factory3[index].name);
  1133. cell8.CellStyle = cellStylebt;
  1134. sheet.AddMergedRegion(new CellRangeAddress(t + 1 + i, t + count, 8, 9));
  1135. SetMergedRegionBorders(sheet, new CellRangeAddress(t + 1 + i, t + 1 + i, 8, 9), workbook);
  1136. break;
  1137. case 1:
  1138. ICell cell9 = irow6.CreateCell(10);
  1139. cell9.SetCellValue(Factory3[index].number.ToString());
  1140. cell9.CellStyle = cellStylebt;
  1141. sheet.AddMergedRegion(new CellRangeAddress(t + 1 + i, t + count, 10, 10));
  1142. SetMergedRegionBorders(sheet, new CellRangeAddress(t + 1 + i, t + 1 + i, 10, 10), workbook);
  1143. break;
  1144. case 2:
  1145. ICell cell12 = irow6.CreateCell(11);
  1146. cell12.SetCellValue(Factory3[index].proportion);
  1147. cell12.CellStyle = cellStylebt;
  1148. sheet.AddMergedRegion(new CellRangeAddress(t + 1 + i, t + count, 11, 11));
  1149. SetMergedRegionBorders(sheet, new CellRangeAddress(t + 1 + i, t + 1 + i, 11, 11), workbook);
  1150. break;
  1151. }
  1152. }
  1153. }
  1154. if ( i == Factory2.Count)
  1155. {
  1156. ICell cell120 = irow6.CreateCell(7);
  1157. cell120.CellStyle = cellStylebt;
  1158. ICell cell124 = irow6.CreateCell(9);
  1159. cell124.CellStyle = cellStylebt;
  1160. ICell cell125 = irow6.CreateCell(10);
  1161. cell125.CellStyle = cellStylebt;
  1162. ICell cell126 = irow6.CreateCell(11);
  1163. cell126.CellStyle = cellStylebt;
  1164. }
  1165. if (!istrue )
  1166. {
  1167. if (i * 2>= Factory1.Count)
  1168. {
  1169. ICell cell119 = irow6.CreateCell(0);
  1170. cell119.CellStyle = cellStylebt;
  1171. }
  1172. if (i *2 +1>= Factory1.Count&i < Factory2.Count)
  1173. {
  1174. ICell cell119 = irow6.CreateCell(7);
  1175. cell119.CellStyle = cellStylebt;
  1176. }
  1177. if (i > Factory2.Count)
  1178. {
  1179. ICell cell119 = irow6.CreateCell(7);
  1180. cell119.CellStyle = cellStylebt;
  1181. }
  1182. if (i == count - 1)
  1183. {
  1184. ICell cell119 = irow6.CreateCell(1);
  1185. cell119.CellStyle = cellStylebt1;
  1186. ICell cell121 = irow6.CreateCell(2);
  1187. cell121.CellStyle = cellStylebt1;
  1188. ICell cell132 = irow6.CreateCell(3);
  1189. cell132.CellStyle = cellStylebt1;
  1190. ICell cell128 = irow6.CreateCell(4);
  1191. cell128.CellStyle = cellStylebt1;
  1192. ICell cell129 = irow6.CreateCell(5);
  1193. cell129.CellStyle = cellStylebt1;
  1194. ICell cell130 = irow6.CreateCell(6);
  1195. cell130.CellStyle = cellStylebt1;
  1196. ICell cell131 = irow6.CreateCell(9);
  1197. cell131.CellStyle = cellStylebt1;
  1198. }
  1199. }
  1200. else if (i == count - 1)
  1201. {
  1202. ICell cell119 = irow6.CreateCell(0);
  1203. cell119.CellStyle = cellStylebt;
  1204. ICell cell121 = irow6.CreateCell(7);
  1205. cell121.CellStyle = cellStylebt;
  1206. ICell cell122 = irow6.CreateCell(4);
  1207. cell122.CellStyle = cellStylebt;
  1208. ICell cell123 = irow6.CreateCell(5);
  1209. cell123.CellStyle = cellStylebt;
  1210. ICell cell124 = irow6.CreateCell(6);
  1211. cell124.CellStyle = cellStylebt;
  1212. sheet.AddMergedRegion(new CellRangeAddress(t + 1 + i, t + 1 + i, 4, 6));
  1213. // sheet.AddMergedRegion(new CellRangeAddress(t + 1 + i, t + 1 + i, 8, 11));
  1214. }
  1215. if (t + 1 + i >structure + Factory3.Count)
  1216. {
  1217. ICell cell132 = irow6.CreateCell(8);
  1218. cell132.CellStyle = cellStylebt;
  1219. ICell cell128 = irow6.CreateCell(9);
  1220. cell128.CellStyle = cellStylebt;
  1221. ICell cell129 = irow6.CreateCell(10);
  1222. cell129.CellStyle = cellStylebt;
  1223. ICell cell130 = irow6.CreateCell(11);
  1224. cell130.CellStyle = cellStylebt;
  1225. }
  1226. }
  1227. }
  1228. }
  1229. #endregion
  1230. //自适应列宽度
  1231. for (int i = 0; i < 12; i++)
  1232. {
  1233. // sheet.AutoSizeColumn(i);
  1234. sheet.SetColumnWidth(i, 15 * 256);
  1235. }
  1236. using (MemoryStream ms = new MemoryStream())
  1237. {
  1238. workbook.Write(ms);
  1239. HttpContext curContext = HttpContext.Current;
  1240. // 设置编码和附件格式
  1241. curContext.Response.ContentType = "application/vnd.ms-excel";
  1242. curContext.Response.ContentEncoding = Encoding.UTF8;
  1243. curContext.Response.Charset = "";
  1244. curContext.Response.AppendHeader("Content-Disposition",
  1245. "attachment;filename=" + HttpUtility.UrlEncode("投诉产品日期分布表" + ".xls", Encoding.UTF8));
  1246. curContext.Response.BinaryWrite(ms.GetBuffer());
  1247. workbook = null;
  1248. ms.Close();
  1249. ms.Dispose();
  1250. curContext.Response.End();
  1251. }
  1252. return "";
  1253. }
  1254. catch
  1255. {
  1256. return "导出失败!";
  1257. }
  1258. }
  1259. public string Complainthandling(DateTime stime, DateTime etime, List<Complaint> complaints,string maxarea,string minarea)
  1260. {
  1261. try
  1262. {
  1263. HSSFWorkbook workbook = new HSSFWorkbook();
  1264. ISheet sheet = workbook.CreateSheet("Sheet1");
  1265. ICellStyle cellStyle = workbook.CreateCellStyle();
  1266. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  1267. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  1268. cellfont.FontHeight = 300;
  1269. cellStyle.SetFont(cellfont);
  1270. ICellStyle cellStylebt = workbook.CreateCellStyle();
  1271. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  1272. cellStylebt.SetFont(cellfontbt);
  1273. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  1274. cellStylebt.Alignment = HorizontalAlignment.Center;
  1275. #region 标题
  1276. ICellStyle cellStylebt1 = workbook.CreateCellStyle();
  1277. NPOI.SS.UserModel.IFont cellfontbt1 = workbook.CreateFont();
  1278. cellfontbt1.Boldweight = (short)FontBoldWeight.Bold;
  1279. cellfontbt1.FontHeight = 500;
  1280. cellStylebt1.SetFont(cellfontbt1);
  1281. cellStylebt1.VerticalAlignment = VerticalAlignment.Center;
  1282. cellStylebt1.Alignment = HorizontalAlignment.Center;
  1283. IRow irow1 = sheet.CreateRow(0);
  1284. ICell cell1 = irow1.CreateCell(0);
  1285. cell1.SetCellValue("各大区投诉处理情况("+ stime.Month + "月"+ stime .Day + "-"+ etime .Day + "日)");
  1286. cell1.CellStyle = cellStylebt1;
  1287. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 10));
  1288. #endregion
  1289. #region 说明
  1290. ICellStyle cellStylebt2 = workbook.CreateCellStyle();
  1291. NPOI.SS.UserModel.IFont cellfontbt2 = workbook.CreateFont();
  1292. cellfontbt2.Boldweight = (short)FontBoldWeight.Bold;
  1293. cellfontbt2.FontHeight = 300;
  1294. cellStylebt2.SetFont(cellfontbt2);
  1295. cellStylebt2.WrapText = true;//设置换行这个要先设置
  1296. cellStylebt2.VerticalAlignment = VerticalAlignment.Center;
  1297. cellStylebt2.Alignment = HorizontalAlignment.Center;
  1298. IRow irow2 = sheet.CreateRow(1);
  1299. ICell cell2 = irow2.CreateCell(0);
  1300. cell2.SetCellValue(" " + stime.Month + "月" + stime.Day + "日-" + etime.Month + "月" + + etime.Day + "日共接400热线投诉"+ complaints [complaints.Count -1].complaintnumber + "起," +
  1301. "待分派投诉"+ 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 +"大区处理率最低。");
  1302. cell2.CellStyle = cellStylebt2;
  1303. sheet.AddMergedRegion(new CellRangeAddress(1, 5, 0, 10));
  1304. #endregion
  1305. ICellStyle cellStylebt3 = workbook.CreateCellStyle();
  1306. NPOI.SS.UserModel.IFont cellfontbt3 = workbook.CreateFont();
  1307. cellfontbt3.Boldweight = (short)FontBoldWeight.Bold;
  1308. cellfontbt3.FontHeight = 200;
  1309. cellStylebt3.SetFont(cellfontbt3);
  1310. cellStylebt3.VerticalAlignment = VerticalAlignment.Center;
  1311. cellStylebt3.Alignment = HorizontalAlignment.Center;
  1312. IRow irow3 = sheet.CreateRow(6);
  1313. string [] cols= { "序号", "大区", "投诉起数", "待分派投诉", "当日分派率", "待处理投诉", "处理中投诉", "已处理投诉", "处理率", "公关经理跟单率" };
  1314. int icolIndex = 0;
  1315. foreach (string dc in cols)
  1316. {
  1317. ICell cell = irow3.CreateCell(icolIndex);
  1318. cell.SetCellValue(dc);
  1319. cell.CellStyle = cellStylebt3;
  1320. icolIndex++;
  1321. }
  1322. for (int i=0;i < complaints.Count -1;i ++)
  1323. {
  1324. IRow irow4 = sheet.CreateRow(7+i );
  1325. for (int j=0; j < 10; j++)
  1326. {
  1327. switch (j )
  1328. {
  1329. case 0:
  1330. ICell cell = irow4.CreateCell(j);
  1331. cell.SetCellValue(complaints[i].serialnumbe);
  1332. cell.CellStyle = cellStylebt;
  1333. break;
  1334. case 1:
  1335. ICell cell4 = irow4.CreateCell(j);
  1336. cell4.SetCellValue(complaints[i].area);
  1337. cell4.CellStyle = cellStylebt;
  1338. break;
  1339. case 2:
  1340. ICell cel5 = irow4.CreateCell(j);
  1341. cel5.SetCellValue(complaints[i].complaintnumber);
  1342. cel5.CellStyle = cellStylebt;
  1343. break;
  1344. case 3:
  1345. ICell cel6 = irow4.CreateCell(j);
  1346. cel6.SetCellValue(complaints[i].assignmentnumber);
  1347. cel6.CellStyle = cellStylebt;
  1348. break;
  1349. case 4:
  1350. ICell cel7 = irow4.CreateCell(j);
  1351. cel7.SetCellValue(complaints[i].assignmentrate);
  1352. cel7.CellStyle = cellStylebt;
  1353. break;
  1354. case 5:
  1355. ICell cel8 = irow4.CreateCell(j);
  1356. cel8.SetCellValue(complaints[i].pendingnumber);
  1357. cel8.CellStyle = cellStylebt;
  1358. break;
  1359. case 6:
  1360. ICell cel9 = irow4.CreateCell(j);
  1361. cel9.SetCellValue(complaints[i].processingnumber);
  1362. cel9.CellStyle = cellStylebt;
  1363. break;
  1364. case 7:
  1365. ICell cell0 = irow4.CreateCell(j);
  1366. cell0.SetCellValue(complaints[i].processednumber);
  1367. cell0.CellStyle = cellStylebt;
  1368. break;
  1369. case 8:
  1370. ICell cell11 = irow4.CreateCell(j);
  1371. cell11.SetCellValue(complaints[i].processedrate);
  1372. cell11.CellStyle = cellStylebt;
  1373. break;
  1374. case 9:
  1375. ICell cell12 = irow4.CreateCell(j);
  1376. cell12.SetCellValue(complaints[i].documentaryrate);
  1377. cell12.CellStyle = cellStylebt;
  1378. break;
  1379. }
  1380. }
  1381. }
  1382. IRow irow5 = sheet.CreateRow(7+ complaints.Count - 1);
  1383. for (int j = 0; j < 9; j++)
  1384. {
  1385. switch (j)
  1386. {
  1387. case 0:
  1388. ICell cell = irow5.CreateCell(j);
  1389. cell.SetCellValue(complaints[complaints.Count -1].area );
  1390. cell.CellStyle = cellStylebt;
  1391. sheet.AddMergedRegion(new CellRangeAddress(7 + complaints.Count - 1, 7 + complaints.Count - 1, 0, 1));
  1392. break;
  1393. case 1:
  1394. ICell cel5 = irow5.CreateCell(j+1);
  1395. cel5.SetCellValue(complaints[complaints.Count - 1].complaintnumber);
  1396. cel5.CellStyle = cellStylebt;
  1397. break;
  1398. case 2:
  1399. ICell cel6 = irow5.CreateCell(j+1);
  1400. cel6.SetCellValue(complaints[complaints.Count - 1].assignmentnumber);
  1401. cel6.CellStyle = cellStylebt;
  1402. break;
  1403. case 3:
  1404. ICell cel7 = irow5.CreateCell(j+1);
  1405. cel7.SetCellValue(complaints[complaints.Count - 1].assignmentrate);
  1406. cel7.CellStyle = cellStylebt;
  1407. break;
  1408. case 4:
  1409. ICell cel8 = irow5.CreateCell(j+1);
  1410. cel8.SetCellValue(complaints[complaints.Count - 1].pendingnumber);
  1411. cel8.CellStyle = cellStylebt;
  1412. break;
  1413. case 5:
  1414. ICell cel9 = irow5.CreateCell(j+1);
  1415. cel9.SetCellValue(complaints[complaints.Count - 1].processingnumber);
  1416. cel9.CellStyle = cellStylebt;
  1417. break;
  1418. case 6:
  1419. ICell cell0 = irow5.CreateCell(j+1);
  1420. cell0.SetCellValue(complaints[complaints.Count - 1].processednumber);
  1421. cell0.CellStyle = cellStylebt;
  1422. break;
  1423. case 7:
  1424. ICell cell11 = irow5.CreateCell(j+1);
  1425. cell11.SetCellValue(complaints[complaints.Count - 1].processedrate);
  1426. cell11.CellStyle = cellStylebt;
  1427. break;
  1428. case 8:
  1429. ICell cell12 = irow5.CreateCell(j+1);
  1430. cell12.SetCellValue(complaints[complaints.Count - 1].documentaryrate);
  1431. cell12.CellStyle = cellStylebt;
  1432. break;
  1433. }
  1434. }
  1435. //自适应列宽度
  1436. for (int i = 0; i < 10; i++)
  1437. {
  1438. //
  1439. sheet.SetColumnWidth(i, 12 * 400);
  1440. }
  1441. using (MemoryStream ms = new MemoryStream())
  1442. {
  1443. workbook.Write(ms);
  1444. HttpContext curContext = HttpContext.Current;
  1445. // 设置编码和附件格式
  1446. curContext.Response.ContentType = "application/vnd.ms-excel";
  1447. curContext.Response.ContentEncoding = Encoding.UTF8;
  1448. curContext.Response.Charset = "";
  1449. curContext.Response.AppendHeader("Content-Disposition",
  1450. "attachment;filename=" + HttpUtility.UrlEncode("各大区投诉处理情况" + ".xls", Encoding.UTF8));
  1451. curContext.Response.BinaryWrite(ms.GetBuffer());
  1452. workbook = null;
  1453. ms.Close();
  1454. ms.Dispose();
  1455. curContext.Response.End();
  1456. }
  1457. return "";
  1458. }
  1459. catch
  1460. {
  1461. return "导出失败!";
  1462. }
  1463. }
  1464. public string AftersaleToExcel(DateTime stime, DateTime etime, List<Aftersale> complaints, string maxarea, string minarea,int pendingnumber,int Tobeassignment)
  1465. {
  1466. try
  1467. {
  1468. HSSFWorkbook workbook = new HSSFWorkbook();
  1469. ISheet sheet = workbook.CreateSheet("Sheet1");
  1470. ICellStyle cellStyle = workbook.CreateCellStyle();
  1471. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  1472. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  1473. cellfont.FontHeight = 300;
  1474. cellStyle.SetFont(cellfont);
  1475. ICellStyle cellStylebt = workbook.CreateCellStyle();
  1476. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  1477. cellStylebt.SetFont(cellfontbt);
  1478. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  1479. cellStylebt.Alignment = HorizontalAlignment.Center;
  1480. #region 标题
  1481. ICellStyle cellStylebt1 = workbook.CreateCellStyle();
  1482. NPOI.SS.UserModel.IFont cellfontbt1 = workbook.CreateFont();
  1483. cellfontbt1.Boldweight = (short)FontBoldWeight.Bold;
  1484. cellfontbt1.FontHeight = 500;
  1485. cellStylebt1.SetFont(cellfontbt1);
  1486. cellStylebt1.VerticalAlignment = VerticalAlignment.Center;
  1487. cellStylebt1.Alignment = HorizontalAlignment.Center;
  1488. IRow irow1 = sheet.CreateRow(0);
  1489. ICell cell1 = irow1.CreateCell(0);
  1490. cell1.SetCellValue("售后APP工单进度统计表(" + stime.Month + "月" + stime.Day + "-" + etime.Month + "月" + etime.Day + "日)");
  1491. cell1.CellStyle = cellStylebt1;
  1492. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 11));
  1493. #endregion
  1494. #region 说明
  1495. ICellStyle cellStylebt2 = workbook.CreateCellStyle();
  1496. NPOI.SS.UserModel.IFont cellfontbt2 = workbook.CreateFont();
  1497. cellfontbt2.Boldweight = (short)FontBoldWeight.Bold;
  1498. cellfontbt2.FontHeight = 300;
  1499. cellStylebt2.SetFont(cellfontbt2);
  1500. cellStylebt2.WrapText = true;//设置换行这个要先设置
  1501. cellStylebt2.VerticalAlignment = VerticalAlignment.Center;
  1502. cellStylebt2.Alignment = HorizontalAlignment.Center;
  1503. IRow irow2 = sheet.CreateRow(1);
  1504. ICell cell2 = irow2.CreateCell(0);
  1505. cell2.SetCellValue(" "+ stime.Year +"年" + stime.Month + "月" + stime.Day + "日-" + etime.Month + "月" + +etime.Day + "日共有售后APP工单" + complaints[complaints.Count - 1].total + "条," +
  1506. "已完结工单" + complaints[complaints.Count - 1].processednumber + "条,完结率" + complaints[complaints.Count - 1].processedrate + ",待分派工单" + complaints[complaints.Count - 1].assignmentnumber + "条,待处理工单" + pendingnumber + "条。19个大区中," + maxarea + "大区处理率最高," + minarea + "大区处理率最低。目前待分派"+ Tobeassignment+"条");
  1507. cell2.CellStyle = cellStylebt2;
  1508. sheet.AddMergedRegion(new CellRangeAddress(1, 5, 0, 11));
  1509. #endregion
  1510. #region
  1511. ICellStyle cellStylebt3 = workbook.CreateCellStyle();
  1512. NPOI.SS.UserModel.IFont cellfontbt3 = workbook.CreateFont();
  1513. cellfontbt3.Boldweight = (short)FontBoldWeight.Bold;
  1514. cellfontbt3.FontHeight = 200;
  1515. cellStylebt3.WrapText = true;//设置换行这个要先设置
  1516. cellStylebt3.SetFont(cellfontbt3);
  1517. cellStylebt3.VerticalAlignment = VerticalAlignment.Center;
  1518. cellStylebt3.Alignment = HorizontalAlignment.Center;
  1519. IRow irow3 = sheet.CreateRow(6);
  1520. IRow irow6 = sheet.CreateRow(7);
  1521. ICell title1 = irow3.CreateCell(0);
  1522. title1.SetCellValue("序号");
  1523. title1.CellStyle = cellStylebt3;
  1524. sheet.AddMergedRegion(new CellRangeAddress(6, 7, 0, 0));
  1525. ICell title12 = irow3.CreateCell(1);
  1526. title12.SetCellValue("大区");
  1527. title12.CellStyle = cellStylebt3;
  1528. sheet.AddMergedRegion(new CellRangeAddress(6, 7, 1, 1));
  1529. ICell title13 = irow3.CreateCell(2);
  1530. title13.SetCellValue("工单总数\n(条)");
  1531. title13.CellStyle = cellStylebt3;
  1532. sheet.AddMergedRegion(new CellRangeAddress(6, 7, 2, 2));
  1533. ICell title14= irow3.CreateCell(3);
  1534. title14.SetCellValue("办事处经理当日分派情况");
  1535. title14.CellStyle = cellStylebt3;
  1536. sheet.AddMergedRegion(new CellRangeAddress(6, 6, 3, 4));
  1537. ICell title15 = irow6.CreateCell(3);
  1538. title15.SetCellValue("起数(条)");
  1539. title15.CellStyle = cellStylebt3;
  1540. ICell title16 = irow6.CreateCell(4);
  1541. title16.SetCellValue("分派率");
  1542. title16.CellStyle = cellStylebt3;
  1543. ICell title17= irow3.CreateCell(5);
  1544. title17.SetCellValue("业务员当日接单情况");
  1545. title17.CellStyle = cellStylebt3;
  1546. sheet.AddMergedRegion(new CellRangeAddress(6, 6, 5, 6));
  1547. ICell title18 = irow6.CreateCell(5);
  1548. title18.SetCellValue("起数(条)");
  1549. title18.CellStyle = cellStylebt3;
  1550. ICell title19 = irow6.CreateCell(6);
  1551. title19.SetCellValue("接单率");
  1552. title19.CellStyle = cellStylebt3;
  1553. ICell title110 = irow3.CreateCell(7);
  1554. title110.SetCellValue("处理中工单(条)");
  1555. title110.CellStyle = cellStylebt3;
  1556. sheet.AddMergedRegion(new CellRangeAddress(6, 7, 7, 7));
  1557. ICell title111 = irow3.CreateCell(8);
  1558. title111.SetCellValue("已完结工单起数(条)");
  1559. title111.CellStyle = cellStylebt3;
  1560. sheet.AddMergedRegion(new CellRangeAddress(6, 7, 8, 8));
  1561. ICell title112 = irow3.CreateCell(9);
  1562. title112.SetCellValue("完结率");
  1563. title112.CellStyle = cellStylebt3;
  1564. sheet.AddMergedRegion(new CellRangeAddress(6, 7, 9, 9));
  1565. ICell title113 = irow3.CreateCell(10);
  1566. title113.SetCellValue("公关经理催单率");
  1567. title113.CellStyle = cellStylebt3;
  1568. sheet.AddMergedRegion(new CellRangeAddress(6, 7, 10, 10));
  1569. ICell title114 = irow3.CreateCell(11);
  1570. title114.SetCellValue("公关经理");
  1571. title114.CellStyle = cellStylebt3;
  1572. sheet.AddMergedRegion(new CellRangeAddress(6, 7, 11, 11));
  1573. #endregion
  1574. for (int i = 0; i < complaints.Count ; i++)
  1575. {
  1576. IRow irow4 = sheet.CreateRow(8 + i);
  1577. for (int j = 0; j < 13; j++)
  1578. {
  1579. switch (j)
  1580. {
  1581. case 0:
  1582. ICell cell3 = irow4.CreateCell(j);
  1583. cell3.SetCellValue(complaints[i].serialnumbe);
  1584. cell3.CellStyle = cellStylebt;
  1585. break;
  1586. case 1:
  1587. ICell cell4 = irow4.CreateCell(j);
  1588. cell4.SetCellValue(complaints[i].area);
  1589. cell4.CellStyle = cellStylebt;
  1590. break;
  1591. case 2:
  1592. ICell cel5 = irow4.CreateCell(j);
  1593. cel5.SetCellValue(complaints[i].total);
  1594. cel5.CellStyle = cellStylebt;
  1595. break;
  1596. case 3:
  1597. ICell cel6 = irow4.CreateCell(j);
  1598. cel6.SetCellValue(complaints[i].assignmentnumber);
  1599. cel6.CellStyle = cellStylebt;
  1600. break;
  1601. case 4:
  1602. ICell cel7 = irow4.CreateCell(j);
  1603. cel7.SetCellValue(complaints[i].assignmentrate);
  1604. cel7.CellStyle = cellStylebt;
  1605. break;
  1606. case 5:
  1607. ICell cel8 = irow4.CreateCell(j);
  1608. cel8.SetCellValue(complaints[i].receiving);
  1609. cel8.CellStyle = cellStylebt;
  1610. break;
  1611. case 6:
  1612. ICell cel9 = irow4.CreateCell(j);
  1613. cel9.SetCellValue(complaints[i].receivingrate);
  1614. cel9.CellStyle = cellStylebt;
  1615. break;
  1616. case 7:
  1617. ICell cell0 = irow4.CreateCell(j);
  1618. cell0.SetCellValue(complaints[i].processingnumber);
  1619. cell0.CellStyle = cellStylebt;
  1620. break;
  1621. case 8:
  1622. ICell cell11 = irow4.CreateCell(j);
  1623. cell11.SetCellValue(complaints[i].processednumber);
  1624. cell11.CellStyle = cellStylebt;
  1625. break;
  1626. case 9:
  1627. ICell cell12 = irow4.CreateCell(j);
  1628. cell12.SetCellValue(complaints[i].processedrate);
  1629. cell12.CellStyle = cellStylebt;
  1630. break;
  1631. case 10:
  1632. ICell cell13 = irow4.CreateCell(j);
  1633. cell13.SetCellValue(complaints[i].reminderrate);
  1634. cell13.CellStyle = cellStylebt;
  1635. break;
  1636. case 11:
  1637. ICell cell14 = irow4.CreateCell(j);
  1638. cell14.SetCellValue(complaints[i].name);
  1639. cell14.CellStyle = cellStylebt;
  1640. break;
  1641. }
  1642. }
  1643. }
  1644. //自适应列宽度
  1645. for (int i = 0; i < 10; i++)
  1646. {
  1647. //
  1648. sheet.SetColumnWidth(i, 12 * 400);
  1649. }
  1650. using (MemoryStream ms = new MemoryStream())
  1651. {
  1652. workbook.Write(ms);
  1653. HttpContext curContext = HttpContext.Current;
  1654. // 设置编码和附件格式
  1655. curContext.Response.ContentType = "application/vnd.ms-excel";
  1656. curContext.Response.ContentEncoding = Encoding.UTF8;
  1657. curContext.Response.Charset = "";
  1658. curContext.Response.AppendHeader("Content-Disposition",
  1659. "attachment;filename=" + HttpUtility.UrlEncode("售后APP工单进度统计表" + ".xls", Encoding.UTF8));
  1660. curContext.Response.BinaryWrite(ms.GetBuffer());
  1661. workbook = null;
  1662. ms.Close();
  1663. ms.Dispose();
  1664. curContext.Response.End();
  1665. }
  1666. return "";
  1667. }
  1668. catch
  1669. {
  1670. return "导出失败!";
  1671. }
  1672. }
  1673. public string TSDBToExcel( DataTable dt)
  1674. {
  1675. try
  1676. {
  1677. HSSFWorkbook workbook = new HSSFWorkbook();
  1678. ISheet sheet = workbook.CreateSheet("各生产单位市场投诉对比情况");
  1679. ICellStyle cellStyle = workbook.CreateCellStyle();
  1680. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  1681. // cellfont.Boldweight = (short)FontBoldWeight.Normal;
  1682. cellStyle.VerticalAlignment = VerticalAlignment.Center;
  1683. cellStyle.Alignment = HorizontalAlignment.Center;
  1684. // cellfont.FontHeightInPoints = 17;
  1685. cellfont.FontHeight = 300;
  1686. cellStyle.SetFont(cellfont);
  1687. ICellStyle cellStylebt = workbook.CreateCellStyle();
  1688. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  1689. cellStylebt.SetFont(cellfontbt);
  1690. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  1691. cellStylebt.Alignment = HorizontalAlignment.Center;
  1692. #region 标题
  1693. ICellStyle cellStylebt1 = workbook.CreateCellStyle();
  1694. NPOI.SS.UserModel.IFont cellfontbt1 = workbook.CreateFont();
  1695. // cellfontbt1.Boldweight = (short)FontBoldWeight.Bold;
  1696. cellfontbt1.FontHeight = 500;
  1697. cellStylebt1.SetFont(cellfontbt1);
  1698. cellStylebt1.VerticalAlignment = VerticalAlignment.Center;
  1699. cellStylebt1.Alignment = HorizontalAlignment.Center;
  1700. IRow irow1 = sheet.CreateRow(0);
  1701. ICell cell1 = irow1.CreateCell(0);
  1702. cell1.SetCellValue("各生产单位市场投诉对比情况");
  1703. cell1.CellStyle = cellStylebt1;
  1704. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 12));
  1705. #endregion
  1706. #region
  1707. ICellStyle cellStylebt3 = workbook.CreateCellStyle();
  1708. NPOI.SS.UserModel.IFont cellfontbt3 = workbook.CreateFont();
  1709. // cellfontbt3.Boldweight = (short)FontBoldWeight.Bold;
  1710. // cellfontbt3.FontHeight = 400;
  1711. cellfontbt3.FontHeightInPoints = 15;
  1712. cellStylebt3.WrapText = true;//设置换行这个要先设置
  1713. cellStylebt3.SetFont(cellfontbt3);
  1714. cellStylebt3.VerticalAlignment = VerticalAlignment.Center;
  1715. cellStylebt3.Alignment = HorizontalAlignment.Center;
  1716. IRow irow3 = sheet.CreateRow(1);
  1717. IRow irow6 = sheet.CreateRow(2);
  1718. ICell title1 = irow3.CreateCell(0);
  1719. title1.SetCellValue("生产厂家");
  1720. title1.CellStyle = cellStylebt3;
  1721. sheet.AddMergedRegion(new CellRangeAddress(1, 2, 0, 0));
  1722. ICell title12 = irow3.CreateCell(1);
  1723. title12.SetCellValue("总投诉");
  1724. title12.CellStyle = cellStylebt3;
  1725. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 1, 3));
  1726. ICell title13 = irow3.CreateCell(4);
  1727. title13.SetCellValue("杂质异物类(Z**)");
  1728. title13.CellStyle = cellStylebt3;
  1729. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 4, 6));
  1730. ICell title14 = irow3.CreateCell(7);
  1731. title14.SetCellValue("破袋发霉类(P)");
  1732. title14.CellStyle = cellStylebt3;
  1733. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 7, 9));
  1734. ICell title15 = irow3.CreateCell(10);
  1735. title15.SetCellValue("变质异味类(B)");
  1736. title15.CellStyle = cellStylebt3;
  1737. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 10, 12));
  1738. ICell title16 = irow6.CreateCell(1);
  1739. title16.SetCellValue("起数");
  1740. title16.CellStyle = cellStylebt3;
  1741. ICell title17 = irow6.CreateCell(2);
  1742. title17.SetCellValue("同期");
  1743. title17.CellStyle = cellStylebt3;
  1744. ICell title18 = irow6.CreateCell(3);
  1745. title18.SetCellValue("增幅%");
  1746. title18.CellStyle = cellStylebt3;
  1747. ICell title19 = irow6.CreateCell(4);
  1748. title19.SetCellValue("起数");
  1749. title19.CellStyle = cellStylebt3;
  1750. ICell title110 = irow6.CreateCell(5);
  1751. title110.SetCellValue("同期");
  1752. title110.CellStyle = cellStylebt3;
  1753. ICell title111 = irow6.CreateCell(6);
  1754. title111.SetCellValue("增幅%");
  1755. title111.CellStyle = cellStylebt3;
  1756. ICell title112 = irow6.CreateCell(7);
  1757. title112.SetCellValue("起数");
  1758. title112.CellStyle = cellStylebt3;
  1759. ICell title113 = irow6.CreateCell(8);
  1760. title113.SetCellValue("同期");
  1761. title113.CellStyle = cellStylebt3;
  1762. ICell title114 = irow6.CreateCell(9);
  1763. title114.SetCellValue("增幅%");
  1764. title114.CellStyle = cellStylebt3;
  1765. ICell title1122 = irow6.CreateCell(10);
  1766. title1122.SetCellValue("起数");
  1767. title1122.CellStyle = cellStylebt3;
  1768. ICell title1133 = irow6.CreateCell(11);
  1769. title1133.SetCellValue("同期");
  1770. title1133.CellStyle = cellStylebt3;
  1771. ICell title1144 = irow6.CreateCell(12);
  1772. title1144.SetCellValue("增幅%");
  1773. title1144.CellStyle = cellStylebt3;
  1774. #endregion
  1775. //建立内容行
  1776. int iRowIndex = 2;
  1777. foreach (DataRow dr in dt.Rows)
  1778. {
  1779. int iCellIndex = 0;
  1780. IRow irow = sheet.CreateRow(iRowIndex + 1);
  1781. for (int i = 0; i < dt.Columns.Count; i++)
  1782. {
  1783. string strsj = string.Empty;
  1784. if (dr[i] != null)
  1785. {
  1786. strsj = dr[i].ToString();
  1787. }
  1788. ICell cell = irow.CreateCell(iCellIndex);
  1789. cell.SetCellValue(strsj);
  1790. cell.CellStyle = cellStyle;
  1791. iCellIndex++;
  1792. }
  1793. iRowIndex++;
  1794. }
  1795. ////自适应列宽度
  1796. //for (int i = 0; i < 12; i++)
  1797. //{
  1798. // sheet.AutoSizeColumn(i);
  1799. //}
  1800. //自适应列宽度
  1801. for (int i = 0; i < 13; i++)
  1802. {
  1803. //
  1804. sheet.AutoSizeColumn(i);
  1805. // sheet.SetColumnWidth(i, 12 * 400);
  1806. }
  1807. using (MemoryStream ms = new MemoryStream())
  1808. {
  1809. workbook.Write(ms);
  1810. HttpContext curContext = HttpContext.Current;
  1811. // 设置编码和附件格式
  1812. curContext.Response.ContentType = "application/vnd.ms-excel";
  1813. curContext.Response.ContentEncoding = Encoding.UTF8;
  1814. curContext.Response.Charset = "";
  1815. curContext.Response.AppendHeader("Content-Disposition",
  1816. "attachment;filename=" + HttpUtility.UrlEncode("各生产单位市场投诉对比情况" + ".xls", Encoding.UTF8));
  1817. curContext.Response.BinaryWrite(ms.GetBuffer());
  1818. workbook = null;
  1819. ms.Close();
  1820. ms.Dispose();
  1821. curContext.Response.End();
  1822. }
  1823. return "";
  1824. }
  1825. catch
  1826. {
  1827. return "导出失败!";
  1828. }
  1829. }
  1830. public string Qualityproblem(DataTable dt, List<qualityproblem> jo,int size)
  1831. {
  1832. try
  1833. {
  1834. HSSFWorkbook workbook = new HSSFWorkbook();
  1835. ISheet sheet = workbook.CreateSheet("质量问题情况统计分析表");
  1836. ICellStyle cellStyle = workbook.CreateCellStyle();
  1837. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  1838. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  1839. cellfont.FontHeight = 300;
  1840. cellStyle.VerticalAlignment = VerticalAlignment.Center;
  1841. cellStyle.Alignment = HorizontalAlignment.Center;
  1842. cellStyle.SetFont(cellfont);
  1843. ICellStyle cellStylebt = workbook.CreateCellStyle();
  1844. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  1845. cellStylebt.SetFont(cellfontbt);
  1846. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  1847. cellStylebt.Alignment = HorizontalAlignment.Center;
  1848. #region 标题
  1849. ICellStyle cellStylebt1 = workbook.CreateCellStyle();
  1850. NPOI.SS.UserModel.IFont cellfontbt1 = workbook.CreateFont();
  1851. cellfontbt1.Boldweight = (short)FontBoldWeight.Bold;
  1852. cellfontbt1.FontHeight = 500;
  1853. cellStylebt1.SetFont(cellfontbt1);
  1854. cellStylebt1.VerticalAlignment = VerticalAlignment.Center;
  1855. cellStylebt1.Alignment = HorizontalAlignment.Center;
  1856. IRow irow1 = sheet.CreateRow(0);
  1857. ICell cell1 = irow1.CreateCell(0);
  1858. cell1.SetCellValue("质量问题情况统计分析表");
  1859. cell1.CellStyle = cellStylebt1;
  1860. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, size));
  1861. #endregion
  1862. #region 说明
  1863. ICellStyle cellStylebt2 = workbook.CreateCellStyle();
  1864. NPOI.SS.UserModel.IFont cellfontbt2 = workbook.CreateFont();
  1865. //cellfontbt2.Boldweight = (short)FontBoldWeight.Bold;
  1866. cellfontbt2.FontHeight = 300;
  1867. cellStylebt2.SetFont(cellfontbt2);
  1868. cellStylebt2.WrapText = true;//设置换行这个要先设置
  1869. cellStylebt2.VerticalAlignment = VerticalAlignment.Center;
  1870. cellStylebt2.Alignment = HorizontalAlignment.Center;
  1871. IRow irow2 = sheet.CreateRow(1);
  1872. IRow irow3 = sheet.CreateRow(2);
  1873. int index = 0;
  1874. foreach (var it in jo)
  1875. {
  1876. if (it.junior.Count==1&& it.junior[0].name == it.junior[0].type)
  1877. {
  1878. ICell cell2 = irow2.CreateCell(index);
  1879. cell2.SetCellValue(it.name);
  1880. cell2.CellStyle = cellStylebt2;
  1881. sheet.AddMergedRegion(new CellRangeAddress(1, 2, index, index));
  1882. }
  1883. else
  1884. {
  1885. ICell cell2 = irow2.CreateCell(index);
  1886. cell2.SetCellValue(it.name);
  1887. cell2.CellStyle = cellStylebt2;
  1888. sheet.AddMergedRegion(new CellRangeAddress(1, 1, index, index+ it.junior.Count-1));
  1889. for(int i=0;i < it.junior.Count;i++)
  1890. {
  1891. ICell cell3 = irow3.CreateCell(index);
  1892. cell3.SetCellValue(it.junior[i].name);
  1893. cell3.CellStyle = cellStylebt2;
  1894. index = index + 1;
  1895. }
  1896. index = index -1;
  1897. }
  1898. index++;
  1899. }
  1900. #endregion
  1901. DataView view = dt.DefaultView;
  1902. view.RowFilter = "生产厂家 <> '肉制品事业部'"; // 设置筛选条件
  1903. view.Sort = "序号 ASC"; // 设置排序条件
  1904. DataTable dt1 = view.ToTable();
  1905. // var dt1 = dt.Select("生产厂家 <> '合计'");
  1906. int iRowIndex = 2;
  1907. foreach (DataRow dr in dt1.Rows)
  1908. {
  1909. int iCellIndex = 0;
  1910. IRow irow = sheet.CreateRow(iRowIndex + 1);
  1911. for (int i = 0; i < dt1.Rows.Count; i++)
  1912. {
  1913. string strsj = string.Empty;
  1914. if (dr[i] != null)
  1915. {
  1916. strsj = dr[i].ToString();
  1917. }
  1918. ICell cell = irow.CreateCell(iCellIndex);
  1919. cell.SetCellValue(strsj);
  1920. cell.CellStyle = cellStyle;
  1921. iCellIndex++;
  1922. }
  1923. iRowIndex++;
  1924. }
  1925. IRow irow4 = sheet.CreateRow(iRowIndex );
  1926. var dt2 = dt.Select("生产厂家 ='肉制品事业部'");
  1927. int iCellIndex1 = 0;
  1928. for (int i = 0; i < dt.Columns.Count; i++)
  1929. {
  1930. if (i==0)
  1931. {
  1932. ICell cell = irow4.CreateCell(iCellIndex1);
  1933. cell.SetCellValue("肉制品事业部");
  1934. cell.CellStyle = cellStyle;
  1935. sheet.AddMergedRegion(new CellRangeAddress(iRowIndex, iRowIndex, 0,1));
  1936. }
  1937. else if (i==1)
  1938. {
  1939. iCellIndex1++;
  1940. continue;
  1941. }
  1942. else
  1943. {
  1944. string strsj = string.Empty;
  1945. if (dt2[0][i] != null)
  1946. {
  1947. strsj = dt2[0][i].ToString();
  1948. }
  1949. ICell cell = irow4.CreateCell(iCellIndex1);
  1950. cell.SetCellValue(strsj);
  1951. cell.CellStyle = cellStyle;
  1952. }
  1953. iCellIndex1++;
  1954. }
  1955. //自适应列宽度
  1956. for (int i = 0; i < size; i++)
  1957. {
  1958. //
  1959. sheet.AutoSizeColumn(i);
  1960. }
  1961. using (MemoryStream ms = new MemoryStream())
  1962. {
  1963. workbook.Write(ms);
  1964. HttpContext curContext = HttpContext.Current;
  1965. // 设置编码和附件格式
  1966. curContext.Response.ContentType = "application/vnd.ms-excel";
  1967. curContext.Response.ContentEncoding = Encoding.UTF8;
  1968. curContext.Response.Charset = "";
  1969. curContext.Response.AppendHeader("Content-Disposition",
  1970. "attachment;filename=" + HttpUtility.UrlEncode("质量问题情况统计分析表" + ".xls", Encoding.UTF8));
  1971. curContext.Response.BinaryWrite(ms.GetBuffer());
  1972. workbook = null;
  1973. ms.Close();
  1974. ms.Dispose();
  1975. curContext.Response.End();
  1976. }
  1977. return "";
  1978. }
  1979. catch
  1980. {
  1981. return "导出失败!";
  1982. }
  1983. }
  1984. public string QualityComplaints(List<qualityComplaints> data)
  1985. {
  1986. try
  1987. {
  1988. HSSFWorkbook workbook = new HSSFWorkbook();
  1989. ISheet sheet = workbook.CreateSheet("年度质量投诉问题情况对比");
  1990. ICellStyle cellStyle = workbook.CreateCellStyle();
  1991. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  1992. // cellfont.Boldweight = (short)FontBoldWeight.Normal;
  1993. cellStyle.VerticalAlignment = VerticalAlignment.Center;
  1994. cellStyle.Alignment = HorizontalAlignment.Center;
  1995. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1996. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1997. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1998. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1999. cellStyle.BorderDiagonalLineStyle = BorderStyle.Thin;
  2000. // cellfont.FontHeightInPoints = 17;
  2001. cellfont.FontHeight = 300;
  2002. cellStyle.SetFont(cellfont);
  2003. ICellStyle cellStylebt = workbook.CreateCellStyle();
  2004. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  2005. cellStylebt.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2006. cellStylebt.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2007. cellStylebt.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2008. cellStylebt.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2009. cellStylebt.BorderDiagonalLineStyle = BorderStyle.Thin;
  2010. cellStylebt.SetFont(cellfontbt);
  2011. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  2012. cellStylebt.Alignment = HorizontalAlignment.Center;
  2013. //ICellStyle frame = workbook.CreateCellStyle();
  2014. //frame.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2015. //frame.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2016. //frame.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2017. //frame.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2018. #region 标题
  2019. ICellStyle cellStylebt1 = workbook.CreateCellStyle();
  2020. NPOI.SS.UserModel.IFont cellfontbt1 = workbook.CreateFont();
  2021. // cellfontbt1.Boldweight = (short)FontBoldWeight.Bold;
  2022. cellfontbt1.FontHeight = 500;
  2023. cellStylebt1.SetFont(cellfontbt1);
  2024. cellStylebt1.VerticalAlignment = VerticalAlignment.Center;
  2025. cellStylebt1.Alignment = HorizontalAlignment.Center;
  2026. cellStylebt1.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2027. cellStylebt1.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2028. cellStylebt1.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2029. cellStylebt1.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2030. cellStylebt1.BorderDiagonalLineStyle = BorderStyle.Thin;
  2031. IRow irow1 = sheet.CreateRow(0);
  2032. ICell cell1 = irow1.CreateCell(0);
  2033. cell1.SetCellValue("年度质量投诉问题情况对比");
  2034. cell1.CellStyle = cellStylebt1;
  2035. CellRangeAddress region = new CellRangeAddress(0, 0, 0, 16);
  2036. sheet.AddMergedRegion(region);
  2037. SetMergedRegionBorders(sheet, region, workbook);
  2038. #endregion
  2039. ICellStyle style13 = workbook.CreateCellStyle();
  2040. style13.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2041. style13.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2042. style13.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2043. style13.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2044. style13.BorderDiagonalLineStyle = BorderStyle.Thin;
  2045. style13.BorderDiagonal = BorderDiagonal.Backward;
  2046. style13.BorderDiagonalColor = IndexedColors.Black.Index;
  2047. string sb = " 项目\n时间";
  2048. IRow irow2 = sheet.CreateRow(1);
  2049. ICell cell2 = irow2.CreateCell(0);
  2050. cell2.SetCellValue(sb);
  2051. cell2.CellStyle = cellStylebt;
  2052. style13.WrapText = true;
  2053. irow2.GetCell(0).CellStyle = style13;
  2054. sheet.AddMergedRegion(new CellRangeAddress(1, 2, 0, 1));
  2055. SetMergedRegionBorders(sheet, new CellRangeAddress(1, 2, 0, 1), workbook);
  2056. #region
  2057. ICellStyle cellStylebt3 = workbook.CreateCellStyle();
  2058. NPOI.SS.UserModel.IFont cellfontbt3 = workbook.CreateFont();
  2059. cellfontbt3.FontHeightInPoints = 15;
  2060. cellStylebt3.WrapText = true;//设置换行这个要先设置
  2061. cellStylebt3.SetFont(cellfontbt3);
  2062. cellStylebt3.VerticalAlignment = VerticalAlignment.Center;
  2063. cellStylebt3.Alignment = HorizontalAlignment.Center;
  2064. cellStylebt3.Alignment = HorizontalAlignment.Center;
  2065. cellStylebt3.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2066. cellStylebt3.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2067. cellStylebt3.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2068. cellStylebt3.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2069. cellStylebt3.BorderDiagonalLineStyle = BorderStyle.Thin;
  2070. IRow irow6 = sheet.CreateRow(2);
  2071. ICell title1 = irow2.CreateCell(2);
  2072. title1.SetCellValue("总投诉");
  2073. title1.CellStyle = cellStylebt3;
  2074. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 2, 4));
  2075. SetMergedRegionBorders(sheet, new CellRangeAddress(1, 1, 2, 4), workbook);
  2076. ICell title13 = irow2.CreateCell(5);
  2077. title13.SetCellValue("破袋发霉类(P)");
  2078. title13.CellStyle = cellStylebt3;
  2079. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 5, 8));
  2080. SetMergedRegionBorders(sheet, new CellRangeAddress(1, 1, 5, 8), workbook);
  2081. ICell title14 = irow2.CreateCell(9);
  2082. title14.SetCellValue("杂质异物类(Z**)");
  2083. title14.CellStyle = cellStylebt3;
  2084. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 9, 12));
  2085. SetMergedRegionBorders(sheet, new CellRangeAddress(1, 1, 9, 12), workbook);
  2086. ICell title15 = irow2.CreateCell(13);
  2087. title15.SetCellValue("变质异味类(B)");
  2088. title15.CellStyle = cellStylebt3;
  2089. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 13, 16));
  2090. SetMergedRegionBorders(sheet, new CellRangeAddress(1, 1, 13, 16), workbook);
  2091. ICell title16 = irow6.CreateCell(2);
  2092. title16.SetCellValue("起数");
  2093. title16.CellStyle = cellStylebt3;
  2094. ICell title17 = irow6.CreateCell(3);
  2095. title17.SetCellValue("同期");
  2096. title17.CellStyle = cellStylebt3;
  2097. ICell title18 = irow6.CreateCell(4);
  2098. title18.SetCellValue("增幅");
  2099. title18.CellStyle = cellStylebt3;
  2100. ICell title19 = irow6.CreateCell(5);
  2101. title19.SetCellValue("起数");
  2102. title19.CellStyle = cellStylebt3;
  2103. ICell title115 = irow6.CreateCell(6);
  2104. title115.SetCellValue("占比");
  2105. title115.CellStyle = cellStylebt3;
  2106. ICell title110 = irow6.CreateCell(7);
  2107. title110.SetCellValue("同期");
  2108. title110.CellStyle = cellStylebt3;
  2109. ICell title111 = irow6.CreateCell(8);
  2110. title111.SetCellValue("增幅");
  2111. title111.CellStyle = cellStylebt3;
  2112. ICell title112 = irow6.CreateCell(9);
  2113. title112.SetCellValue("起数");
  2114. title112.CellStyle = cellStylebt3;
  2115. ICell title116 = irow6.CreateCell(10);
  2116. title116.SetCellValue("占比");
  2117. title116.CellStyle = cellStylebt3;
  2118. ICell title113 = irow6.CreateCell(11);
  2119. title113.SetCellValue("同期");
  2120. title113.CellStyle = cellStylebt3;
  2121. ICell title114 = irow6.CreateCell(12);
  2122. title114.SetCellValue("增幅");
  2123. title114.CellStyle = cellStylebt3;
  2124. ICell title1122 = irow6.CreateCell(13);
  2125. title1122.SetCellValue("起数");
  2126. title1122.CellStyle = cellStylebt3;
  2127. ICell title117 = irow6.CreateCell(14);
  2128. title117.SetCellValue("占比");
  2129. title117.CellStyle = cellStylebt3;
  2130. ICell title1133 = irow6.CreateCell(15);
  2131. title1133.SetCellValue("同期");
  2132. title1133.CellStyle = cellStylebt3;
  2133. ICell title1144 = irow6.CreateCell(16);
  2134. title1144.SetCellValue("增幅");
  2135. title1144.CellStyle = cellStylebt3;
  2136. IRow irow3 = sheet.CreateRow(3);
  2137. string year = data.FirstOrDefault().year;
  2138. char specialChar = '\n';
  2139. StringBuilder sbyear = new StringBuilder();
  2140. for (int i = 0; i < year.Length; i++)
  2141. {
  2142. sbyear.Append(year[i]);
  2143. if (i < year.Length - 1)
  2144. {
  2145. sbyear.Append(specialChar);
  2146. }
  2147. }
  2148. string result = sbyear.ToString();
  2149. ICell cell3 = irow3.CreateCell(0);
  2150. cell3.SetCellValue(sbyear.ToString());
  2151. cell3.CellStyle = cellStyle;
  2152. cell3.CellStyle.WrapText = true;
  2153. sheet.AddMergedRegion(new CellRangeAddress(3, 21, 0, 0));
  2154. #endregion
  2155. //建立内容行
  2156. int iRowIndex = 3;
  2157. foreach(var it in data )
  2158. {
  2159. if (iRowIndex==3)
  2160. {
  2161. for(int i=1;i<17;i ++)
  2162. {
  2163. ICell cell = irow3.CreateCell(i);
  2164. cell.SetCellValue(GetQualityComplaints(it ,i ));
  2165. cell.CellStyle = cellStyle;
  2166. }
  2167. }
  2168. else
  2169. {
  2170. IRow irow = sheet.CreateRow(iRowIndex);
  2171. if (iRowIndex == data.Count + 2)
  2172. {
  2173. ICell cell = irow.CreateCell(0);
  2174. cell.CellStyle = cellStyle;
  2175. }
  2176. for (int i = 1; i < 17; i++)
  2177. {
  2178. ICell cell = irow.CreateCell(i);
  2179. cell.SetCellValue(GetQualityComplaints(it, i));
  2180. cell.CellStyle = cellStyle;
  2181. }
  2182. }
  2183. iRowIndex++;
  2184. }
  2185. for (int i = 0; i < 17; i++)
  2186. {
  2187. //
  2188. sheet.AutoSizeColumn(i);
  2189. // sheet.SetColumnWidth(i, 12 * 400);
  2190. }
  2191. using (MemoryStream ms = new MemoryStream())
  2192. {
  2193. workbook.Write(ms);
  2194. HttpContext curContext = HttpContext.Current;
  2195. // 设置编码和附件格式
  2196. curContext.Response.ContentType = "application/vnd.ms-excel";
  2197. curContext.Response.ContentEncoding = Encoding.UTF8;
  2198. curContext.Response.Charset = "";
  2199. curContext.Response.AppendHeader("Content-Disposition",
  2200. "attachment;filename=" + HttpUtility.UrlEncode("各生产单位市场投诉对比情况" + ".xls", Encoding.UTF8));
  2201. curContext.Response.BinaryWrite(ms.GetBuffer());
  2202. workbook = null;
  2203. ms.Close();
  2204. ms.Dispose();
  2205. curContext.Response.End();
  2206. }
  2207. return "";
  2208. }
  2209. catch
  2210. {
  2211. return "导出失败!";
  2212. }
  2213. }
  2214. public string DateOfManufacture(DataTable dt,DateTime time)
  2215. {
  2216. try
  2217. {
  2218. HSSFWorkbook workbook = new HSSFWorkbook();
  2219. ISheet sheet = workbook.CreateSheet("近三年投诉产品生产日期分布表");
  2220. ICellStyle cellStyle = workbook.CreateCellStyle();
  2221. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  2222. // cellfont.Boldweight = (short)FontBoldWeight.Normal;
  2223. cellStyle.VerticalAlignment = VerticalAlignment.Center;
  2224. cellStyle.Alignment = HorizontalAlignment.Center;
  2225. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2226. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2227. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2228. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2229. cellStyle.BorderDiagonalLineStyle = BorderStyle.Thin;
  2230. // cellfont.FontHeightInPoints = 17;
  2231. cellfont.FontHeight = 300;
  2232. cellStyle.SetFont(cellfont);
  2233. ICellStyle cellStylebt = workbook.CreateCellStyle();
  2234. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  2235. cellStylebt.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2236. cellStylebt.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2237. cellStylebt.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2238. cellStylebt.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2239. cellStylebt.BorderDiagonalLineStyle = BorderStyle.Thin;
  2240. cellStylebt.SetFont(cellfontbt);
  2241. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  2242. cellStylebt.Alignment = HorizontalAlignment.Center;
  2243. //ICellStyle frame = workbook.CreateCellStyle();
  2244. //frame.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2245. //frame.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2246. //frame.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2247. //frame.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2248. #region 标题
  2249. ICellStyle cellStylebt1 = workbook.CreateCellStyle();
  2250. NPOI.SS.UserModel.IFont cellfontbt1 = workbook.CreateFont();
  2251. // cellfontbt1.Boldweight = (short)FontBoldWeight.Bold;
  2252. cellfontbt1.FontHeight = 500;
  2253. cellStylebt1.SetFont(cellfontbt1);
  2254. cellStylebt1.VerticalAlignment = VerticalAlignment.Center;
  2255. cellStylebt1.Alignment = HorizontalAlignment.Center;
  2256. cellStylebt1.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2257. cellStylebt1.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2258. cellStylebt1.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2259. cellStylebt1.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2260. cellStylebt1.BorderDiagonalLineStyle = BorderStyle.Thin;
  2261. IRow irow1 = sheet.CreateRow(0);
  2262. ICell cell1 = irow1.CreateCell(0);
  2263. cell1.SetCellValue("近三年投诉产品生产日期分布表");
  2264. cell1.CellStyle = cellStylebt1;
  2265. CellRangeAddress region = new CellRangeAddress(0, 0, 0, 37);
  2266. sheet.AddMergedRegion(region);
  2267. SetMergedRegionBorders(sheet, region, workbook);
  2268. #endregion
  2269. ICellStyle style13 = workbook.CreateCellStyle();
  2270. style13.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2271. style13.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2272. style13.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2273. style13.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2274. style13.BorderDiagonalLineStyle = BorderStyle.Thin;
  2275. style13.VerticalAlignment = VerticalAlignment.Center;
  2276. style13.Alignment = HorizontalAlignment.Left;
  2277. style13.BorderDiagonal = BorderDiagonal.Backward;
  2278. style13.BorderDiagonalColor = IndexedColors.Black.Index;
  2279. string sb = " 投诉月份\n生产月份";
  2280. IRow irow2 = sheet.CreateRow(1);
  2281. ICell cell2 = irow2.CreateCell(0);
  2282. cell2.SetCellValue(sb);
  2283. cell2.CellStyle = cellStylebt;
  2284. style13.WrapText = true;
  2285. irow2.GetCell(0).CellStyle = style13;
  2286. sheet.AddMergedRegion(new CellRangeAddress(1, 2, 0, 0));
  2287. SetMergedRegionBorders(sheet, new CellRangeAddress(1, 2, 0, 0), workbook);
  2288. ICellStyle cellStylebt3 = workbook.CreateCellStyle();
  2289. NPOI.SS.UserModel.IFont cellfontbt3 = workbook.CreateFont();
  2290. cellfontbt3.FontHeightInPoints = 15;
  2291. cellStylebt3.WrapText = true;//设置换行这个要先设置
  2292. cellStylebt3.SetFont(cellfontbt3);
  2293. cellStylebt3.VerticalAlignment = VerticalAlignment.Center;
  2294. cellStylebt3.Alignment = HorizontalAlignment.Center;
  2295. cellStylebt3.Alignment = HorizontalAlignment.Center;
  2296. cellStylebt3.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2297. cellStylebt3.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2298. cellStylebt3.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2299. cellStylebt3.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2300. cellStylebt3.BorderDiagonalLineStyle = BorderStyle.Thin;
  2301. IRow irow6 = sheet.CreateRow(2);
  2302. ICell title1 = irow2.CreateCell(1);
  2303. title1.SetCellValue(time.AddYears(-3).Year.ToString()+"年");
  2304. title1.CellStyle = cellStylebt3;
  2305. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 1, 12));
  2306. SetMergedRegionBorders(sheet, new CellRangeAddress(1, 1, 1, 12), workbook);
  2307. ICell title13 = irow2.CreateCell(13);
  2308. title13.SetCellValue(time.AddYears(-2).Year.ToString() + "年");
  2309. title13.CellStyle = cellStylebt3;
  2310. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 13, 24));
  2311. SetMergedRegionBorders(sheet, new CellRangeAddress(1, 1, 13, 24), workbook);
  2312. ICell title14 = irow2.CreateCell(25);
  2313. title14.SetCellValue(time.AddYears(-1).Year.ToString() + "年");
  2314. title14.CellStyle = cellStylebt3;
  2315. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 25, 36));
  2316. SetMergedRegionBorders(sheet, new CellRangeAddress(1, 1, 25, 36), workbook);
  2317. ICell title15 = irow2.CreateCell(37);
  2318. title15.SetCellValue("投诉起数");
  2319. title15.CellStyle = cellStylebt3;
  2320. sheet.AddMergedRegion(new CellRangeAddress(1, 2, 37, 37));
  2321. SetMergedRegionBorders(sheet, new CellRangeAddress(1, 2, 37, 37), workbook);
  2322. DateTime start = DateTime.Parse(time.AddYears(-3).Year.ToString() + "-01-01 00:00:00");
  2323. DateTime end = DateTime.Parse(time.AddYears(-1).Year.ToString() + "-12-31 23:59:59");
  2324. int index = 1;
  2325. for (DateTime i = start; i <= end; i = i.AddMonths(1))
  2326. {
  2327. ICell title16 = irow6.CreateCell(index);
  2328. title16.SetCellValue(i.ToString("MM月"));
  2329. title16.CellStyle = cellStylebt3;
  2330. index++;
  2331. }
  2332. int iRowIndex = 2;
  2333. foreach (DataRow dr in dt.Rows)
  2334. {
  2335. int iCellIndex = 0;
  2336. IRow irow = sheet.CreateRow(iRowIndex + 1);
  2337. for (int i = 0; i < dt.Columns.Count; i++)
  2338. {
  2339. string strsj = string.Empty;
  2340. if (dr[i] != null)
  2341. {
  2342. strsj = dr[i].ToString();
  2343. }
  2344. ICell cell = irow.CreateCell(iCellIndex);
  2345. cell.SetCellValue(strsj);
  2346. cell.CellStyle = cellStyle;
  2347. iCellIndex++;
  2348. }
  2349. iRowIndex++;
  2350. }
  2351. for (int i = 0; i < 37; i++)
  2352. {
  2353. //
  2354. sheet.AutoSizeColumn(i);
  2355. // sheet.SetColumnWidth(i, 12 * 400);
  2356. }
  2357. using (MemoryStream ms = new MemoryStream())
  2358. {
  2359. workbook.Write(ms);
  2360. HttpContext curContext = HttpContext.Current;
  2361. // 设置编码和附件格式
  2362. curContext.Response.ContentType = "application/vnd.ms-excel";
  2363. curContext.Response.ContentEncoding = Encoding.UTF8;
  2364. curContext.Response.Charset = "";
  2365. curContext.Response.AppendHeader("Content-Disposition",
  2366. "attachment;filename=" + HttpUtility.UrlEncode("近三年投诉产品生产日期分布表" + ".xls", Encoding.UTF8));
  2367. curContext.Response.BinaryWrite(ms.GetBuffer());
  2368. workbook = null;
  2369. ms.Close();
  2370. ms.Dispose();
  2371. curContext.Response.End();
  2372. }
  2373. return "";
  2374. }
  2375. catch
  2376. {
  2377. return "导出失败!";
  2378. }
  2379. }
  2380. private void SetMergedRegionBorders(ISheet sheet, CellRangeAddress region, IWorkbook workbook)
  2381. {
  2382. int rowStart = region.FirstRow;
  2383. int rowEnd = region.LastRow;
  2384. int colStart = region.FirstColumn;
  2385. int colEnd = region.LastColumn;
  2386. // 设置顶部边框
  2387. for (int col = colStart; col <= colEnd; col++)
  2388. {
  2389. SetCellBorder(sheet, rowStart, col, "Top", BorderStyle.Thin, workbook);
  2390. }
  2391. // 设置底部边框
  2392. for (int col = colStart; col <= colEnd; col++)
  2393. {
  2394. SetCellBorder(sheet, rowEnd, col, "Bottom", BorderStyle.Thin, workbook);
  2395. }
  2396. // 设置左侧边框
  2397. for (int row = rowStart; row <= rowEnd; row++)
  2398. {
  2399. SetCellBorder(sheet, row, colStart, "Left", BorderStyle.Thin, workbook);
  2400. }
  2401. // 设置右侧边框
  2402. for (int row = rowStart; row <= rowEnd; row++)
  2403. {
  2404. SetCellBorder(sheet, row, colEnd, "Right", BorderStyle.Thin, workbook);
  2405. }
  2406. }
  2407. private void SetCellBorder(ISheet sheet, int rowIdx, int colIdx, string direction, BorderStyle style, IWorkbook workbook)
  2408. {
  2409. IRow row = sheet.GetRow(rowIdx) ?? sheet.CreateRow(rowIdx);
  2410. ICell cell = row.GetCell(colIdx) ?? row.CreateCell(colIdx);
  2411. ICellStyle originalStyle = cell.CellStyle;
  2412. ICellStyle newStyle = workbook.CreateCellStyle();
  2413. // 复制原有样式
  2414. if (originalStyle != null)
  2415. {
  2416. newStyle.CloneStyleFrom(originalStyle);
  2417. }
  2418. // 设置边框
  2419. switch (direction)
  2420. {
  2421. case "Top":
  2422. newStyle.BorderTop = style;
  2423. break;
  2424. case "Bottom":
  2425. newStyle.BorderBottom = style;
  2426. break;
  2427. case "Left":
  2428. newStyle.BorderLeft = style;
  2429. break;
  2430. case "Right":
  2431. newStyle.BorderRight = style;
  2432. break;
  2433. }
  2434. // 应用新样式
  2435. cell.CellStyle = newStyle;
  2436. }
  2437. private string GetQualityComplaints(qualityComplaints data,int i)
  2438. {
  2439. object str = "";
  2440. switch(i )
  2441. {
  2442. case 1:
  2443. str = data.month;
  2444. break;
  2445. case 2:
  2446. str = data.Total.ToString();
  2447. break;
  2448. case 3:
  2449. str = data.Previous;
  2450. break;
  2451. case 4:
  2452. str = data.Percent;
  2453. break;
  2454. case 5:
  2455. str = data.P_Total;
  2456. break;
  2457. case 6:
  2458. str = data.P_Proportion;
  2459. break;
  2460. case 7:
  2461. str = data.P_Previous;
  2462. break;
  2463. case 8:
  2464. str = data.P_Percent;
  2465. break;
  2466. case 9:
  2467. str = data.Z_Total;
  2468. break;
  2469. case 10:
  2470. str = data.Z_Proportion;
  2471. break;
  2472. case 11:
  2473. str = data.Z_Previous;
  2474. break;
  2475. case 12:
  2476. str = data.Z_Percent;
  2477. break;
  2478. case 13:
  2479. str = data.B_Total;
  2480. break;
  2481. case 14:
  2482. str = data.B_Proportion;
  2483. break;
  2484. case 15:
  2485. str = data.B_Previous;
  2486. break;
  2487. case 16:
  2488. str = data.B_Percent;
  2489. break;
  2490. }
  2491. return str.ToString();
  2492. }
  2493. /// <summary>
  2494. /// NPOI导出EXCEL
  2495. /// </summary>
  2496. /// <param name="dt">数据源</param>
  2497. /// <param name="title">导出文件的名称</param>
  2498. /// <param name="array">列宽数组</param>
  2499. public string NpoiExcel(DataTable table,string F_ParamValue)
  2500. {
  2501. try {
  2502. NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
  2503. NPOI.SS.UserModel.ISheet sheet = book.CreateSheet("Sheet1");
  2504. NPOI.SS.UserModel.IRow headerrow = sheet.CreateRow(0);
  2505. headerrow.Height = 30 * 20;
  2506. ICellStyle style = book.CreateCellStyle();
  2507. style.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2508. style.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2509. style.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2510. style.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2511. style.WrapText = true;
  2512. style.Alignment = HorizontalAlignment.Center;
  2513. style.VerticalAlignment = VerticalAlignment.Center;
  2514. List<Workorderpicture> workorderpictures = new List<Workorderpicture>();
  2515. foreach (DataRow dr in table.Rows)
  2516. {
  2517. Workorderpicture workorderpicture = new Workorderpicture();
  2518. workorderpicture.picture = new List<string>();
  2519. var FileUrl = GetFileData(dr["F_DealFile"].ToString (), F_ParamValue);
  2520. workorderpicture.workorder = dr["F_WorkOrderId"].ToString();
  2521. foreach (var dt in FileUrl )
  2522. {
  2523. workorderpicture.picture.Add(dt);
  2524. }
  2525. workorderpictures.Add(workorderpicture);
  2526. }
  2527. var count = workorderpictures.Select(x => x.picture.Count).Max();
  2528. //建立内容行
  2529. int iRowIndex = 0;
  2530. foreach (var it in workorderpictures)
  2531. {
  2532. int iCellIndex = 0;
  2533. IRow irow = sheet.CreateRow(iRowIndex );
  2534. for (int i = 0; i < count; i++)
  2535. {
  2536. string strsj = string.Empty;
  2537. if (iCellIndex == 0)
  2538. {
  2539. ICell cell = irow.CreateCell(iCellIndex);
  2540. cell.SetCellValue(it.workorder);
  2541. cell.CellStyle = style;
  2542. iCellIndex++;
  2543. }
  2544. else
  2545. {
  2546. if (it .picture .Count <= count)
  2547. {
  2548. ICell cell = irow.CreateCell(iCellIndex);
  2549. // cell.SetCellValue(it.workorder);
  2550. if (iCellIndex<= it.picture.Count)
  2551. {
  2552. bool n= AddPieChartAsync(book, sheet, it.picture[iCellIndex - 1], iRowIndex, iCellIndex, 1000);
  2553. if (!n )
  2554. cell.SetCellValue("图片不存在!");
  2555. }
  2556. else
  2557. cell.SetCellValue("");
  2558. cell.CellStyle = style;
  2559. iCellIndex++;
  2560. }
  2561. }
  2562. }
  2563. iRowIndex++;
  2564. }
  2565. //自适应列宽度
  2566. for (int i = 0; i < count; i++)
  2567. {
  2568. sheet.AutoSizeColumn(800);
  2569. }
  2570. using (MemoryStream ms = new MemoryStream())
  2571. {
  2572. book.Write(ms);
  2573. HttpContext curContext = HttpContext.Current;
  2574. // 设置编码和附件格式
  2575. curContext.Response.ContentType = "application/vnd.ms-excel";
  2576. curContext.Response.ContentEncoding = Encoding.UTF8;
  2577. curContext.Response.Charset = "";
  2578. curContext.Response.AppendHeader("Content-Disposition",
  2579. "attachment;filename=" + HttpUtility.UrlEncode("工单列表" + ".xls", Encoding.UTF8));
  2580. curContext.Response.BinaryWrite(ms.GetBuffer());
  2581. book = null;
  2582. ms.Close();
  2583. ms.Dispose();
  2584. curContext.Response.End();
  2585. }
  2586. return "";
  2587. }
  2588. catch (Exception e)
  2589. {
  2590. LogFactory.GetLogger("导出").Error(e.ToJson());
  2591. return "导出失败!" + e.Message;
  2592. }
  2593. }
  2594. public class Workorderpicture
  2595. {
  2596. public string workorder;
  2597. public List<string> picture;
  2598. }
  2599. public List<string> GetFileData(string ids, string prefix)
  2600. {
  2601. List<string> F_Url = new List<string>();
  2602. DataTable dt = new DataTable();
  2603. if (!string.IsNullOrEmpty(ids))
  2604. {
  2605. dt = DbHelperSQL.Query("select * from T_Sys_Accessories where F_Id in (" + ids + ")").Tables[0];
  2606. foreach (DataRow dr in dt.Rows)
  2607. {
  2608. string Url = prefix + dr["F_Url"].ToString();
  2609. F_Url.Add(Url);
  2610. }
  2611. }
  2612. return F_Url;
  2613. }
  2614. /// <summary>
  2615. /// 向sheet插入图片
  2616. /// </summary>
  2617. /// <param name="workbook">工作辅</param>
  2618. /// <param name="sheet">sheet页</param>
  2619. /// <param name="fileurl">图片地址</param>
  2620. /// <param name="row">当前行</param>
  2621. /// <param name="col">当前列</param>
  2622. /// <param name="RowHeight">行高</param>
  2623. public bool AddPieChartAsync(HSSFWorkbook workbook, ISheet sheet, string fileurl, int row, int col, short RowHeight)
  2624. {
  2625. WebClient myWebClient = new WebClient();
  2626. myWebClient.Credentials = CredentialCache.DefaultCredentials;
  2627. myWebClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
  2628. try
  2629. {
  2630. byte[] bytes = myWebClient.DownloadData(fileurl);
  2631. if (bytes != null)
  2632. {
  2633. int pictureIdx = workbook.AddPicture(bytes, NPOI.SS.UserModel.PictureType.JPEG);
  2634. HSSFPatriarch patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
  2635. HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 20, 20, col, row, col + 1, row + 1);
  2636. //##处理照片位置,【图片左上角为(col, row)第row+1行col+1列,右下角为( col +1, row +1)第 col +1+1行row +1+1列,宽为100,高为50
  2637. HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);
  2638. var index = sheet.GetRow(row) ?? sheet.CreateRow(row);
  2639. index.Height = RowHeight;
  2640. //pict.Resize(0);//这句话一定不要,这是用图片原始大小来显示
  2641. }
  2642. return true;
  2643. }
  2644. catch
  2645. {
  2646. return false ;
  2647. }
  2648. }
  2649. }
  2650. }