Nenhuma Descrição

NPOIHelper.cs 142KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Web;
  9. using NPOI.HSSF.UserModel;
  10. using NPOI.OpenXmlFormats.Wordprocessing;
  11. using NPOI.SS.UserModel;
  12. using NPOI.SS.Util;
  13. using NPOI.XSSF.UserModel;
  14. namespace CallCenter.Utility
  15. {
  16. public class NPOIHelper
  17. {
  18. private string _title;
  19. private string _sheetName;
  20. private string _filePath;
  21. /// <summary>
  22. /// 导出到Excel
  23. /// </summary>
  24. /// <param name="table"></param>
  25. /// <returns></returns>
  26. public bool ToExcel(DataTable table, string[] columns = null)
  27. {
  28. FileStream fs = new FileStream(this._filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  29. IWorkbook workBook = new HSSFWorkbook();
  30. if (string.IsNullOrWhiteSpace(this._sheetName))
  31. {
  32. this._sheetName = "sheet1";
  33. }
  34. ISheet sheet = workBook.CreateSheet(this._sheetName);
  35. //处理表格标题
  36. IRow row = sheet.CreateRow(0);
  37. row.CreateCell(0).SetCellValue(this._title);
  38. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, table.Columns.Count - 1));
  39. row.Height = 500;
  40. ICellStyle cellStyle = workBook.CreateCellStyle();
  41. IFont font = workBook.CreateFont();
  42. font.FontName = "微软雅黑";
  43. font.FontHeightInPoints = 17;
  44. cellStyle.SetFont(font);
  45. cellStyle.VerticalAlignment = VerticalAlignment.Center;
  46. cellStyle.Alignment = HorizontalAlignment.Center;
  47. row.Cells[0].CellStyle = cellStyle;
  48. //处理表格列头
  49. row = sheet.CreateRow(1);
  50. if (columns == null)
  51. {
  52. for (int i = 0; i < table.Columns.Count; i++)
  53. {
  54. row.CreateCell(i).SetCellValue(table.Columns[i].ColumnName);
  55. row.Height = 350;
  56. sheet.AutoSizeColumn(i);
  57. }
  58. }
  59. else
  60. {
  61. for (int i = 0; i < columns.Length; i++)
  62. {
  63. row.CreateCell(i).SetCellValue(columns[i]);
  64. row.Height = 350;
  65. sheet.AutoSizeColumn(i);
  66. }
  67. }
  68. //处理数据内容
  69. for (int i = 0; i < table.Rows.Count; i++)
  70. {
  71. row = sheet.CreateRow(2 + i);
  72. row.Height = 250;
  73. for (int j = 0; j < table.Columns.Count; j++)
  74. {
  75. row.CreateCell(j).SetCellValue(table.Rows[i][j].ToString());
  76. sheet.SetColumnWidth(j, 256 * 15);
  77. }
  78. }
  79. //写入数据流
  80. workBook.Write(fs);
  81. fs.Flush();
  82. fs.Close();
  83. return true;
  84. }
  85. /// <summary>
  86. /// 导出到Excel
  87. /// </summary>
  88. /// <param name="table"></param>
  89. /// <param name="title"></param>
  90. /// <param name="sheetName">空字符串或null的话默认sheet1</param>
  91. /// <param name="columns">自定义表格列头,默认null</param>
  92. /// <returns></returns>
  93. public bool ToExcel(DataTable table, string title, string sheetName, string filePath, string[] columns = null)
  94. {
  95. this._title = title;
  96. this._sheetName = sheetName;
  97. this._filePath = filePath;
  98. return ToExcel(table, columns);
  99. }
  100. /// <summary>
  101. /// 投诉产品日期分布表
  102. /// </summary>
  103. /// <param name="ds"></param>
  104. /// <returns></returns>
  105. public string SatisfiedToExcel(string Name, IOrderedEnumerable<deptSatisfiedReport> deptSatisfied, string[] cols = null)
  106. {
  107. try
  108. {
  109. //if (dt.Rows.Count > 0)
  110. //{
  111. HSSFWorkbook workbook = new HSSFWorkbook();
  112. ISheet sheet = workbook.CreateSheet("Sheet1");
  113. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  114. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  115. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  116. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  117. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  118. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  119. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  120. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  121. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  122. //字体
  123. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  124. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  125. headerfont.FontHeightInPoints = 12;
  126. HeadercellStyle.SetFont(headerfont);
  127. //用column name 作为列名
  128. int icolIndex = 0;
  129. IRow headerRow = sheet.CreateRow(0);
  130. foreach (string dc in cols)
  131. {
  132. ICell cell = headerRow.CreateCell(icolIndex);
  133. cell.SetCellValue(dc);
  134. cell.CellStyle = HeadercellStyle;
  135. icolIndex++;
  136. }
  137. ICellStyle cellStyle = workbook.CreateCellStyle();
  138. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  139. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  140. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  141. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  142. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  143. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  144. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  145. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  146. cellStyle.SetFont(cellfont);
  147. //建立内容行
  148. int iRowIndex = 0;
  149. string strsj = "";
  150. foreach (var dr in deptSatisfied )
  151. {
  152. IRow irow = sheet.CreateRow(iRowIndex + 1);
  153. for (int i = 0; i <5 ; i++)
  154. {
  155. switch (i )
  156. {
  157. case 0:
  158. strsj = dr.deptname;
  159. ICell cell = irow.CreateCell(i);
  160. cell.SetCellValue(strsj);
  161. cell.CellStyle = cellStyle;
  162. break;
  163. case 1:
  164. strsj = dr.count.ToString ();
  165. ICell cell1 = irow.CreateCell(i);
  166. cell1.SetCellValue(strsj);
  167. cell1.CellStyle = cellStyle;
  168. break;
  169. case 2:
  170. strsj = dr.satisfiedcount.ToString();
  171. ICell cell2 = irow.CreateCell(i);
  172. cell2.SetCellValue(strsj);
  173. cell2.CellStyle = cellStyle;
  174. break;
  175. case 3:
  176. strsj = dr.notsatisfiedcount.ToString();
  177. ICell cell3 = irow.CreateCell(i);
  178. cell3.SetCellValue(strsj);
  179. cell3.CellStyle = cellStyle;
  180. break;
  181. case 4:
  182. strsj = dr.satisfiedrate;
  183. ICell cell4 = irow.CreateCell(i);
  184. cell4.SetCellValue(strsj);
  185. cell4.CellStyle = cellStyle;
  186. break;
  187. }
  188. }
  189. iRowIndex++;
  190. }
  191. //自适应列宽度
  192. for (int i = 0; i < icolIndex; i++)
  193. {
  194. sheet.AutoSizeColumn(i);
  195. }
  196. using (MemoryStream ms = new MemoryStream())
  197. {
  198. workbook.Write(ms);
  199. HttpContext curContext = HttpContext.Current;
  200. // 设置编码和附件格式
  201. curContext.Response.ContentType = "application/vnd.ms-excel";
  202. curContext.Response.ContentEncoding = Encoding.UTF8;
  203. curContext.Response.Charset = "";
  204. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  205. curContext.Response.AppendHeader("Content-Disposition",
  206. "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
  207. curContext.Response.BinaryWrite(ms.GetBuffer());
  208. workbook = null;
  209. ms.Close();
  210. ms.Dispose();
  211. curContext.Response.End();
  212. }
  213. //}
  214. return "";
  215. }
  216. catch
  217. {
  218. return "导出失败!";
  219. }
  220. }
  221. /// <summary>
  222. /// 弹出下载框导出excel
  223. /// </summary>
  224. /// <param name="Name"></param>
  225. /// <param name="dt"></param>
  226. /// <returns></returns>
  227. public string ExportToExcel(string Name, DataTable dt, string[] cols = null, int issort = 0)
  228. {
  229. try
  230. {
  231. //if (dt.Rows.Count > 0)
  232. //{
  233. HSSFWorkbook workbook = new HSSFWorkbook();
  234. ISheet sheet = workbook.CreateSheet(Name);
  235. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  236. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  237. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  238. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  239. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  240. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  241. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  242. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  243. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  244. //字体
  245. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  246. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  247. headerfont.FontHeightInPoints = 12;
  248. HeadercellStyle.SetFont(headerfont);
  249. //用column name 作为列名
  250. int icolIndex = 0;
  251. IRow headerRow = sheet.CreateRow(0);
  252. if (issort == 1)
  253. {
  254. ICell cell = headerRow.CreateCell(icolIndex);
  255. cell.SetCellValue("序号");
  256. cell.CellStyle = HeadercellStyle;
  257. icolIndex++;
  258. }
  259. if (cols == null || (cols != null && cols.Length == 0))
  260. {
  261. foreach (DataColumn dc in dt.Columns)
  262. {
  263. ICell cell = headerRow.CreateCell(icolIndex);
  264. cell.SetCellValue(dc.ColumnName);
  265. cell.CellStyle = HeadercellStyle;
  266. icolIndex++;
  267. }
  268. }
  269. else
  270. {
  271. foreach (string dc in cols)
  272. {
  273. ICell cell = headerRow.CreateCell(icolIndex);
  274. cell.SetCellValue(dc);
  275. cell.CellStyle = HeadercellStyle;
  276. icolIndex++;
  277. }
  278. }
  279. ICellStyle cellStyle = workbook.CreateCellStyle();
  280. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  281. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  282. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  283. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  284. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  285. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  286. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  287. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  288. cellStyle.SetFont(cellfont);
  289. //建立内容行
  290. int iRowIndex = 0;
  291. foreach (DataRow dr in dt.Rows)
  292. {
  293. int iCellIndex = 0;
  294. IRow irow = sheet.CreateRow(iRowIndex + 1);
  295. if (issort == 1)
  296. {
  297. ICell cell = irow.CreateCell(iCellIndex);
  298. cell.SetCellValue(iRowIndex + 1);
  299. cell.CellStyle = cellStyle;
  300. iCellIndex++;
  301. }
  302. for (int i = 0; i < dt.Columns.Count; i++)
  303. {
  304. string strsj = string.Empty;
  305. if (dr[i] != null)
  306. {
  307. strsj = dr[i].ToString();
  308. }
  309. ICell cell = irow.CreateCell(iCellIndex);
  310. cell.SetCellValue(strsj);
  311. cell.CellStyle = cellStyle;
  312. iCellIndex++;
  313. }
  314. iRowIndex++;
  315. }
  316. //自适应列宽度
  317. for (int i = 0; i < icolIndex; i++)
  318. {
  319. sheet.AutoSizeColumn(i);
  320. }
  321. using (MemoryStream ms = new MemoryStream())
  322. {
  323. workbook.Write(ms);
  324. HttpContext curContext = HttpContext.Current;
  325. // 设置编码和附件格式
  326. curContext.Response.ContentType = "application/vnd.ms-excel";
  327. curContext.Response.ContentEncoding = Encoding.UTF8;
  328. curContext.Response.Charset = "";
  329. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  330. curContext.Response.AppendHeader("Content-Disposition",
  331. "attachment;filename=" + HttpUtility.UrlEncode(Name + "_导出文件_" + DateTime.Now.Ticks + ".xls", Encoding.UTF8));
  332. curContext.Response.BinaryWrite(ms.GetBuffer());
  333. workbook = null;
  334. ms.Close();
  335. ms.Dispose();
  336. curContext.Response.End();
  337. }
  338. //}
  339. return "";
  340. }
  341. catch
  342. {
  343. return "导出失败!";
  344. }
  345. }
  346. /// <summary>
  347. /// 导入excel转换为datatable
  348. /// </summary>
  349. /// <param name="upfile"></param>
  350. /// <param name="headrow"></param>
  351. /// <returns></returns>
  352. public DataTable ExcelToTable(HttpPostedFile upfile, int headrow)
  353. {
  354. DataTable dt = new DataTable();
  355. IWorkbook workbook = null;
  356. Stream stream = upfile.InputStream;
  357. string suffix = upfile.FileName.Substring(upfile.FileName.LastIndexOf(".") + 1).ToLower();
  358. if (suffix == "xlsx") // 2007版本
  359. {
  360. workbook = new XSSFWorkbook(stream);
  361. }
  362. else if (suffix == "xls") // 2003版本
  363. {
  364. workbook = new HSSFWorkbook(stream);
  365. }
  366. //获取excel的第一个sheet
  367. ISheet sheet = workbook.GetSheetAt(0);
  368. //获取sheet的第一行
  369. IRow headerRow = sheet.GetRow(headrow);
  370. //一行最后一个方格的编号 即总的列数
  371. int cellCount = headerRow.LastCellNum;
  372. //最后一列的标号 即总的行数
  373. int rowCount = sheet.LastRowNum;
  374. //列名
  375. for (int i = 0; i < cellCount; i++)
  376. {
  377. dt.Columns.Add(headerRow.GetCell(i).ToString());
  378. }
  379. for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++)
  380. {
  381. DataRow dr = dt.NewRow();
  382. IRow row = sheet.GetRow(i);
  383. for (int j = row.FirstCellNum; j < cellCount; j++)
  384. {
  385. if (row.GetCell(j) != null)
  386. {
  387. dr[j] = row.GetCell(j).ToString();
  388. }
  389. }
  390. dt.Rows.Add(dr);
  391. }
  392. sheet = null;
  393. workbook = null;
  394. return dt;
  395. }
  396. /// <summary>
  397. /// 导入excel转换为datatable
  398. /// </summary>
  399. /// <param name="upfile"></param>
  400. /// <param name="headrow"></param>
  401. /// <returns></returns>
  402. public DataTable ExcelToTable(HttpPostedFileBase upfile, int headrow)
  403. {
  404. DataTable dt = new DataTable();
  405. IWorkbook workbook = null;
  406. Stream stream = upfile.InputStream;
  407. string suffix = upfile.FileName.Substring(upfile.FileName.LastIndexOf(".") + 1).ToLower();
  408. if (suffix == "xlsx") // 2007版本
  409. {
  410. workbook = new XSSFWorkbook(stream);
  411. }
  412. else if (suffix == "xls") // 2003版本
  413. {
  414. workbook = new HSSFWorkbook(stream);
  415. }
  416. //获取excel的第一个sheet
  417. ISheet sheet = workbook.GetSheetAt(0);
  418. //获取sheet的第一行
  419. IRow headerRow = sheet.GetRow(headrow);
  420. //一行最后一个方格的编号 即总的列数
  421. int cellCount = headerRow.LastCellNum;
  422. //最后一列的标号 即总的行数
  423. int rowCount = sheet.LastRowNum;
  424. //列名
  425. for (int i = 0; i < cellCount; i++)
  426. {
  427. dt.Columns.Add(headerRow.GetCell(i).ToString());
  428. }
  429. for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++)
  430. {
  431. DataRow dr = dt.NewRow();
  432. IRow row = sheet.GetRow(i);
  433. for (int j = row.FirstCellNum; j < cellCount; j++)
  434. {
  435. if (row.GetCell(j) != null)
  436. {
  437. dr[j] = row.GetCell(j).ToString().Trim(' ').Trim('\t');
  438. }
  439. }
  440. dt.Rows.Add(dr);
  441. }
  442. sheet = null;
  443. workbook = null;
  444. return dt;
  445. }
  446. private string GetCellValue(DataRow dr,int index,int i )
  447. {
  448. string str = "";
  449. switch (i)
  450. {
  451. case 0:
  452. str = index.ToString();
  453. break;
  454. case 1:
  455. str = dr["deptname"].ToString();
  456. break;
  457. case 2:
  458. str = dr["Undertakers"].ToString();
  459. break;
  460. case 3:
  461. str = dr["Undertakersrate"].ToString();
  462. break;
  463. case 4:
  464. str = dr["Undertakersscore"].ToString();
  465. break;
  466. case 5:
  467. str = dr["overdue"].ToString();
  468. break;
  469. case 6:
  470. str = dr["overduerate"].ToString();
  471. break;
  472. case 7:
  473. str = dr["overduescore"].ToString();
  474. break;
  475. case 8:
  476. str = dr["unsuccessful"].ToString();
  477. break;
  478. case 9:
  479. str = dr["unsuccessfulrate"].ToString();
  480. break;
  481. case 10:
  482. str = dr["unsuccessfulscore"].ToString();
  483. break;
  484. case 11:
  485. str = dr["Chargeback"].ToString();
  486. break;
  487. case 12:
  488. str = dr["Chargebackrate"].ToString();
  489. break;
  490. case 13:
  491. str = dr["Chargebackscore"].ToString();
  492. break;
  493. case 14:
  494. str = dr["Comment"].ToString();
  495. break;
  496. case 15:
  497. str = dr["Oncedissatisfied"].ToString();
  498. break;
  499. case 16:
  500. str = dr["dissatisfied"].ToString();
  501. break;
  502. case 17:
  503. str = dr["satisfiedrate"].ToString();
  504. break;
  505. case 18:
  506. str = dr["satisfiedscore"].ToString();
  507. break;
  508. case 19:
  509. str = dr["total"].ToString();
  510. break;
  511. case 20:
  512. str = index.ToString();
  513. break;
  514. }
  515. return str;
  516. }
  517. public string ExcelMergerHotline(List <MergerHot.MergerHotSource> mergerHotSources
  518. )
  519. {
  520. return "";
  521. }
  522. /// <summary>
  523. /// 督办数据报表导出
  524. /// </summary>
  525. /// <param name="ds"></param>
  526. /// <returns></returns>
  527. public string DBExportToExcel(DataTable dt,string Name,string month,string starttime ,string endtime )
  528. {
  529. try
  530. {
  531. HSSFWorkbook workbook = new HSSFWorkbook();
  532. ISheet sheet = workbook.CreateSheet(Name);
  533. ICellStyle cellStyle = workbook.CreateCellStyle();
  534. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  535. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  536. cellStyle.SetFont(cellfont);
  537. ICellStyle cellStylebt = workbook.CreateCellStyle();
  538. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  539. cellfontbt.Boldweight = (short)FontBoldWeight.Bold;
  540. cellStylebt.SetFont(cellfontbt);
  541. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  542. cellStylebt.Alignment = HorizontalAlignment.Center;
  543. IRow irow1 = sheet.CreateRow(0);
  544. ICell cell1 = irow1.CreateCell(0);
  545. cell1.SetCellValue("12345联动服务工作"+ month + "月份办理情况通报表");
  546. cell1.CellStyle = cellStylebt;
  547. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 0, 19));
  548. IRow irow2 = sheet.CreateRow(2);
  549. ICell cell2 = irow2.CreateCell(0);
  550. cell2.SetCellValue("统计周期:"+ starttime+"至"+ endtime+" 统计时间:"+DateTime .Now.ToString ("yyyy年MM月dd日"));
  551. cell2.CellStyle = cellStylebt;
  552. sheet.AddMergedRegion(new CellRangeAddress(2, 2, 0, 19));
  553. IRow irow3 = sheet.CreateRow(3);
  554. ICell cell3 = irow3.CreateCell(0);
  555. cell3.SetCellValue("一、县(市、区)联动单位");
  556. cell3.CellStyle = cellStylebt;
  557. sheet.AddMergedRegion(new CellRangeAddress(3, 3, 0, 19));
  558. string[] cols = {"序号","联动单位","承办件",
  559. "承办率","得分(5分)","超期件","按时反馈率","得分(20分)",
  560. "未果件","办结率","得分(10分)","退单件","有效回复率",
  561. "得分(15分)","群众评议总数","一次不满意件","不满意件","满意率","得分(50分)","总分","排名"};
  562. IRow irow4 = sheet.CreateRow(4);
  563. int icolIndex = 0;
  564. foreach (string dc in cols)
  565. {
  566. ICell cell = irow4.CreateCell(icolIndex);
  567. cell.SetCellValue(dc);
  568. cell.CellStyle = cellStylebt;
  569. icolIndex++;
  570. }
  571. int iRowIndex = 5;
  572. DataRow[] rows = dt.Select("category=1");
  573. int index = 0;
  574. foreach (DataRow dr in rows)
  575. {
  576. index++;
  577. int iCellIndex = 0;
  578. IRow irow = sheet.CreateRow(iRowIndex );
  579. for (int i = 0; i < 21; i++)
  580. {
  581. ICell cell = irow.CreateCell(iCellIndex);
  582. cell.SetCellValue(GetCellValue(dr, index,i ));
  583. cell.CellStyle = cellStyle;
  584. iCellIndex++;
  585. }
  586. iRowIndex++;
  587. }
  588. IRow irow5 = sheet.CreateRow(iRowIndex);
  589. ICell cell5 = irow5.CreateCell(0);
  590. cell5.SetCellValue("二、市直机关联动单位一组");
  591. cell5.CellStyle = cellStylebt;
  592. sheet.AddMergedRegion(new CellRangeAddress(iRowIndex, iRowIndex, 0, 19));
  593. iRowIndex++;
  594. IRow irow6 = sheet.CreateRow(iRowIndex);
  595. icolIndex = 0;
  596. foreach (string dc in cols)
  597. {
  598. ICell cell = irow6.CreateCell(icolIndex);
  599. cell.SetCellValue(dc);
  600. cell.CellStyle = cellStylebt;
  601. icolIndex++;
  602. }
  603. iRowIndex++;
  604. rows = dt.Select("category=2");
  605. index = 0;
  606. foreach (DataRow dr in rows)
  607. {
  608. index++;
  609. int iCellIndex = 0;
  610. IRow irow = sheet.CreateRow(iRowIndex);
  611. for (int i = 0; i < 21; i++)
  612. {
  613. ICell cell = irow.CreateCell(iCellIndex);
  614. cell.SetCellValue(GetCellValue(dr, index,i ));
  615. cell.CellStyle = cellStyle;
  616. iCellIndex++;
  617. }
  618. iRowIndex++;
  619. }
  620. IRow irow7 = sheet.CreateRow(iRowIndex);
  621. ICell cell7 = irow7.CreateCell(0);
  622. cell7.SetCellValue("三、市直机关联动单位二组");
  623. cell7.CellStyle = cellStylebt;
  624. sheet.AddMergedRegion(new CellRangeAddress(iRowIndex, iRowIndex, 0, 19));
  625. iRowIndex++;
  626. IRow irow8 = sheet.CreateRow(iRowIndex);
  627. icolIndex = 0;
  628. foreach (string dc in cols)
  629. {
  630. ICell cell = irow8.CreateCell(icolIndex);
  631. cell.SetCellValue(dc);
  632. cell.CellStyle = cellStylebt;
  633. icolIndex++;
  634. }
  635. iRowIndex++;
  636. rows = dt.Select("category=3");
  637. index = 0;
  638. foreach (DataRow dr in rows)
  639. {
  640. index++;
  641. int iCellIndex = 0;
  642. IRow irow = sheet.CreateRow(iRowIndex);
  643. for (int i = 0; i < 21; i++)
  644. {
  645. ICell cell = irow.CreateCell(iCellIndex);
  646. cell.SetCellValue(GetCellValue(dr, index,i ));
  647. cell.CellStyle = cellStyle;
  648. iCellIndex++;
  649. }
  650. iRowIndex++;
  651. }
  652. IRow irow9 = sheet.CreateRow(iRowIndex);
  653. ICell cell9 = irow9.CreateCell(0);
  654. cell9.SetCellValue("四、公益型企业联动单位");
  655. cell9.CellStyle = cellStylebt;
  656. sheet.AddMergedRegion(new CellRangeAddress(iRowIndex, iRowIndex, 0, 19));
  657. iRowIndex++;
  658. IRow irow10 = sheet.CreateRow(iRowIndex);
  659. icolIndex = 0;
  660. foreach (string dc in cols)
  661. {
  662. ICell cell = irow10.CreateCell(icolIndex);
  663. cell.SetCellValue(dc);
  664. cell.CellStyle = cellStylebt;
  665. icolIndex++;
  666. }
  667. iRowIndex++;
  668. rows = dt.Select("category=4");
  669. index = 0;
  670. foreach (DataRow dr in rows)
  671. {
  672. index++;
  673. int iCellIndex = 0;
  674. IRow irow = sheet.CreateRow(iRowIndex);
  675. for (int i = 0; i < 21; i++)
  676. {
  677. ICell cell = irow.CreateCell(iCellIndex);
  678. cell.SetCellValue(GetCellValue(dr, index,i ));
  679. cell.CellStyle = cellStyle;
  680. iCellIndex++;
  681. }
  682. iRowIndex++;
  683. }
  684. //自适应列宽度
  685. for (int i = 0; i < 20; i++)
  686. {
  687. sheet.AutoSizeColumn(i);
  688. // sheet.SetColumnWidth(i, 20 * 256);
  689. }
  690. using (MemoryStream ms = new MemoryStream())
  691. {
  692. workbook.Write(ms);
  693. HttpContext curContext = HttpContext.Current;
  694. // 设置编码和附件格式
  695. curContext.Response.ContentType = "application/vnd.ms-excel";
  696. curContext.Response.ContentEncoding = Encoding.UTF8;
  697. curContext.Response.Charset = "";
  698. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  699. curContext.Response.AppendHeader("Content-Disposition",
  700. "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
  701. curContext.Response.BinaryWrite(ms.GetBuffer());
  702. workbook = null;
  703. ms.Close();
  704. ms.Dispose();
  705. curContext.Response.End();
  706. }
  707. return "";
  708. }
  709. catch
  710. {
  711. return "导出失败!";
  712. }
  713. }
  714. /// <summary>
  715. /// 督办数据报表导出
  716. /// </summary>
  717. /// <param name="ds"></param>
  718. /// <returns></returns>
  719. public string DBEJExportToExcel(DataTable dt, string Name, string month, string starttime, string endtime)
  720. {
  721. try
  722. {
  723. HSSFWorkbook workbook = new HSSFWorkbook();
  724. ISheet sheet = workbook.CreateSheet(Name);
  725. ICellStyle cellStyle = workbook.CreateCellStyle();
  726. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  727. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  728. cellStyle.SetFont(cellfont);
  729. ICellStyle cellStylebt = workbook.CreateCellStyle();
  730. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  731. cellfontbt.Boldweight = (short)FontBoldWeight.Bold;
  732. cellStylebt.SetFont(cellfontbt);
  733. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  734. cellStylebt.Alignment = HorizontalAlignment.Center;
  735. IRow irow1 = sheet.CreateRow(0);
  736. ICell cell1 = irow1.CreateCell(0);
  737. cell1.SetCellValue("12345联动服务工作" + month + "月份办理情况通报表");
  738. cell1.CellStyle = cellStylebt;
  739. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 0, 19));
  740. IRow irow2 = sheet.CreateRow(2);
  741. ICell cell2 = irow2.CreateCell(0);
  742. cell2.SetCellValue("统计周期:" + starttime + "至" + endtime + " 统计时间:" + DateTime.Now.ToString("yyyy年MM月dd日"));
  743. cell2.CellStyle = cellStylebt;
  744. sheet.AddMergedRegion(new CellRangeAddress(2, 2, 0, 19));
  745. IRow irow3 = sheet.CreateRow(3);
  746. ICell cell3 = irow3.CreateCell(0);
  747. cell3.SetCellValue("一、县(市、区)联动单位");
  748. cell3.CellStyle = cellStylebt;
  749. sheet.AddMergedRegion(new CellRangeAddress(3, 3, 0, 19));
  750. string[] cols = {"序号","联动单位","承办件",
  751. "承办率","得分(5分)","超期件","按时反馈率","得分(20分)",
  752. "未果件","办结率","得分(10分)","退单件","有效回复率",
  753. "得分(15分)","群众评议总数","一次不满意件","不满意件","满意率","得分(50分)","总分","排名"};
  754. IRow irow4 = sheet.CreateRow(4);
  755. int icolIndex = 0;
  756. foreach (string dc in cols)
  757. {
  758. ICell cell = irow4.CreateCell(icolIndex);
  759. cell.SetCellValue(dc);
  760. cell.CellStyle = cellStylebt;
  761. icolIndex++;
  762. }
  763. int iRowIndex = 5;
  764. // DataRow[] rows = dt.Select("category=1");
  765. int index = 0;
  766. foreach (DataRow dr in dt.Rows )
  767. {
  768. index++;
  769. int iCellIndex = 0;
  770. IRow irow = sheet.CreateRow(iRowIndex);
  771. for (int i = 0; i < 21; i++)
  772. {
  773. ICell cell = irow.CreateCell(iCellIndex);
  774. cell.SetCellValue(GetCellValue(dr, index, i));
  775. cell.CellStyle = cellStyle;
  776. iCellIndex++;
  777. }
  778. iRowIndex++;
  779. }
  780. //自适应列宽度
  781. for (int i = 0; i < 20; i++)
  782. {
  783. sheet.AutoSizeColumn(i);
  784. // sheet.SetColumnWidth(i, 20 * 256);
  785. }
  786. using (MemoryStream ms = new MemoryStream())
  787. {
  788. workbook.Write(ms);
  789. HttpContext curContext = HttpContext.Current;
  790. // 设置编码和附件格式
  791. curContext.Response.ContentType = "application/vnd.ms-excel";
  792. curContext.Response.ContentEncoding = Encoding.UTF8;
  793. curContext.Response.Charset = "";
  794. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  795. curContext.Response.AppendHeader("Content-Disposition",
  796. "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
  797. curContext.Response.BinaryWrite(ms.GetBuffer());
  798. workbook = null;
  799. ms.Close();
  800. ms.Dispose();
  801. curContext.Response.End();
  802. }
  803. return "";
  804. }
  805. catch
  806. {
  807. return "导出失败!";
  808. }
  809. }
  810. //
  811. /// <summary>
  812. /// 督办数据报表导出
  813. /// </summary>
  814. /// <param name="ds"></param>
  815. /// <returns></returns>
  816. public string ExportMergerToExcel(List<MergerHot.MergerHotSource> MergerHotSource)
  817. {
  818. try
  819. {
  820. int lenth = 0;
  821. if (MergerHotSource!=null&& MergerHotSource.Count>0)
  822. {
  823. if (MergerHotSource[0]!=null && MergerHotSource[0].MergerHot !=null
  824. && MergerHotSource[0].MergerHot.Count >0)
  825. {
  826. lenth = MergerHotSource[0].MergerHot.Count * 5+4;
  827. }
  828. else
  829. {
  830. return "";
  831. }
  832. }
  833. else
  834. {
  835. return "";
  836. }
  837. HSSFWorkbook workbook = new HSSFWorkbook();
  838. ISheet sheet = workbook.CreateSheet("归并热线每日数据统计");
  839. ICellStyle cellStyle = workbook.CreateCellStyle();
  840. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  841. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  842. cellStyle.SetFont(cellfont);
  843. ICellStyle cellStylebt = workbook.CreateCellStyle();
  844. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  845. cellfontbt.Boldweight = (short)FontBoldWeight.Bold;
  846. cellStylebt.SetFont(cellfontbt);
  847. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  848. cellStylebt.Alignment = HorizontalAlignment.Center;
  849. cellStylebt.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  850. cellStylebt.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  851. cellStylebt.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  852. cellStylebt.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  853. IRow irow1 = sheet.CreateRow(0);
  854. ICell cell1 = irow1.CreateCell(0);
  855. cell1.SetCellValue("归并热线每日数据统计");
  856. cell1.CellStyle=cellStylebt;
  857. ICellStyle cellStylebt1 = workbook.CreateCellStyle();
  858. NPOI.SS.UserModel.IFont cellfontbt1 = workbook.CreateFont();
  859. cellfontbt1.Boldweight = (short)FontBoldWeight.Bold;
  860. cellfontbt1.FontHeightInPoints = 22;
  861. cellStylebt1.SetFont(cellfontbt1);
  862. cellStylebt1.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  863. cellStylebt1.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  864. cellStylebt1.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  865. cellStylebt1.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  866. cellStylebt1.VerticalAlignment = VerticalAlignment.Center;
  867. cellStylebt1.Alignment = HorizontalAlignment.Center;
  868. cell1.CellStyle = cellStylebt1;
  869. sheet.AddMergedRegion(new CellRangeAddress(0, 2, 0, lenth));
  870. IRow irow2 = sheet.CreateRow(3);
  871. int icolIndex = 0;
  872. ICellStyle cellStylebt2 = workbook.CreateCellStyle();
  873. NPOI.SS.UserModel.IFont cellfontbt2 = workbook.CreateFont();
  874. cellfontbt2.Boldweight = (short)FontBoldWeight.Bold;
  875. cellStylebt2.WrapText = true;//设置换行这个要先设置
  876. cellfontbt2.FontHeightInPoints = 14;
  877. cellStylebt2.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  878. cellStylebt2.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  879. cellStylebt2.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  880. cellStylebt2.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  881. cellStylebt2.SetFont(cellfontbt2);
  882. cellStylebt2.VerticalAlignment = VerticalAlignment.Center;
  883. cellStylebt2.Alignment = HorizontalAlignment.Center;
  884. HSSFPalette palette = workbook.GetCustomPalette(); //调色板实例
  885. palette.SetColorAtIndex(8, 237,237, 237);
  886. NPOI.HSSF.Util.HSSFColor hssFColor = palette.FindColor(237, 237, 237);
  887. cellStylebt2.FillForegroundColor = hssFColor.Indexed ;
  888. cellStylebt2.FillPattern = FillPattern.SolidForeground;
  889. cellStylebt2.FillBackgroundColor = hssFColor.Indexed; ;
  890. ICellStyle cellStylebt3 = workbook.CreateCellStyle();
  891. NPOI.SS.UserModel.IFont cellfontbt3 = workbook.CreateFont();
  892. cellfontbt3.Boldweight = (short)FontBoldWeight.Bold;
  893. cellStylebt3.WrapText = true;//设置换行这个要先设置
  894. cellfontbt3.FontHeightInPoints = 14;
  895. cellStylebt3.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  896. cellStylebt3.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  897. cellStylebt3.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  898. cellStylebt3.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  899. cellStylebt3.SetFont(cellfontbt3);
  900. cellStylebt3.VerticalAlignment = VerticalAlignment.Center;
  901. cellStylebt3.Alignment = HorizontalAlignment.Center;
  902. HSSFPalette palette1 = workbook.GetCustomPalette(); //调色板实例
  903. palette1.SetColorAtIndex(9, 221,235,247);
  904. NPOI.HSSF.Util.HSSFColor hssFColor1 = palette1.FindColor(221, 235, 247);
  905. cellStylebt3.FillForegroundColor = hssFColor1.Indexed ;
  906. cellStylebt3.FillPattern = FillPattern.SolidForeground;
  907. cellStylebt3.FillBackgroundColor = hssFColor1.Indexed;
  908. ICellStyle cellStylebt4 = workbook.CreateCellStyle();
  909. NPOI.SS.UserModel.IFont cellfontbt4 = workbook.CreateFont();
  910. cellStylebt4.WrapText = true;//设置换行这个要先设置
  911. cellfontbt4.Boldweight = (short)FontBoldWeight.Bold;
  912. cellfontbt4.FontHeightInPoints = 11;
  913. cellStylebt4.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  914. cellStylebt4.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  915. cellStylebt4.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  916. cellStylebt4.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  917. cellStylebt4.SetFont(cellfontbt4);
  918. cellStylebt4.VerticalAlignment = VerticalAlignment.Center;
  919. cellStylebt4.Alignment = HorizontalAlignment.Center;
  920. ICellStyle cellStylebt5 = workbook.CreateCellStyle();
  921. NPOI.SS.UserModel.IFont cellfontbt5 = workbook.CreateFont();
  922. cellfontbt5.Boldweight = (short)FontBoldWeight.Bold;
  923. cellfontbt5.FontHeightInPoints = 11;
  924. cellStylebt5.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  925. cellStylebt5.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  926. cellStylebt5.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  927. cellStylebt5.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  928. cellStylebt5.WrapText = true;//设置换行这个要先设置
  929. cellStylebt5.SetFont(cellfontbt5);
  930. cellStylebt5.VerticalAlignment = VerticalAlignment.Center;
  931. cellStylebt5.Alignment = HorizontalAlignment.Center;
  932. HSSFPalette palette2 = workbook.GetCustomPalette(); //调色板实例
  933. palette2.SetColorAtIndex(10, 226, 239, 218);
  934. NPOI.HSSF.Util.HSSFColor hssFColor2 = palette2.FindColor(226, 239, 218);
  935. cellStylebt5.FillForegroundColor = hssFColor2.Indexed ;
  936. cellStylebt5.FillPattern = FillPattern.SolidForeground;
  937. cellStylebt5.FillBackgroundColor = hssFColor2.Indexed;
  938. ICellStyle cellStylebt8 = workbook.CreateCellStyle();
  939. NPOI.SS.UserModel.IFont cellfontbt8 = workbook.CreateFont();
  940. cellfontbt8.FontHeightInPoints = 10;
  941. cellStylebt8.SetFont(cellfontbt8);
  942. cellStylebt8.WrapText = true;//设置换行这个要先设置
  943. cellStylebt8.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  944. cellStylebt8.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  945. cellStylebt8.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  946. cellStylebt8.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  947. cellStylebt8.VerticalAlignment = VerticalAlignment.Center;
  948. cellStylebt8.Alignment = HorizontalAlignment.Center;
  949. HSSFPalette palette3 = workbook.GetCustomPalette(); //调色板实例
  950. palette3.SetColorAtIndex(11, 198, 224, 180);
  951. NPOI.HSSF.Util.HSSFColor hssFColor3 = palette3.FindColor(198, 224, 180);
  952. cellStylebt8.FillForegroundColor = hssFColor3.Indexed ;
  953. cellStylebt8.FillPattern = FillPattern.SolidForeground;
  954. cellStylebt8.FillBackgroundColor = hssFColor3.Indexed;
  955. ICellStyle cellStylebt9 = workbook.CreateCellStyle();
  956. NPOI.SS.UserModel.IFont cellfontbt9 = workbook.CreateFont();
  957. cellfontbt9.FontHeightInPoints = 11;
  958. cellfontbt9.Boldweight = (short)FontBoldWeight.Bold;
  959. cellStylebt9.SetFont(cellfontbt9);
  960. cellStylebt9.WrapText = true;//设置换行这个要先设置
  961. cellStylebt9.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  962. cellStylebt9.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  963. cellStylebt9.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  964. cellStylebt9.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  965. cellStylebt9.VerticalAlignment = VerticalAlignment.Center;
  966. cellStylebt9.Alignment = HorizontalAlignment.Center;
  967. HSSFPalette palette4 = workbook.GetCustomPalette(); //调色板实例
  968. palette4.SetColorAtIndex(12, 255, 242, 204);
  969. NPOI.HSSF.Util.HSSFColor hssFColor4 = palette4.FindColor(255, 242, 204);
  970. cellStylebt9.FillForegroundColor = hssFColor4.Indexed ; ;
  971. cellStylebt9.FillPattern = FillPattern.SolidForeground;
  972. cellStylebt9.FillBackgroundColor = hssFColor4.Indexed; ; ;
  973. ICellStyle cellStylebt10 = workbook.CreateCellStyle();
  974. NPOI.SS.UserModel.IFont cellfontbt10 = workbook.CreateFont();
  975. cellfontbt10.Boldweight = (short)FontBoldWeight.Bold;
  976. cellfontbt10.FontHeightInPoints = 11;
  977. cellStylebt10.WrapText = true;//设置换行这个要先设置
  978. cellStylebt10.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  979. cellStylebt10.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  980. cellStylebt10.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  981. cellStylebt10.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  982. cellStylebt10.SetFont(cellfontbt10);
  983. cellStylebt10.VerticalAlignment = VerticalAlignment.Center;
  984. cellStylebt10.Alignment = HorizontalAlignment.Center;
  985. HSSFPalette palette5 = workbook.GetCustomPalette(); //调色板实例
  986. palette5.SetColorAtIndex(13, 217, 225, 242);
  987. NPOI.HSSF.Util.HSSFColor hssFColor5 = palette5.FindColor(217, 225, 242);
  988. cellStylebt10.FillForegroundColor = hssFColor5.Indexed ;
  989. cellStylebt10.FillPattern = FillPattern.SolidForeground;
  990. cellStylebt10.FillBackgroundColor = hssFColor5.Indexed; ;
  991. ICellStyle cellStylebt6 = workbook.CreateCellStyle();
  992. NPOI.SS.UserModel.IFont cellfontbt6 = workbook.CreateFont();
  993. cellfontbt6.Boldweight = (short)FontBoldWeight.Bold;
  994. cellStylebt6.WrapText = true;//设置换行这个要先设置
  995. cellfontbt6.FontHeightInPoints = 10;
  996. cellStylebt6.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  997. cellStylebt6.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  998. cellStylebt6.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  999. cellStylebt6.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1000. cellStylebt6.SetFont(cellfontbt6);
  1001. cellStylebt6.VerticalAlignment = VerticalAlignment.Center;
  1002. cellStylebt6.Alignment = HorizontalAlignment.Center;
  1003. cellStylebt6.FillForegroundColor = hssFColor.Indexed ;
  1004. cellStylebt6.FillPattern = FillPattern.SolidForeground;
  1005. cellStylebt6.FillBackgroundColor = hssFColor.Indexed; ;
  1006. ICellStyle cellStylebt11 = workbook.CreateCellStyle();
  1007. NPOI.SS.UserModel.IFont cellfontbt11 = workbook.CreateFont();
  1008. cellfontbt11.FontHeightInPoints = 10;
  1009. cellStylebt11.WrapText = true;//设置换行这个要先设置
  1010. cellStylebt11.SetFont(cellfontbt11);
  1011. cellStylebt11.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1012. cellStylebt11.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1013. cellStylebt11.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1014. cellStylebt11.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1015. cellStylebt11.VerticalAlignment = VerticalAlignment.Center;
  1016. cellStylebt11.Alignment = HorizontalAlignment.Center;
  1017. ICellStyle cellStylebt12 = workbook.CreateCellStyle();
  1018. NPOI.SS.UserModel.IFont cellfontbt12 = workbook.CreateFont();
  1019. cellfontbt12.Boldweight = (short)FontBoldWeight.Bold;
  1020. cellfontbt12.FontHeightInPoints = 10;
  1021. cellStylebt12.WrapText = true;//设置换行这个要先设置
  1022. cellStylebt12.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1023. cellStylebt12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1024. cellStylebt12.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1025. cellStylebt12.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1026. cellStylebt12.SetFont(cellfontbt12);
  1027. cellStylebt12.VerticalAlignment = VerticalAlignment.Center;
  1028. cellStylebt12.Alignment = HorizontalAlignment.Center;
  1029. cellStylebt12.FillForegroundColor = hssFColor5.Indexed;
  1030. cellStylebt12.FillPattern = FillPattern.SolidForeground;
  1031. cellStylebt12.FillBackgroundColor = hssFColor5.Indexed; ;
  1032. ICellStyle cellStylebt13 = workbook.CreateCellStyle();
  1033. NPOI.SS.UserModel.IFont cellfontbt13 = workbook.CreateFont();
  1034. cellfontbt13.Boldweight = (short)FontBoldWeight.Bold;
  1035. cellStylebt13.WrapText = true;//设置换行这个要先设置
  1036. cellfontbt13.FontHeightInPoints = 11;
  1037. cellStylebt13.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1038. cellStylebt13.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1039. cellStylebt13.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1040. cellStylebt13.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1041. cellStylebt13.SetFont(cellfontbt13);
  1042. cellStylebt13.VerticalAlignment = VerticalAlignment.Center;
  1043. cellStylebt13.Alignment = HorizontalAlignment.Center;
  1044. cellStylebt13.FillForegroundColor = hssFColor.Indexed;
  1045. cellStylebt13.FillPattern = FillPattern.SolidForeground;
  1046. cellStylebt13.FillBackgroundColor = hssFColor.Indexed; ;
  1047. ICellStyle cellStylebt14 = workbook.CreateCellStyle();
  1048. NPOI.SS.UserModel.IFont cellfontbt14 = workbook.CreateFont();
  1049. cellfontbt14.FontHeightInPoints = 10;
  1050. cellStylebt14.SetFont(cellfontbt14);
  1051. cellStylebt14.WrapText = true;//设置换行这个要先设置
  1052. cellStylebt14.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1053. cellStylebt14.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1054. cellStylebt14.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1055. cellStylebt14.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1056. cellStylebt14.VerticalAlignment = VerticalAlignment.Center;
  1057. cellStylebt14.Alignment = HorizontalAlignment.Center;
  1058. cellStylebt14.FillForegroundColor = hssFColor4.Indexed; ;
  1059. cellStylebt14.FillPattern = FillPattern.SolidForeground;
  1060. cellStylebt14.FillBackgroundColor = hssFColor4.Indexed; ; ;
  1061. ICellStyle cellStylebt15 = workbook.CreateCellStyle();
  1062. NPOI.SS.UserModel.IFont cellfontbt15 = workbook.CreateFont();
  1063. cellfontbt15.Boldweight = (short)FontBoldWeight.Bold;
  1064. cellfontbt15.FontHeightInPoints = 10;
  1065. cellStylebt15.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1066. cellStylebt15.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1067. cellStylebt15.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1068. cellStylebt15.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1069. cellStylebt15.WrapText = true;//设置换行这个要先设置
  1070. cellStylebt15.SetFont(cellfontbt15);
  1071. cellStylebt15.VerticalAlignment = VerticalAlignment.Center;
  1072. cellStylebt15.Alignment = HorizontalAlignment.Center;
  1073. cellStylebt15.FillForegroundColor = hssFColor2.Indexed;
  1074. cellStylebt15.FillPattern = FillPattern.SolidForeground;
  1075. cellStylebt15.FillBackgroundColor = hssFColor2.Indexed;
  1076. int indexsource = -1;
  1077. for (int i = 0; i <= MergerHotSource[0].MergerHot.Count
  1078. ;i++)
  1079. {
  1080. if (i == 0)
  1081. {
  1082. ICell cell = irow2.CreateCell(0);
  1083. cell.SetCellValue("来源");
  1084. cell.CellStyle = cellStylebt2;
  1085. indexsource = 0;
  1086. sheet.SetColumnWidth(0, 12 * 300);
  1087. }
  1088. else if (i >0&&i <= MergerHotSource[0].MergerHot.Count-2)
  1089. {
  1090. ICell cell = irow2.CreateCell(indexsource+1);
  1091. cell.SetCellValue(MergerHotSource[0].MergerHot[i - 1].Source);
  1092. cell.CellStyle = cellStylebt2;
  1093. if (i ==1 )
  1094. {
  1095. if (MergerHotSource[0].MergerHot[i - 1].Source=="12345")
  1096. {
  1097. indexsource = 6;
  1098. for (int z=2;z <= indexsource - 1;z++)
  1099. {
  1100. ICell cell4 = irow2.CreateCell(z);
  1101. cell4.CellStyle = cellStylebt2;
  1102. }
  1103. sheet.AddMergedRegion(new CellRangeAddress(3, 3, 1, indexsource-1));
  1104. }
  1105. else
  1106. {
  1107. indexsource = 5;
  1108. for (int z = 2; z <= indexsource - 1; z++)
  1109. {
  1110. ICell cell4 = irow2.CreateCell(z);
  1111. cell4.CellStyle = cellStylebt2;
  1112. }
  1113. sheet.AddMergedRegion(new CellRangeAddress(3, 3, 1, indexsource-1));
  1114. }
  1115. }
  1116. else
  1117. {
  1118. if (MergerHotSource[0].MergerHot[i - 1].Source == "12345")
  1119. {
  1120. for (int z = indexsource + 2; z <= indexsource + 5 ; z++)
  1121. {
  1122. ICell cell4 = irow2.CreateCell(z);
  1123. cell4.CellStyle = cellStylebt2;
  1124. }
  1125. sheet.AddMergedRegion(new CellRangeAddress(3, 3, indexsource + 1, indexsource + 5));
  1126. indexsource = indexsource + 6;
  1127. }
  1128. else
  1129. {
  1130. for (int z = indexsource + 2; z <= indexsource + 4; z++)
  1131. {
  1132. ICell cell4 = irow2.CreateCell(z);
  1133. cell4.CellStyle = cellStylebt2;
  1134. }
  1135. sheet.AddMergedRegion(new CellRangeAddress(3, 3, indexsource + 1, indexsource + 4));
  1136. indexsource += 5;
  1137. }
  1138. }
  1139. }
  1140. else
  1141. {
  1142. ICell cell = irow2.CreateCell(indexsource + 1);
  1143. if (i == MergerHotSource[0].MergerHot.Count - 1)
  1144. {
  1145. cell.SetCellValue("当日小计");
  1146. }
  1147. else if (i == MergerHotSource[0].MergerHot.Count )
  1148. {
  1149. cell.SetCellValue("累计");
  1150. }
  1151. for (int z = indexsource + 2; z <= indexsource + 5; z++)
  1152. {
  1153. ICell cell4 = irow2.CreateCell(z);
  1154. cell4.CellStyle = cellStylebt2;
  1155. }
  1156. sheet.AddMergedRegion(new CellRangeAddress(3, 3, indexsource + 1, indexsource + 5));
  1157. cell.CellStyle = cellStylebt3;
  1158. indexsource += 6;
  1159. }
  1160. }
  1161. IRow irow3 = sheet.CreateRow(4);
  1162. indexsource = 0;
  1163. sheet.CreateRow(4).Height = 200 * 8;
  1164. for (int i = 0; i <= MergerHotSource[0].MergerHot.Count; i++)
  1165. {
  1166. if (i == 0)
  1167. {
  1168. ICell cell = irow3.CreateCell(0);
  1169. cell.SetCellValue("日期");
  1170. cell.CellStyle = cellStylebt4;
  1171. }
  1172. else if (i > 0 && i <= MergerHotSource[0].MergerHot.Count - 2)
  1173. {
  1174. if (i == 1)
  1175. {
  1176. indexsource = 1;
  1177. if (MergerHotSource[0].MergerHot[i - 1].Source == "12345")
  1178. {
  1179. for (int j = 0; j < 5; j++)
  1180. {
  1181. ICell cell = irow3.CreateCell(indexsource);
  1182. switch (j)
  1183. {
  1184. case 0:
  1185. cell.SetCellValue("来\n电\n量\n");
  1186. cell.CellStyle = cellStylebt5;
  1187. sheet.SetColumnWidth(1, 12 * 100);
  1188. break;
  1189. case 1:
  1190. cell.SetCellValue("接\n通\n量\n");
  1191. cell.CellStyle = cellStylebt9;
  1192. sheet.SetColumnWidth(2, 12 * 100);
  1193. break;
  1194. case 2:
  1195. cell.SetCellValue("接\n通\n率\n");
  1196. cell.CellStyle = cellStylebt13;
  1197. sheet.SetColumnWidth(3, 12 * 100);
  1198. break;
  1199. case 3:
  1200. cell.SetCellValue("未\n接\n通\n");
  1201. cell.CellStyle = cellStylebt4;
  1202. sheet.SetColumnWidth(4, 12 * 100);
  1203. break;
  1204. case 4:
  1205. cell.SetCellValue("主\n动\n放\n弃\n");
  1206. cell.CellStyle = cellStylebt4;
  1207. sheet.SetColumnWidth(5, 12 * 100);
  1208. break;
  1209. }
  1210. indexsource += 1;
  1211. }
  1212. sheet.SetColumnWidth(6, 12 * 35);
  1213. indexsource = 6;
  1214. }
  1215. else
  1216. {
  1217. indexsource = 1;
  1218. for (int j = 0; j < 4; j++)
  1219. {
  1220. ICell cell = irow3.CreateCell(indexsource);
  1221. switch (j)
  1222. {
  1223. case 0:
  1224. cell.SetCellValue("来\n电\n量\n");
  1225. cell.CellStyle = cellStylebt5;
  1226. sheet.SetColumnWidth(1, 12 * 100);
  1227. break;
  1228. case 1:
  1229. cell.SetCellValue("接\n通\n量\n");
  1230. cell.CellStyle = cellStylebt9;
  1231. sheet.SetColumnWidth(2, 12 * 100);
  1232. break;
  1233. case 2:
  1234. cell.SetCellValue("未\n接\n通\n");
  1235. cell.CellStyle = cellStylebt4;
  1236. sheet.SetColumnWidth(3, 12 * 100);
  1237. break;
  1238. case 3:
  1239. cell.SetCellValue("主\n动\n放\n弃\n");
  1240. cell.CellStyle = cellStylebt4;
  1241. sheet.SetColumnWidth(4, 12 * 100);
  1242. break;
  1243. }
  1244. indexsource += 1;
  1245. }
  1246. sheet.SetColumnWidth(5, 12 * 35);
  1247. indexsource = 5;
  1248. }
  1249. }
  1250. else
  1251. {
  1252. if (MergerHotSource[0].MergerHot[i - 1].Source == "12345")
  1253. {
  1254. for (int j = 0; j < 5; j++)
  1255. {
  1256. indexsource += 1;
  1257. ICell cell = irow3.CreateCell(indexsource);
  1258. switch (j)
  1259. {
  1260. case 0:
  1261. cell.SetCellValue("来\n电\n量\n");
  1262. cell.CellStyle = cellStylebt5;
  1263. sheet.SetColumnWidth(indexsource, 12 * 100);
  1264. break;
  1265. case 1:
  1266. cell.SetCellValue("接\n通\n量\n");
  1267. cell.CellStyle = cellStylebt9;
  1268. sheet.SetColumnWidth(indexsource, 12 * 100);
  1269. break;
  1270. case 2:
  1271. cell.SetCellValue("接\n通\n率\n");
  1272. cell.CellStyle = cellStylebt2;
  1273. sheet.SetColumnWidth(7, 12 * 100);
  1274. break;
  1275. case 3:
  1276. cell.SetCellValue("未\n接\n通\n");
  1277. cell.CellStyle = cellStylebt4;
  1278. sheet.SetColumnWidth(indexsource, 12 * 100);
  1279. break;
  1280. case 4:
  1281. cell.SetCellValue("主\n动\n放\n弃\n");
  1282. cell.CellStyle = cellStylebt4;
  1283. sheet.SetColumnWidth(indexsource, 12 * 100);
  1284. break;
  1285. }
  1286. }
  1287. indexsource += 1;
  1288. sheet.SetColumnWidth(indexsource, 12 * 35);
  1289. }
  1290. else
  1291. {
  1292. for (int j = 0; j < 4; j++)
  1293. {
  1294. indexsource += 1;
  1295. ICell cell = irow3.CreateCell(indexsource);
  1296. switch (j)
  1297. {
  1298. case 0:
  1299. cell.SetCellValue("来\n电\n量\n");
  1300. cell.CellStyle = cellStylebt5;
  1301. sheet.SetColumnWidth(indexsource, 12 * 100);
  1302. break;
  1303. case 1:
  1304. cell.SetCellValue("接\n通\n量\n");
  1305. cell.CellStyle = cellStylebt9;
  1306. sheet.SetColumnWidth(indexsource, 12 * 100);
  1307. break;
  1308. case 2:
  1309. cell.SetCellValue("未\n接\n通\n");
  1310. cell.CellStyle = cellStylebt4;
  1311. sheet.SetColumnWidth(indexsource, 12 * 100);
  1312. break;
  1313. case 3:
  1314. cell.SetCellValue("主\n动\n放\n弃\n");
  1315. cell.CellStyle = cellStylebt4;
  1316. sheet.SetColumnWidth(indexsource, 12 * 100);
  1317. break;
  1318. }
  1319. }
  1320. indexsource += 1;
  1321. sheet.SetColumnWidth(indexsource, 12 * 35);
  1322. }
  1323. }
  1324. }
  1325. else
  1326. {
  1327. for (int j = 0; j < 5; j++)
  1328. {
  1329. indexsource += 1;
  1330. ICell cell = irow3.CreateCell(indexsource);
  1331. switch (j)
  1332. {
  1333. case 0:
  1334. cell.SetCellValue("来\n电\n量\n");
  1335. cell.CellStyle = cellStylebt5;
  1336. sheet.SetColumnWidth(indexsource, 12 * 100);
  1337. break;
  1338. case 1:
  1339. cell.SetCellValue("接\n通\n量\n");
  1340. cell.CellStyle = cellStylebt9;
  1341. sheet.SetColumnWidth(indexsource, 12 * 100);
  1342. break;
  1343. case 2:
  1344. cell.SetCellValue("未\n接\n通\n");
  1345. cell.CellStyle = cellStylebt4;
  1346. sheet.SetColumnWidth(indexsource, 12 * 100);
  1347. break;
  1348. case 3:
  1349. cell.SetCellValue("主\n动\n放\n弃\n");
  1350. cell.CellStyle = cellStylebt4;
  1351. sheet.SetColumnWidth(indexsource, 12 * 100);
  1352. break;
  1353. case 4:
  1354. cell.SetCellValue("接\n通\n率\n");
  1355. cell.CellStyle = cellStylebt10;
  1356. sheet.SetColumnWidth(indexsource, 12 * 200);
  1357. break;
  1358. }
  1359. }
  1360. indexsource += 1;
  1361. sheet.SetColumnWidth(indexsource, 12 * 35);
  1362. }
  1363. }
  1364. for (int i=0;i< MergerHotSource.Count; i++)
  1365. {
  1366. IRow irow4 = sheet.CreateRow(5+i );
  1367. for (int j = 0; j <= MergerHotSource[i].MergerHot.Count; j++)
  1368. {
  1369. if (j == 0)
  1370. {
  1371. ICell cell = irow4.CreateCell(0);
  1372. cell.SetCellValue(MergerHotSource[i].Data);
  1373. cell.CellStyle = cellStylebt11;
  1374. indexsource = 0;
  1375. }
  1376. else
  1377. {
  1378. if (i == MergerHotSource.Count-1)
  1379. {
  1380. if (j == 1)
  1381. {
  1382. if (MergerHotSource[i].MergerHot[j - 1].Source == "12345")
  1383. {
  1384. for (int z = 0; z < 5; z++)
  1385. {
  1386. indexsource += 1;
  1387. ICell cell = irow4.CreateCell(indexsource);
  1388. switch (z)
  1389. {
  1390. case 0:
  1391. cell.SetCellValue(MergerHotSource[i]
  1392. .MergerHot[j - 1].Call);
  1393. cell.CellStyle = cellStylebt8;
  1394. break;
  1395. case 1:
  1396. cell.SetCellValue(MergerHotSource[i]
  1397. .MergerHot[j - 1].Connect);
  1398. cell.CellStyle = cellStylebt8;
  1399. break;
  1400. case 2:
  1401. cell.SetCellValue(MergerHotSource[i]
  1402. .MergerHot[j - 1].rate);
  1403. cell.CellStyle = cellStylebt8;
  1404. break;
  1405. case 3:
  1406. cell.SetCellValue(MergerHotSource[i]
  1407. .MergerHot[j - 1].Notconnected);
  1408. cell.CellStyle = cellStylebt8;
  1409. break;
  1410. case 4:
  1411. cell.SetCellValue(MergerHotSource[i]
  1412. .MergerHot[j - 1].voluntarily);
  1413. cell.CellStyle = cellStylebt8;
  1414. break;
  1415. }
  1416. }
  1417. indexsource += 1;
  1418. }
  1419. else
  1420. {
  1421. for (int z = 0; z < 4; z++)
  1422. {
  1423. indexsource += 1;
  1424. ICell cell = irow4.CreateCell(indexsource);
  1425. switch (z)
  1426. {
  1427. case 0:
  1428. cell.SetCellValue(MergerHotSource[i]
  1429. .MergerHot[j - 1].Call);
  1430. cell.CellStyle = cellStylebt8;
  1431. break;
  1432. case 1:
  1433. cell.SetCellValue(MergerHotSource[i]
  1434. .MergerHot[j - 1].Connect);
  1435. cell.CellStyle = cellStylebt8;
  1436. break;
  1437. case 2:
  1438. cell.SetCellValue(MergerHotSource[i]
  1439. .MergerHot[j - 1].Notconnected);
  1440. cell.CellStyle = cellStylebt8;
  1441. break;
  1442. case 3:
  1443. cell.SetCellValue(MergerHotSource[i]
  1444. .MergerHot[j - 1].voluntarily);
  1445. cell.CellStyle = cellStylebt8;
  1446. break;
  1447. }
  1448. }
  1449. indexsource += 1;
  1450. }
  1451. }
  1452. else
  1453. {
  1454. if (MergerHotSource[i].MergerHot[j - 1].Source == "12345")
  1455. {
  1456. for (int z = 0; z < 5; z++)
  1457. {
  1458. indexsource += 1;
  1459. ICell cell = irow4.CreateCell(indexsource);
  1460. switch (z)
  1461. {
  1462. case 0:
  1463. cell.SetCellValue(MergerHotSource[i]
  1464. .MergerHot[j - 1].Call);
  1465. cell.CellStyle = cellStylebt8;
  1466. break;
  1467. case 1:
  1468. cell.SetCellValue(MergerHotSource[i]
  1469. .MergerHot[j - 1].Connect);
  1470. cell.CellStyle = cellStylebt8;
  1471. break;
  1472. case 2:
  1473. cell.SetCellValue(MergerHotSource[i]
  1474. .MergerHot[j - 1].rate);
  1475. cell.CellStyle = cellStylebt8;
  1476. break;
  1477. case 3:
  1478. cell.SetCellValue(MergerHotSource[i]
  1479. .MergerHot[j - 1].Notconnected);
  1480. cell.CellStyle = cellStylebt8;
  1481. break;
  1482. case 4:
  1483. cell.SetCellValue(MergerHotSource[i]
  1484. .MergerHot[j - 1].voluntarily);
  1485. cell.CellStyle = cellStylebt8;
  1486. break;
  1487. }
  1488. }
  1489. indexsource += 1;
  1490. }
  1491. else if (MergerHotSource[i].MergerHot[j - 1].Source == "当日小计"
  1492. || MergerHotSource[i].MergerHot[j - 1].Source == "累计")
  1493. {
  1494. for (int z = 0; z < 5; z++)
  1495. {
  1496. indexsource += 1;
  1497. ICell cell = irow4.CreateCell(indexsource);
  1498. switch (z)
  1499. {
  1500. case 0:
  1501. cell.SetCellValue(MergerHotSource[i]
  1502. .MergerHot[j - 1].Call);
  1503. cell.CellStyle = cellStylebt8;
  1504. break;
  1505. case 1:
  1506. cell.SetCellValue(MergerHotSource[i]
  1507. .MergerHot[j - 1].Connect);
  1508. cell.CellStyle = cellStylebt8;
  1509. break;
  1510. case 2:
  1511. cell.SetCellValue(MergerHotSource[i]
  1512. .MergerHot[j - 1].Notconnected);
  1513. cell.CellStyle = cellStylebt8;
  1514. break;
  1515. case 3:
  1516. cell.SetCellValue(MergerHotSource[i]
  1517. .MergerHot[j - 1].voluntarily);
  1518. cell.CellStyle = cellStylebt8;
  1519. break;
  1520. case 4:
  1521. cell.SetCellValue(MergerHotSource[i]
  1522. .MergerHot[j - 1].rate);
  1523. cell.CellStyle = cellStylebt8;
  1524. break;
  1525. }
  1526. }
  1527. indexsource += 1;
  1528. }
  1529. else
  1530. {
  1531. for (int z = 0; z < 4; z++)
  1532. {
  1533. indexsource += 1;
  1534. ICell cell = irow4.CreateCell(indexsource);
  1535. switch (z)
  1536. {
  1537. case 0:
  1538. cell.SetCellValue(MergerHotSource[i]
  1539. .MergerHot[j - 1].Call);
  1540. cell.CellStyle = cellStylebt8;
  1541. break;
  1542. case 1:
  1543. cell.SetCellValue(MergerHotSource[i]
  1544. .MergerHot[j - 1].Connect);
  1545. cell.CellStyle = cellStylebt8;
  1546. break;
  1547. case 2:
  1548. cell.SetCellValue(MergerHotSource[i]
  1549. .MergerHot[j - 1].Notconnected);
  1550. cell.CellStyle = cellStylebt8;
  1551. break;
  1552. case 3:
  1553. cell.SetCellValue(MergerHotSource[i]
  1554. .MergerHot[j - 1].voluntarily);
  1555. cell.CellStyle = cellStylebt8;
  1556. break;
  1557. }
  1558. }
  1559. indexsource += 1;
  1560. }
  1561. }
  1562. }
  1563. else
  1564. {
  1565. if (j == 1)
  1566. {
  1567. if (MergerHotSource[i].MergerHot[j - 1].Source == "12345")
  1568. {
  1569. for (int z = 0; z < 5; z++)
  1570. {
  1571. indexsource += 1;
  1572. ICell cell = irow4.CreateCell(indexsource);
  1573. switch (z)
  1574. {
  1575. case 0:
  1576. cell.SetCellValue(MergerHotSource[i]
  1577. .MergerHot[j - 1].Call);
  1578. cell.CellStyle = cellStylebt15;
  1579. break;
  1580. case 1:
  1581. cell.SetCellValue(MergerHotSource[i]
  1582. .MergerHot[j - 1].Connect);
  1583. cell.CellStyle = cellStylebt14;
  1584. break;
  1585. case 2:
  1586. cell.SetCellValue(MergerHotSource[i]
  1587. .MergerHot[j - 1].rate);
  1588. cell.CellStyle = cellStylebt6;
  1589. break;
  1590. case 3:
  1591. cell.SetCellValue(MergerHotSource[i]
  1592. .MergerHot[j - 1].Notconnected);
  1593. cell.CellStyle = cellStylebt11;
  1594. break;
  1595. case 4:
  1596. cell.SetCellValue(MergerHotSource[i]
  1597. .MergerHot[j - 1].voluntarily);
  1598. cell.CellStyle = cellStylebt11;
  1599. break;
  1600. }
  1601. }
  1602. indexsource += 1;
  1603. }
  1604. else
  1605. {
  1606. for (int z = 0; z < 4; z++)
  1607. {
  1608. indexsource += 1;
  1609. ICell cell = irow4.CreateCell(indexsource);
  1610. switch (z)
  1611. {
  1612. case 0:
  1613. cell.SetCellValue(MergerHotSource[i]
  1614. .MergerHot[j - 1].Call);
  1615. cell.CellStyle = cellStylebt15;
  1616. break;
  1617. case 1:
  1618. cell.SetCellValue(MergerHotSource[i]
  1619. .MergerHot[j - 1].Connect);
  1620. cell.CellStyle = cellStylebt14;
  1621. break;
  1622. case 2:
  1623. cell.SetCellValue(MergerHotSource[i]
  1624. .MergerHot[j - 1].Notconnected);
  1625. cell.CellStyle = cellStylebt11;
  1626. break;
  1627. case 3:
  1628. cell.SetCellValue(MergerHotSource[i]
  1629. .MergerHot[j - 1].voluntarily);
  1630. cell.CellStyle = cellStylebt11;
  1631. break;
  1632. }
  1633. }
  1634. indexsource += 1;
  1635. }
  1636. }
  1637. else
  1638. {
  1639. if (MergerHotSource[i].MergerHot[j - 1].Source == "12345")
  1640. {
  1641. for (int z = 0; z < 5; z++)
  1642. {
  1643. indexsource += 1;
  1644. ICell cell = irow4.CreateCell(indexsource);
  1645. switch (z)
  1646. {
  1647. case 0:
  1648. cell.SetCellValue(MergerHotSource[i]
  1649. .MergerHot[j - 1].Call);
  1650. cell.CellStyle = cellStylebt15;
  1651. break;
  1652. case 1:
  1653. cell.SetCellValue(MergerHotSource[i]
  1654. .MergerHot[j - 1].Connect);
  1655. cell.CellStyle = cellStylebt14;
  1656. break;
  1657. case 2:
  1658. cell.SetCellValue(MergerHotSource[i]
  1659. .MergerHot[j - 1].rate);
  1660. cell.CellStyle = cellStylebt6;
  1661. break;
  1662. case 3:
  1663. cell.SetCellValue(MergerHotSource[i]
  1664. .MergerHot[j - 1].Notconnected);
  1665. cell.CellStyle = cellStylebt11;
  1666. break;
  1667. case 4:
  1668. cell.SetCellValue(MergerHotSource[i]
  1669. .MergerHot[j - 1].voluntarily);
  1670. cell.CellStyle = cellStylebt11;
  1671. break;
  1672. }
  1673. }
  1674. indexsource += 1;
  1675. }
  1676. else if (MergerHotSource[i].MergerHot[j - 1].Source == "当日小计"
  1677. || MergerHotSource[i].MergerHot[j - 1].Source == "累计")
  1678. {
  1679. for (int z = 0; z < 5; z++)
  1680. {
  1681. indexsource += 1;
  1682. ICell cell = irow4.CreateCell(indexsource);
  1683. switch (z)
  1684. {
  1685. case 0:
  1686. cell.SetCellValue(MergerHotSource[i]
  1687. .MergerHot[j - 1].Call);
  1688. cell.CellStyle = cellStylebt15;
  1689. break;
  1690. case 1:
  1691. cell.SetCellValue(MergerHotSource[i]
  1692. .MergerHot[j - 1].Connect);
  1693. cell.CellStyle = cellStylebt14;
  1694. break;
  1695. case 2:
  1696. cell.SetCellValue(MergerHotSource[i]
  1697. .MergerHot[j - 1].Notconnected);
  1698. cell.CellStyle = cellStylebt11;
  1699. break;
  1700. case 3:
  1701. cell.SetCellValue(MergerHotSource[i]
  1702. .MergerHot[j - 1].voluntarily);
  1703. cell.CellStyle = cellStylebt11;
  1704. break;
  1705. case 4:
  1706. cell.SetCellValue(MergerHotSource[i]
  1707. .MergerHot[j - 1].rate);
  1708. cell.CellStyle = cellStylebt12;
  1709. break;
  1710. }
  1711. }
  1712. indexsource += 1;
  1713. }
  1714. else
  1715. {
  1716. for (int z = 0; z < 4; z++)
  1717. {
  1718. indexsource += 1;
  1719. ICell cell = irow4.CreateCell(indexsource);
  1720. switch (z)
  1721. {
  1722. case 0:
  1723. cell.SetCellValue(MergerHotSource[i]
  1724. .MergerHot[j - 1].Call);
  1725. cell.CellStyle = cellStylebt15;
  1726. break;
  1727. case 1:
  1728. cell.SetCellValue(MergerHotSource[i]
  1729. .MergerHot[j - 1].Connect);
  1730. cell.CellStyle = cellStylebt14;
  1731. break;
  1732. case 2:
  1733. cell.SetCellValue(MergerHotSource[i]
  1734. .MergerHot[j - 1].Notconnected);
  1735. cell.CellStyle = cellStylebt11;
  1736. break;
  1737. case 3:
  1738. cell.SetCellValue(MergerHotSource[i]
  1739. .MergerHot[j - 1].voluntarily);
  1740. cell.CellStyle = cellStylebt11;
  1741. break;
  1742. }
  1743. }
  1744. indexsource += 1;
  1745. }
  1746. }
  1747. }
  1748. }
  1749. }
  1750. }
  1751. //自适应列宽度
  1752. //for (int i = 0; i < 20; i++)
  1753. //{
  1754. // sheet.AutoSizeColumn(i);
  1755. // // sheet.SetColumnWidth(i, 20 * 256);
  1756. //}
  1757. using (MemoryStream ms = new MemoryStream())
  1758. {
  1759. workbook.Write(ms);
  1760. HttpContext curContext = HttpContext.Current;
  1761. // 设置编码和附件格式
  1762. curContext.Response.ContentType = "application/vnd.ms-excel";
  1763. curContext.Response.ContentEncoding = Encoding.UTF8;
  1764. curContext.Response.Charset = "";
  1765. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  1766. curContext.Response.AppendHeader("Content-Disposition",
  1767. "attachment;filename=" + HttpUtility.UrlEncode("归并数据统计" + ".xls", Encoding.UTF8));
  1768. curContext.Response.BinaryWrite(ms.GetBuffer());
  1769. workbook = null;
  1770. ms.Close();
  1771. ms.Dispose();
  1772. curContext.Response.End();
  1773. }
  1774. return "";
  1775. }
  1776. catch
  1777. {
  1778. return "导出失败!";
  1779. }
  1780. }
  1781. /// <summary>
  1782. /// 弹出下载框导出excel
  1783. /// </summary>
  1784. /// <param name="Name"></param>
  1785. /// <param name="dt"></param>
  1786. /// <returns></returns>
  1787. public string TSExportToExcel(DataTable dt, int tscount)
  1788. {
  1789. try
  1790. {
  1791. //if (dt.Rows.Count > 0)
  1792. //{
  1793. HSSFWorkbook workbook = new HSSFWorkbook();
  1794. ISheet sheet = workbook.CreateSheet("Sheet1");
  1795. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  1796. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1797. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1798. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1799. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1800. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  1801. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  1802. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  1803. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  1804. //字体
  1805. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  1806. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  1807. headerfont.FontHeightInPoints = 12;
  1808. HeadercellStyle.SetFont(headerfont);
  1809. //用column name 作为列名
  1810. int icolIndex = 0;
  1811. IRow headerRow = sheet.CreateRow(0);
  1812. foreach (DataColumn dc in dt.Columns)
  1813. {
  1814. ICell cell = headerRow.CreateCell(icolIndex);
  1815. cell.SetCellValue(dc.ColumnName);
  1816. cell.CellStyle = HeadercellStyle;
  1817. icolIndex++;
  1818. }
  1819. ICellStyle cellStyle = workbook.CreateCellStyle();
  1820. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  1821. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  1822. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1823. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1824. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1825. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1826. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  1827. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  1828. cellStyle.SetFont(cellfont);
  1829. //建立内容行
  1830. int iRowIndex = 0;
  1831. foreach (DataRow dr in dt.Rows)
  1832. {
  1833. int iCellIndex = 0;
  1834. IRow irow = sheet.CreateRow(iRowIndex + 1);
  1835. for (int i = 0; i < dt.Columns.Count; i++)
  1836. {
  1837. string strsj = string.Empty;
  1838. if (dr[i] != null)
  1839. {
  1840. strsj = dr[i].ToString();
  1841. }
  1842. ICell cell = irow.CreateCell(iCellIndex);
  1843. cell.SetCellValue(strsj);
  1844. cell.CellStyle = cellStyle;
  1845. iCellIndex++;
  1846. }
  1847. iRowIndex++;
  1848. }
  1849. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 1));
  1850. sheet.AddMergedRegion(new CellRangeAddress(1, tscount, 0, 0));
  1851. for (int i = iRowIndex; i > tscount; i--)
  1852. {
  1853. sheet.AddMergedRegion(new CellRangeAddress(i, i, 0, 1));
  1854. }
  1855. //自适应列宽度
  1856. for (int i = 0; i < icolIndex; i++)
  1857. {
  1858. sheet.AutoSizeColumn(i);
  1859. }
  1860. using (MemoryStream ms = new MemoryStream())
  1861. {
  1862. workbook.Write(ms);
  1863. HttpContext curContext = HttpContext.Current;
  1864. // 设置编码和附件格式
  1865. curContext.Response.ContentType = "application/vnd.ms-excel";
  1866. curContext.Response.ContentEncoding = Encoding.UTF8;
  1867. curContext.Response.Charset = "";
  1868. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  1869. curContext.Response.AppendHeader("Content-Disposition",
  1870. "attachment;filename=" + HttpUtility.UrlEncode("投诉统计_导出文件_" + DateTime.Now.Ticks + ".xls", Encoding.UTF8));
  1871. curContext.Response.BinaryWrite(ms.GetBuffer());
  1872. workbook = null;
  1873. ms.Close();
  1874. ms.Dispose();
  1875. curContext.Response.End();
  1876. }
  1877. //}
  1878. return "";
  1879. }
  1880. catch
  1881. {
  1882. return "导出失败!";
  1883. }
  1884. }
  1885. /// <summary>
  1886. /// 生成excel到路径
  1887. /// </summary>
  1888. /// <param name="Name"></param>
  1889. /// <param name="dt"></param>
  1890. /// <returns></returns>
  1891. public string CreateExcelFile(string Name, DataTable dt, string Path,string[] cols = null)
  1892. {
  1893. try
  1894. {
  1895. if (dt.Rows.Count > 0)
  1896. {
  1897. HSSFWorkbook workbook = new HSSFWorkbook();
  1898. ISheet sheet = workbook.CreateSheet(Name);
  1899. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  1900. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1901. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1902. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1903. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1904. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  1905. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  1906. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  1907. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  1908. //字体
  1909. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  1910. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  1911. headerfont.FontHeightInPoints = 12;
  1912. HeadercellStyle.SetFont(headerfont);
  1913. //用column name 作为列名
  1914. int icolIndex = 0;
  1915. IRow headerRow = sheet.CreateRow(0);
  1916. if (cols == null || (cols != null && cols.Length == 0))
  1917. {
  1918. foreach (DataColumn dc in dt.Columns)
  1919. {
  1920. ICell cell = headerRow.CreateCell(icolIndex);
  1921. cell.SetCellValue(dc.ColumnName);
  1922. cell.CellStyle = HeadercellStyle;
  1923. icolIndex++;
  1924. }
  1925. }
  1926. else
  1927. {
  1928. foreach (string dc in cols)
  1929. {
  1930. ICell cell = headerRow.CreateCell(icolIndex);
  1931. cell.SetCellValue(dc);
  1932. cell.CellStyle = HeadercellStyle;
  1933. icolIndex++;
  1934. }
  1935. }
  1936. ICellStyle cellStyle = workbook.CreateCellStyle();
  1937. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  1938. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  1939. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1940. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1941. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1942. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1943. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  1944. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  1945. cellStyle.SetFont(cellfont);
  1946. //建立内容行
  1947. int iRowIndex = 0;
  1948. foreach (DataRow dr in dt.Rows)
  1949. {
  1950. int iCellIndex = 0;
  1951. IRow irow = sheet.CreateRow(iRowIndex + 1);
  1952. for (int i = 0; i < dt.Columns.Count; i++)
  1953. {
  1954. string strsj = string.Empty;
  1955. if (dr[i] != null)
  1956. {
  1957. strsj = dr[i].ToString();
  1958. }
  1959. ICell cell = irow.CreateCell(iCellIndex);
  1960. cell.SetCellValue(strsj);
  1961. cell.CellStyle = cellStyle;
  1962. iCellIndex++;
  1963. }
  1964. iRowIndex++;
  1965. }
  1966. //自适应列宽度
  1967. for (int i = 0; i < icolIndex; i++)
  1968. {
  1969. sheet.AutoSizeColumn(i);
  1970. }
  1971. Name = HttpUtility.UrlEncode(Name + "_导出文件_" + DateTime.Now.Ticks + ".xls");
  1972. Path = HttpContext.Current.Server.MapPath("..") + Path;
  1973. if (!Directory.Exists(Path)) Directory.CreateDirectory(Path);
  1974. FileStream fileHSSF = new FileStream(Path+ Name, FileMode.Create);
  1975. workbook.Write(fileHSSF);
  1976. fileHSSF.Close();
  1977. fileHSSF.Dispose();
  1978. workbook = null;
  1979. }
  1980. return "";
  1981. }
  1982. catch
  1983. {
  1984. return "生成失败!";
  1985. }
  1986. }
  1987. /// <summary>
  1988. /// 弹出下载框导出excel
  1989. /// </summary>
  1990. /// <param name="Name"></param>
  1991. /// <param name="dt"></param>
  1992. /// <returns></returns>
  1993. public string ExportToExcel64(string name, string base64url)
  1994. {
  1995. try
  1996. {
  1997. int delLength = base64url.IndexOf(',') + 1;
  1998. string str = base64url.Substring(delLength, base64url.Length - delLength);
  1999. byte[] bData = Convert.FromBase64String(str);
  2000. HttpContext curContext = HttpContext.Current;
  2001. // 设置编码和附件格式
  2002. curContext.Response.ContentType = "application/vnd.ms-excel";
  2003. curContext.Response.ContentEncoding = Encoding.UTF8;
  2004. curContext.Response.Charset = "";
  2005. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  2006. curContext.Response.AppendHeader("Content-Disposition",
  2007. "attachment;filename=" + HttpUtility.UrlEncode(name + "_导出文件_" + DateTime.Now.Ticks + ".xls", Encoding.UTF8));
  2008. curContext.Response.BinaryWrite(bData);
  2009. curContext.Response.End();
  2010. return "";
  2011. }
  2012. catch
  2013. {
  2014. return "导出失败!";
  2015. }
  2016. }
  2017. /// <summary>
  2018. /// 简报导出
  2019. /// </summary>
  2020. /// <param name="ds"></param>
  2021. /// <returns></returns>
  2022. public string SimpleExportToExcel(DataSet ds)
  2023. {
  2024. try
  2025. {
  2026. HSSFWorkbook workbook = new HSSFWorkbook();
  2027. ISheet sheet = workbook.CreateSheet("Sheet1");
  2028. ICellStyle cellStyle = workbook.CreateCellStyle();
  2029. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  2030. //cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  2031. //cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2032. //cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2033. //cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2034. //cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2035. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  2036. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  2037. cellStyle.SetFont(cellfont);
  2038. ICellStyle cellStylebt = workbook.CreateCellStyle();
  2039. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  2040. cellfontbt.Boldweight = (short)FontBoldWeight.Bold;
  2041. cellStylebt.SetFont(cellfontbt);
  2042. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  2043. cellStylebt.Alignment = HorizontalAlignment.Center;
  2044. IRow irow1 = sheet.CreateRow(1);
  2045. ICell cell1 = irow1.CreateCell(0);
  2046. cell1.SetCellValue("时间:"+ ds.Tables[5].Rows[0]["sdate"].ToString()+ " 至 "+ ds.Tables[5].Rows[0]["edate"].ToString());
  2047. cell1.CellStyle = cellStylebt;
  2048. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 0, 7));
  2049. //((HSSFSheet)sheet).SetEnclosedBorderOfRegion(new CellRangeAddress(1, 1, 0, 7), BorderStyle.Thin, NPOI.HSSF.Util.HSSFColor.Black.Index);
  2050. #region 话务受理情况
  2051. DataTable dt1 = ds.Tables[0];
  2052. IRow irow2 = sheet.CreateRow(2);
  2053. ICell cell2 = irow2.CreateCell(0);
  2054. cell2.SetCellValue("话务受理情况");
  2055. cell2.CellStyle = cellStylebt;
  2056. sheet.AddMergedRegion(new CellRangeAddress(2, 2, 0, 7));
  2057. IRow irow3 = sheet.CreateRow(3);
  2058. ICell cell31 = irow3.CreateCell(0);
  2059. cell31.SetCellValue("来电");
  2060. cell31.CellStyle = cellStyle;
  2061. ICell cell32 = irow3.CreateCell(1);
  2062. cell32.SetCellValue(Int32.Parse(dt1.Rows[0]["ldcount"].ToString()));
  2063. cell32.CellStyle = cellStyle;
  2064. ICell cell33 = irow3.CreateCell(2);
  2065. cell33.SetCellValue("接听");
  2066. cell33.CellStyle = cellStyle;
  2067. ICell cell34 = irow3.CreateCell(3);
  2068. cell34.SetCellValue(Int32.Parse(dt1.Rows[0]["jtcount"].ToString()));
  2069. cell34.CellStyle = cellStyle;
  2070. ICell cell35 = irow3.CreateCell(4);
  2071. cell35.SetCellValue("有效接听");
  2072. cell35.CellStyle = cellStyle;
  2073. ICell cell36 = irow3.CreateCell(5);
  2074. cell36.SetCellValue(Int32.Parse(dt1.Rows[0]["yxcount"].ToString()));
  2075. cell36.CellStyle = cellStyle;
  2076. #endregion
  2077. #region 工单受理情况
  2078. DataTable dt2 = ds.Tables[1];
  2079. IRow irow4 = sheet.CreateRow(4);
  2080. ICell cell4 = irow4.CreateCell(0);
  2081. cell4.SetCellValue("工单受理情况");
  2082. cell4.CellStyle = cellStylebt;
  2083. sheet.AddMergedRegion(new CellRangeAddress(4, 4, 0, 7));
  2084. IRow irow5 = sheet.CreateRow(5);
  2085. ICell cell51 = irow5.CreateCell(0);
  2086. cell51.SetCellValue("受理");
  2087. cell51.CellStyle = cellStyle;
  2088. ICell cell52 = irow5.CreateCell(1);
  2089. cell52.SetCellValue(Int32.Parse(dt2.Rows[0]["slcount"].ToString()));
  2090. cell52.CellStyle = cellStyle;
  2091. ICell cell53 = irow5.CreateCell(2);
  2092. cell53.SetCellValue("待提交");
  2093. cell53.CellStyle = cellStyle;
  2094. ICell cell54 = irow5.CreateCell(3);
  2095. cell54.SetCellValue(Int32.Parse(dt2.Rows[0]["dtjcount"].ToString()));
  2096. cell54.CellStyle = cellStyle;
  2097. ICell cell55 = irow5.CreateCell(4);
  2098. cell55.SetCellValue("待交办");
  2099. cell55.CellStyle = cellStyle;
  2100. ICell cell56 = irow5.CreateCell(5);
  2101. cell56.SetCellValue(Int32.Parse(dt2.Rows[0]["djbcount"].ToString()));
  2102. cell56.CellStyle = cellStyle;
  2103. ICell cell57 = irow5.CreateCell(6);
  2104. cell57.SetCellValue("待查收");
  2105. cell57.CellStyle = cellStyle;
  2106. ICell cell58 = irow5.CreateCell(7);
  2107. cell58.SetCellValue(Int32.Parse(dt2.Rows[0]["dcscount"].ToString()));
  2108. cell58.CellStyle = cellStyle;
  2109. IRow irow6 = sheet.CreateRow(6);
  2110. ICell cell61 = irow6.CreateCell(0);
  2111. cell61.SetCellValue("待审核退回");
  2112. cell61.CellStyle = cellStyle;
  2113. ICell cell62 = irow6.CreateCell(1);
  2114. cell62.SetCellValue(Int32.Parse(dt2.Rows[0]["dshthcount"].ToString()));
  2115. cell62.CellStyle = cellStyle;
  2116. ICell cell63 = irow6.CreateCell(2);
  2117. cell63.SetCellValue("待办理");
  2118. cell63.CellStyle = cellStyle;
  2119. ICell cell64 = irow6.CreateCell(3);
  2120. cell64.SetCellValue(Int32.Parse(dt2.Rows[0]["dblcount"].ToString()));
  2121. cell64.CellStyle = cellStyle;
  2122. ICell cell65 = irow6.CreateCell(4);
  2123. cell65.SetCellValue("待延时审核");
  2124. cell65.CellStyle = cellStyle;
  2125. ICell cell66 = irow6.CreateCell(5);
  2126. cell66.SetCellValue(Int32.Parse(dt2.Rows[0]["dshyscount"].ToString()));
  2127. cell66.CellStyle = cellStyle;
  2128. ICell cell67 = irow6.CreateCell(6);
  2129. cell67.SetCellValue("待回访");
  2130. cell67.CellStyle = cellStyle;
  2131. ICell cell68 = irow6.CreateCell(7);
  2132. cell68.SetCellValue(Int32.Parse(dt2.Rows[0]["dhfcount"].ToString()));
  2133. cell68.CellStyle = cellStyle;
  2134. IRow irow7 = sheet.CreateRow(7);
  2135. ICell cell71 = irow7.CreateCell(0);
  2136. cell71.SetCellValue("待结案");
  2137. cell71.CellStyle = cellStyle;
  2138. ICell cell72 = irow7.CreateCell(1);
  2139. cell72.SetCellValue(Int32.Parse(dt2.Rows[0]["dwjcount"].ToString()));
  2140. cell72.CellStyle = cellStyle;
  2141. ICell cell73 = irow7.CreateCell(2);
  2142. cell73.SetCellValue("待重办");
  2143. cell73.CellStyle = cellStyle;
  2144. ICell cell74 = irow7.CreateCell(3);
  2145. cell74.SetCellValue(Int32.Parse(dt2.Rows[0]["dcbcount"].ToString()));
  2146. cell74.CellStyle = cellStyle;
  2147. ICell cell75 = irow7.CreateCell(4);
  2148. cell75.SetCellValue("已结案");
  2149. cell75.CellStyle = cellStyle;
  2150. ICell cell76 = irow7.CreateCell(5);
  2151. cell76.SetCellValue(Int32.Parse(dt2.Rows[0]["ywjcount"].ToString()));
  2152. cell76.CellStyle = cellStyle;
  2153. ICell cell77 = irow7.CreateCell(6);
  2154. cell77.SetCellValue("在线办理");
  2155. cell77.CellStyle = cellStyle;
  2156. ICell cell78 = irow7.CreateCell(7);
  2157. cell78.SetCellValue(Int32.Parse(dt2.Rows[0]["zxbjcount"].ToString()));
  2158. cell78.CellStyle = cellStyle;
  2159. IRow irow8 = sheet.CreateRow(8);
  2160. ICell cell81 = irow8.CreateCell(0);
  2161. cell81.SetCellValue("中心转派");
  2162. cell81.CellStyle = cellStyle;
  2163. ICell cell82 = irow8.CreateCell(1);
  2164. cell82.SetCellValue(Int32.Parse(dt2.Rows[0]["zxzpcount"].ToString()));
  2165. cell82.CellStyle = cellStyle;
  2166. ICell cell83 = irow8.CreateCell(2);
  2167. cell83.SetCellValue("在线办理率");
  2168. cell83.CellStyle = cellStyle;
  2169. ICell cell84 = irow8.CreateCell(3);
  2170. cell84.SetCellValue(dt2.Rows[0]["zxbjrate"].ToString());
  2171. cell84.CellStyle = cellStyle;
  2172. #endregion
  2173. #region 政府热线受理情况
  2174. DataTable dt3 = ds.Tables[2];
  2175. IRow irow9 = sheet.CreateRow(9);
  2176. ICell cell9 = irow9.CreateCell(0);
  2177. cell9.SetCellValue("政府热线受理情况");
  2178. cell9.CellStyle = cellStylebt;
  2179. sheet.AddMergedRegion(new CellRangeAddress(9, 9, 0, 7));
  2180. IRow irow10 = sheet.CreateRow(10);
  2181. IRow irow11 = sheet.CreateRow(11);
  2182. int n = 0;
  2183. foreach (DataRow dr3 in dt3.Rows)
  2184. {
  2185. if (n < 4)
  2186. {
  2187. ICell cell101 = irow10.CreateCell(2 * (n + 1) - 2);
  2188. cell101.SetCellValue(dr3["source"].ToString());
  2189. cell101.CellStyle = cellStyle;
  2190. ICell cell102 = irow10.CreateCell(2 * (n + 1) - 1);
  2191. cell102.SetCellValue(Int32.Parse(dr3["count"].ToString()));
  2192. cell102.CellStyle = cellStyle;
  2193. }
  2194. else
  2195. {
  2196. ICell cell111 = irow11.CreateCell(2 * (n -3) - 2);
  2197. cell111.SetCellValue(dr3["source"].ToString());
  2198. cell111.CellStyle = cellStyle;
  2199. ICell cell112 = irow11.CreateCell(2 * (n - 3) - 1);
  2200. cell112.SetCellValue(Int32.Parse(dr3["count"].ToString()));
  2201. cell112.CellStyle = cellStyle;
  2202. }
  2203. n = n + 1;
  2204. }
  2205. #endregion
  2206. #region 工单受理类型情况
  2207. DataTable dt4 = ds.Tables[3];
  2208. IRow irow12 = sheet.CreateRow(12);
  2209. ICell cell12 = irow12.CreateCell(0);
  2210. cell12.SetCellValue("工单受理类型情况");
  2211. cell12.CellStyle = cellStylebt;
  2212. sheet.AddMergedRegion(new CellRangeAddress(12, 12, 0, 7));
  2213. IRow irow13 = sheet.CreateRow(13);
  2214. IRow irow14 = sheet.CreateRow(14);
  2215. int m = 0;
  2216. foreach (DataRow dr4 in dt4.Rows)
  2217. {
  2218. if (m < 4)
  2219. {
  2220. ICell cell131 = irow13.CreateCell(2 * (m + 1) - 2);
  2221. cell131.SetCellValue(dr4["type"].ToString());
  2222. cell131.CellStyle = cellStyle;
  2223. ICell cell132 = irow13.CreateCell(2 * (m + 1) - 1);
  2224. cell132.SetCellValue(Int32.Parse(dr4["count"].ToString()));
  2225. cell132.CellStyle = cellStyle;
  2226. }
  2227. else
  2228. {
  2229. ICell cell141 = irow14.CreateCell(2 * (m - 3) - 2);
  2230. cell141.SetCellValue(dr4["type"].ToString());
  2231. cell141.CellStyle = cellStyle;
  2232. ICell cell152 = irow14.CreateCell(2 * (m - 3) - 1);
  2233. cell152.SetCellValue(Int32.Parse(dr4["count"].ToString()));
  2234. cell152.CellStyle = cellStyle;
  2235. }
  2236. m = m + 1;
  2237. }
  2238. #endregion
  2239. #region 工单事发区域情况
  2240. DataTable dt5 = ds.Tables[4];
  2241. IRow irow15 = sheet.CreateRow(15);
  2242. ICell cell15 = irow15.CreateCell(0);
  2243. cell15.SetCellValue("工单事发区域情况");
  2244. cell15.CellStyle = cellStylebt;
  2245. sheet.AddMergedRegion(new CellRangeAddress(15, 15, 0, 7));
  2246. int rowcount = (dt5.Rows.Count / 4) + 1;
  2247. for (int i = 0; i < rowcount; i++)
  2248. {
  2249. IRow irow = sheet.CreateRow(16 + i);
  2250. for (int j = 0; j < 4; j++)
  2251. {
  2252. int num = i * 4 + j;
  2253. if (num != dt5.Rows.Count)
  2254. {
  2255. var dr = dt5.Rows[num];
  2256. ICell cellname = irow.CreateCell(2 * (j + 1) - 2);
  2257. cellname.SetCellValue(dr["areaname"].ToString());
  2258. cellname.CellStyle = cellStyle;
  2259. ICell cellcount = irow.CreateCell(2 * (j + 1) - 1);
  2260. cellcount.SetCellValue(Int32.Parse(dr["count"].ToString()));
  2261. cellcount.CellStyle = cellStyle;
  2262. }
  2263. else
  2264. {
  2265. break;
  2266. }
  2267. }
  2268. }
  2269. #endregion
  2270. //自适应列宽度
  2271. for (int i = 0; i < 8; i++)
  2272. {
  2273. sheet.AutoSizeColumn(i);
  2274. }
  2275. using (MemoryStream ms = new MemoryStream())
  2276. {
  2277. workbook.Write(ms);
  2278. HttpContext curContext = HttpContext.Current;
  2279. // 设置编码和附件格式
  2280. curContext.Response.ContentType = "application/vnd.ms-excel";
  2281. curContext.Response.ContentEncoding = Encoding.UTF8;
  2282. curContext.Response.Charset = "";
  2283. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  2284. curContext.Response.AppendHeader("Content-Disposition",
  2285. "attachment;filename=" + HttpUtility.UrlEncode("业务简报_导出文件_" + DateTime.Now.Ticks + ".xls", Encoding.UTF8));
  2286. curContext.Response.BinaryWrite(ms.GetBuffer());
  2287. workbook = null;
  2288. ms.Close();
  2289. ms.Dispose();
  2290. curContext.Response.End();
  2291. }
  2292. return "";
  2293. }
  2294. catch
  2295. {
  2296. return "导出失败!";
  2297. }
  2298. }
  2299. /// <summary>
  2300. /// 弹出下载框导出excel(数据第一行为标题)
  2301. /// </summary>
  2302. /// <param name="Name"></param>
  2303. /// <param name="dt"></param>
  2304. /// <returns></returns>
  2305. public string ExportToExcel2(string Name, DataTable dt, string[] cols = null)
  2306. {
  2307. try
  2308. {
  2309. //if (dt.Rows.Count > 0)
  2310. //{
  2311. HSSFWorkbook workbook = new HSSFWorkbook();
  2312. ISheet sheet = workbook.CreateSheet("Sheet1");
  2313. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  2314. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2315. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2316. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2317. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2318. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  2319. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  2320. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  2321. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  2322. //字体
  2323. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  2324. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  2325. headerfont.FontHeightInPoints = 12;
  2326. HeadercellStyle.SetFont(headerfont);
  2327. //用column name 作为列名
  2328. int icolIndex = 0;
  2329. IRow headerRow = sheet.CreateRow(0);
  2330. if (cols == null || (cols != null && cols.Length == 0))
  2331. {
  2332. //foreach (DataColumn dc in dt.Columns)
  2333. //{
  2334. // ICell cell = headerRow.CreateCell(icolIndex);
  2335. // cell.SetCellValue(dc.ColumnName);
  2336. // cell.CellStyle = HeadercellStyle;
  2337. // icolIndex++;
  2338. //}
  2339. for (int i = 0; i < dt.Columns.Count; i++)
  2340. {
  2341. ICell cell = headerRow.CreateCell(icolIndex);
  2342. cell.SetCellValue(dt.Rows[0][i].ToString());
  2343. cell.CellStyle = HeadercellStyle;
  2344. icolIndex++;
  2345. }
  2346. }
  2347. else
  2348. {
  2349. foreach (string dc in cols)
  2350. {
  2351. ICell cell = headerRow.CreateCell(icolIndex);
  2352. cell.SetCellValue(dc);
  2353. cell.CellStyle = HeadercellStyle;
  2354. icolIndex++;
  2355. }
  2356. }
  2357. ICellStyle cellStyle = workbook.CreateCellStyle();
  2358. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  2359. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  2360. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2361. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2362. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2363. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2364. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  2365. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  2366. cellStyle.SetFont(cellfont);
  2367. //建立内容行
  2368. int iRowIndex = 0;
  2369. foreach (DataRow dr in dt.Rows)
  2370. {
  2371. if (iRowIndex > 0)
  2372. {
  2373. int iCellIndex = 0;
  2374. IRow irow = sheet.CreateRow(iRowIndex);
  2375. for (int i = 0; i < dt.Columns.Count; i++)
  2376. {
  2377. string strsj = string.Empty;
  2378. if (dr[i] != null)
  2379. {
  2380. strsj = dr[i].ToString();
  2381. }
  2382. ICell cell = irow.CreateCell(iCellIndex);
  2383. cell.SetCellValue(strsj);
  2384. cell.CellStyle = cellStyle;
  2385. iCellIndex++;
  2386. }
  2387. }
  2388. iRowIndex++;
  2389. }
  2390. //自适应列宽度
  2391. for (int i = 0; i < icolIndex; i++)
  2392. {
  2393. sheet.AutoSizeColumn(i);
  2394. }
  2395. using (MemoryStream ms = new MemoryStream())
  2396. {
  2397. workbook.Write(ms);
  2398. HttpContext curContext = HttpContext.Current;
  2399. // 设置编码和附件格式
  2400. curContext.Response.ContentType = "application/vnd.ms-excel";
  2401. curContext.Response.ContentEncoding = Encoding.UTF8;
  2402. curContext.Response.Charset = "";
  2403. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  2404. curContext.Response.AppendHeader("Content-Disposition",
  2405. "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
  2406. curContext.Response.BinaryWrite(ms.GetBuffer());
  2407. workbook = null;
  2408. ms.Close();
  2409. ms.Dispose();
  2410. curContext.Response.End();
  2411. }
  2412. //}
  2413. return "";
  2414. }
  2415. catch
  2416. {
  2417. return "导出失败!";
  2418. }
  2419. }
  2420. /// <summary>
  2421. /// 导出word
  2422. /// </summary>
  2423. /// <returns></returns>
  2424. public byte[] ExportToWordDemo()
  2425. {
  2426. try
  2427. {
  2428. //创建document文档对象对象实例
  2429. NPOI.XWPF.UserModel.XWPFDocument document = new NPOI.XWPF.UserModel.XWPFDocument();
  2430. document.CreateParagraph();
  2431. NPOI.XWPF.UserModel.XWPFParagraph paragraph = document.CreateParagraph();//创建段落对象
  2432. paragraph.Alignment = NPOI.XWPF.UserModel.ParagraphAlignment.CENTER;//文字显示位置,段落排列(左对齐,居中,右对齐)
  2433. NPOI.XWPF.UserModel.XWPFRun xwpfRun = paragraph.CreateRun();//创建段落文本对象
  2434. xwpfRun.IsBold = true;//文字加粗
  2435. xwpfRun.SetText("安阳市12345政务服务热线交办单");//填充内容
  2436. xwpfRun.FontSize = 18;//设置文字大小
  2437. xwpfRun.SetFontFamily("宋体", NPOI.XWPF.UserModel.FontCharRange.None); //设置标题样式如:(微软雅黑,隶书,楷体)根据自己的需求而定
  2438. document.CreateParagraph();
  2439. //创建文档中的表格对象实例
  2440. NPOI.XWPF.UserModel.XWPFTable table = document.CreateTable(9, 8);//显示的行列数rows:3行,cols:4列
  2441. table.Width = 5200;//总宽度
  2442. table.SetColumnWidth(0, 650); /* 设置列宽 */
  2443. table.SetColumnWidth(1, 650);
  2444. table.SetColumnWidth(2, 650);
  2445. table.SetColumnWidth(3, 650);
  2446. table.SetColumnWidth(4, 650);
  2447. table.SetColumnWidth(5, 650);
  2448. table.SetColumnWidth(6, 650);
  2449. table.SetColumnWidth(7, 650);
  2450. for (int i = 0; i < 9; i++)
  2451. {
  2452. for (int j = 0; j < 8; j++)
  2453. {
  2454. var cp = table.GetRow(i).GetCell(j).GetCTTc().AddNewTcPr();
  2455. cp.AddNewVAlign().val = ST_VerticalJc.center;
  2456. }
  2457. }
  2458. //第一行
  2459. table.GetRow(0).GetCTRow().AddNewTrPr().AddNewTrHeight().val= (ulong)300;
  2460. var cp0 = table.GetRow(0).GetCell(0).GetCTTc().AddNewTcPr();
  2461. cp0.AddNewVMerge().val = ST_Merge.restart;
  2462. cp0.AddNewVAlign().val = ST_VerticalJc.center;
  2463. cp0.tcW = new CT_TblWidth();cp0.tcW.w = "650";cp0.tcW.type = ST_TblWidth.dxa;
  2464. table.GetRow(0).GetCell(0).SetParagraph(GetParagraph(table, true, "事项编号"));
  2465. var cp1 = table.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr();
  2466. cp1.AddNewVMerge().val = ST_Merge.restart;
  2467. cp1.AddNewVAlign().val = ST_VerticalJc.center;
  2468. table.GetRow(0).GetCell(1).SetParagraph(GetParagraph(table, false, "DH9941052721041200351"));
  2469. table.GetRow(0).GetCell(2).SetParagraph(GetParagraph(table, true, "工单来源"));
  2470. table.GetRow(0).GetCell(3).SetParagraph(GetParagraph(table, false, "市长电话"));
  2471. table.GetRow(0).GetCell(4).SetParagraph(GetParagraph(table, true, "办理时限"));
  2472. table.GetRow(0).GetCell(5).SetParagraph(GetParagraph(table, false, "2021-04-21"));
  2473. table.GetRow(0).GetCell(6).SetParagraph(GetParagraph(table, true, "联系电话"));
  2474. table.GetRow(0).GetCell(7).SetParagraph(GetParagraph(table, false, "017185328292"));
  2475. //第二行
  2476. table.GetRow(1).GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)300;
  2477. var cp2 = table.GetRow(1).GetCell(0).GetCTTc().AddNewTcPr();
  2478. cp2.AddNewVMerge().val = ST_Merge.@continue;
  2479. var cp3 = table.GetRow(1).GetCell(1).GetCTTc().AddNewTcPr();
  2480. cp3.AddNewVMerge().val = ST_Merge.@continue;
  2481. table.GetRow(1).GetCell(2).SetParagraph(GetParagraph(table, true, "签收时间"));
  2482. table.GetRow(1).GetCell(3).SetParagraph(GetParagraph(table, false, "2021-04-12"));
  2483. table.GetRow(1).GetCell(4).SetParagraph(GetParagraph(table, true, "办理时间"));
  2484. table.GetRow(1).GetCell(5).SetParagraph(GetParagraph(table, false, "2021-04-12"));
  2485. table.GetRow(1).GetCell(6).SetParagraph(GetParagraph(table, true, "来电人"));
  2486. table.GetRow(1).GetCell(7).SetParagraph(GetParagraph(table, false, "徐青田"));
  2487. //第三行
  2488. table.GetRow(2).GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)300;
  2489. table.GetRow(2).MergeCells(1, 7);//合并列
  2490. table.GetRow(2).GetCell(0).SetParagraph(GetParagraph(table, true, "事项区域"));
  2491. table.GetRow(2).GetCell(1).SetParagraph(GetParagraph(table, false, "河南省郑州市高新技术开发区大学科技园东区", false));
  2492. //第四行
  2493. table.GetRow(3).GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)2000;
  2494. table.GetRow(3).MergeCells(1, 7);//合并列
  2495. table.GetRow(3).GetCell(0).SetParagraph(GetParagraph(table, true, "内容摘要"));
  2496. table.GetRow(3).GetCell(1).SetParagraph(GetParagraph(table, false, "来电人反映:内黄县城关镇宛庄村第四生产队第二、三小组土地至今无法确权(遗留问题),村委会和镇政府承诺给解决,至今未解决,请政府责成相关部门调查处理。", false));
  2497. //第五行
  2498. table.GetRow(4).GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)300;
  2499. table.GetRow(4).MergeCells(1, 4);//合并列
  2500. table.GetRow(4).MergeCells(3, 4);//合并列
  2501. table.GetRow(4).GetCell(0).SetParagraph(GetParagraph(table, true, "承办单位"));
  2502. table.GetRow(4).GetCell(1).SetParagraph(GetParagraph(table, false, "内黄县政府"));
  2503. table.GetRow(4).GetCell(2).SetParagraph(GetParagraph(table, true, "协办单位"));
  2504. table.GetRow(4).GetCell(3).SetParagraph(GetParagraph(table, false, "内黄县政府"));
  2505. //第六行
  2506. table.GetRow(5).GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)2000;
  2507. table.GetRow(5).MergeCells(1, 7);//合并列
  2508. table.GetRow(5).GetCell(0).SetParagraph(GetParagraph(table, true, "调度意见"));
  2509. table.GetRow(5).GetCell(1).SetParagraph(GetParagraph(table, false, "请于1小时内与来电人取得联系(匿名除外),并于7个工作日内将办理情况反馈来电人,并将办理结果报市长便民公开电话受理中心,同时做好来电人信息保密工作。(若超期将进行通报;若无故不回复,将转交市委市政府督查局。)", false));
  2510. //第七行
  2511. table.GetRow(6).GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)1500;
  2512. table.GetRow(6).MergeCells(1, 7);//合并列
  2513. table.GetRow(6).GetCell(0).SetParagraph(GetParagraph(table, true, "承办意见"));
  2514. table.GetRow(6).GetCell(1).SetParagraph(GetParagraph(table, false, "请于1小时内与来电人取得联系(匿名除外),并于7个工作日内将办理情况反馈来电人,并将办理结果报市长便民公开电话受理中心,同时做好来电人信息保密工作。(若超期将进行通报;若无故不回复,将转交市委市政府督查局。)", false));
  2515. //第八行
  2516. table.GetRow(7).GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)1500;
  2517. table.GetRow(7).MergeCells(1, 7);//合并列
  2518. table.GetRow(7).GetCell(0).SetParagraph(GetParagraph(table, true, "领导批示"));
  2519. table.GetRow(7).GetCell(1).SetParagraph(GetParagraph(table, false, "请于1小时内与来电人取得联系(匿名除外),并于7个工作日内将办理情况反馈来电人,并将办理结果报市长便民公开电话受理中心,同时做好来电人信息保密工作。(若超期将进行通报;若无故不回复,将转交市委市政府督查局。)", false));
  2520. //第九行
  2521. table.GetRow(8).GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)2000;
  2522. table.GetRow(8).MergeCells(1, 7);//合并列
  2523. table.GetRow(8).GetCell(0).SetParagraph(GetParagraph(table, true, "处理结果"));
  2524. table.GetRow(8).GetCell(1).SetParagraph(GetParagraph(table, false, "2021-04-12 10:19:14 内黄县政府部门接到市长电话来源工单(编号:DH9941052721041200351)反映土地确权问题,已联系郑州第三方中标土地确权公司负责人待工作人员到我县后,就可以确权 [办理结果:已办理,承办人:刘志强,职务:城关镇人大主席,联系电话:15824608111,刘志强向当事人进行了反馈,当事人对结果表示未评价。]", false));
  2525. document.CreateParagraph();
  2526. NPOI.XWPF.UserModel.XWPFParagraph paragraph1 = document.CreateParagraph();//创建段落对象
  2527. paragraph1.Alignment = NPOI.XWPF.UserModel.ParagraphAlignment.CENTER;//文字显示位置,段落排列(左对齐,居中,右对齐)
  2528. NPOI.XWPF.UserModel.XWPFRun xwpfRun1 = paragraph1.CreateRun();//创建段落文本对象
  2529. xwpfRun1.IsBold = true;//文字加粗
  2530. xwpfRun1.SetText("联系电话:(0372) 12345审核人:8000 调度员:8065");//填充内容
  2531. xwpfRun1.FontSize = 9;//设置文字大小
  2532. xwpfRun1.SetFontFamily("宋体", NPOI.XWPF.UserModel.FontCharRange.None); //设置标题样式如:(微软雅黑,隶书,楷体)根据自己的需求而定
  2533. NPOI.XWPF.UserModel.XWPFParagraph paragraph2 = document.CreateParagraph();//创建段落对象
  2534. paragraph2.Alignment = NPOI.XWPF.UserModel.ParagraphAlignment.RIGHT;//文字显示位置,段落排列(左对齐,居中,右对齐)
  2535. NPOI.XWPF.UserModel.XWPFRun xwpfRun2 = paragraph2.CreateRun();//创建段落文本对象
  2536. using (FileStream picData = new FileStream(HttpContext.Current.Server.MapPath("/Upload/Word/word.png"), FileMode.Open, FileAccess.Read))
  2537. {
  2538. //图片的文件流 图片类型 图片名称 设置的宽度以及高度
  2539. xwpfRun2.AddPicture(picData, (int)PictureType.PNG, "word.png", NPOI.Util.Units.ToEMU(100), NPOI.Util.Units.ToEMU(100));
  2540. }
  2541. using (MemoryStream ms = new MemoryStream())
  2542. {
  2543. document.Write(ms);
  2544. return ms.GetBuffer();
  2545. }
  2546. }
  2547. catch(Exception ex)
  2548. {
  2549. return null;
  2550. }
  2551. }
  2552. /// <summary>
  2553. ///
  2554. /// </summary>
  2555. /// <param name="document"></param>
  2556. /// <param name="isbold"></param>
  2557. /// <param name="content"></param>
  2558. /// <returns></returns>
  2559. private NPOI.XWPF.UserModel.XWPFParagraph GetParagraph(NPOI.XWPF.UserModel.XWPFTable table,bool isbold,string content, bool iscenter=true)
  2560. {
  2561. var para = new CT_P();
  2562. //设置单元格文本对齐
  2563. para.AddNewPPr().AddNewTextAlignment();
  2564. NPOI.XWPF.UserModel.XWPFParagraph paragraph = new NPOI.XWPF.UserModel.XWPFParagraph(para,table.Body);
  2565. paragraph.VerticalAlignment = NPOI.XWPF.UserModel.TextAlignment.CENTER;
  2566. if (iscenter)
  2567. {
  2568. paragraph.Alignment = NPOI.XWPF.UserModel.ParagraphAlignment.CENTER;
  2569. }
  2570. else
  2571. {
  2572. paragraph.Alignment = NPOI.XWPF.UserModel.ParagraphAlignment.LEFT;
  2573. }
  2574. NPOI.XWPF.UserModel.XWPFRun xwpfRun = paragraph.CreateRun();//创建段落文本对象
  2575. xwpfRun.IsBold = isbold;//文字加粗
  2576. xwpfRun.SetText(content);//填充内容
  2577. xwpfRun.FontSize = 9;//设置文字大小
  2578. xwpfRun.SetFontFamily("宋体", NPOI.XWPF.UserModel.FontCharRange.None); //设置标题样式如:(微软雅黑,隶书,楷体)根据自己的需求而定
  2579. return paragraph;
  2580. }
  2581. /// <summary>
  2582. /// 插入图片
  2583. /// </summary>
  2584. /// <param name="table"></param>
  2585. /// <returns></returns>
  2586. private NPOI.XWPF.UserModel.XWPFParagraph GetParagraphImg(NPOI.XWPF.UserModel.XWPFTable table, bool isbold, string content, bool iscenter = true)
  2587. {
  2588. var para = new CT_P();
  2589. //设置单元格文本对齐
  2590. para.AddNewPPr().AddNewTextAlignment();
  2591. NPOI.XWPF.UserModel.XWPFParagraph paragraph = new NPOI.XWPF.UserModel.XWPFParagraph(para, table.Body);
  2592. paragraph.VerticalAlignment = NPOI.XWPF.UserModel.TextAlignment.CENTER;
  2593. if (iscenter)
  2594. {
  2595. paragraph.Alignment = NPOI.XWPF.UserModel.ParagraphAlignment.CENTER;
  2596. }
  2597. else
  2598. {
  2599. paragraph.Alignment = NPOI.XWPF.UserModel.ParagraphAlignment.LEFT;
  2600. }
  2601. NPOI.XWPF.UserModel.XWPFRun xwpfRun = paragraph.CreateRun();//创建段落文本对象
  2602. xwpfRun.IsBold = isbold;//文字加粗
  2603. xwpfRun.SetText(content);//填充内容
  2604. xwpfRun.FontSize = 9;//设置文字大小
  2605. xwpfRun.SetFontFamily("宋体", NPOI.XWPF.UserModel.FontCharRange.None); //设置标题样式如:(微软雅黑,隶书,楷体)根据自己的需求而定
  2606. using (FileStream picData = new FileStream(HttpContext.Current.Server.MapPath("/Upload/Word/word.png"), FileMode.Open, FileAccess.Read))
  2607. {
  2608. //图片的文件流 图片类型 图片名称 设置的宽度以及高度
  2609. xwpfRun.AddPicture(picData, (int)PictureType.PNG, "word.png", NPOI.Util.Units.ToEMU(100), NPOI.Util.Units.ToEMU(100));
  2610. }
  2611. return paragraph;
  2612. }
  2613. }
  2614. }