No Description

NPOIHelper.cs 212KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523
  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 < icolIndex; 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. /// </summary>
  398. /// <param name="Name"></param>
  399. /// <param name="dt"></param>
  400. /// <returns></returns>
  401. public string TotalTel(string Name, DataTable dt)
  402. {
  403. try
  404. {
  405. HSSFWorkbook workbook = new HSSFWorkbook();
  406. string message = "话务报表";
  407. ISheet sheet = workbook.CreateSheet(message);
  408. ICellStyle cellStyle = workbook.CreateCellStyle();
  409. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  410. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  411. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  412. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  413. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  414. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  415. cellStyle.VerticalAlignment = VerticalAlignment.Center;
  416. cellStyle.Alignment = HorizontalAlignment.Center;
  417. cellStyle.SetFont(cellfont);
  418. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  419. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  420. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  421. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  422. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  423. HeadercellStyle.VerticalAlignment = VerticalAlignment.Center;
  424. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  425. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  426. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  427. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  428. //字体
  429. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  430. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  431. headerfont.FontHeightInPoints = 15;
  432. HeadercellStyle.SetFont(headerfont);
  433. IRow irow1 = sheet.CreateRow(0);
  434. IRow irow3 = sheet.CreateRow(1);
  435. for (int i = 0; i < 9; i++)
  436. {
  437. ICell cel16 = irow1.CreateCell(i);
  438. cel16.CellStyle = HeadercellStyle;
  439. ICell cel18 = irow3.CreateCell(i);
  440. cel18.CellStyle = HeadercellStyle;
  441. }
  442. ICell cell1 = irow1.CreateCell(0);
  443. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 0, 0));
  444. cell1.CellStyle = HeadercellStyle;
  445. cell1.SetCellValue("时间");
  446. ICell cell2 = irow1.CreateCell(1);
  447. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 1, 1));
  448. cell2.SetCellValue("电话总量");
  449. cell2.CellStyle = HeadercellStyle;
  450. ICell cell3 = irow1.CreateCell(2);
  451. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 2, 3));
  452. cell3.SetCellValue("呼入总量");
  453. cell3.CellStyle = HeadercellStyle;
  454. ICell cell9 = irow1.CreateCell(4);
  455. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 4, 5));
  456. cell9.SetCellValue("呼出总量");
  457. cell9.CellStyle = HeadercellStyle;
  458. ICell cell4 = irow1.CreateCell(6);
  459. cell4.SetCellValue("呼入接通量");
  460. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 6, 6));
  461. cell4.CellStyle = HeadercellStyle;
  462. ICell cell11 = irow1.CreateCell(7);
  463. cell11.SetCellValue("呼入未接通量");
  464. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 7, 7));
  465. cell11.CellStyle = HeadercellStyle;
  466. ICell cell12 = irow1.CreateCell(8);
  467. cell12.SetCellValue("接通率");
  468. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 8, 8));
  469. cell12.CellStyle = HeadercellStyle;
  470. string[] cols = {"转坐席总量","主动放弃量","外呼总量","转移总量"};
  471. int icolIndex = 0;
  472. foreach (string dc in cols)
  473. {
  474. ICell cell = irow3.CreateCell(icolIndex + 2);
  475. cell.SetCellValue(dc);
  476. cell.CellStyle = HeadercellStyle;
  477. icolIndex++;
  478. }
  479. int iRowIndex = 1;
  480. foreach (DataRow dr in dt.Rows)
  481. {
  482. int iCellIndex = 0;
  483. IRow irow = sheet.CreateRow(iRowIndex + 1);
  484. for (int i = 0; i < dt.Columns.Count; i++)
  485. {
  486. string strsj = string.Empty;
  487. if (dr[i] != null)
  488. {
  489. strsj = dr[i].ToString();
  490. }
  491. ICell cell = irow.CreateCell(iCellIndex);
  492. cell.SetCellValue(strsj);
  493. cell.CellStyle = cellStyle;
  494. iCellIndex++;
  495. }
  496. iRowIndex++;
  497. }
  498. //自适应列宽度
  499. for (int i = 0; i < 9; i++)
  500. {
  501. sheet.SetColumnWidth(i, 20 * 256);
  502. }
  503. using (MemoryStream ms = new MemoryStream())
  504. {
  505. workbook.Write(ms);
  506. HttpContext curContext = HttpContext.Current;
  507. // 设置编码和附件格式
  508. curContext.Response.ContentType = "application/vnd.ms-excel";
  509. curContext.Response.ContentEncoding = Encoding.UTF8;
  510. curContext.Response.Charset = "";
  511. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  512. curContext.Response.AppendHeader("Content-Disposition",
  513. "attachment;filename=" + HttpUtility.UrlEncode(message + ".xls", Encoding.UTF8));
  514. curContext.Response.BinaryWrite(ms.GetBuffer());
  515. workbook = null;
  516. ms.Close();
  517. ms.Dispose();
  518. curContext.Response.End();
  519. }
  520. return "";
  521. }
  522. catch
  523. {
  524. return "导出失败!";
  525. }
  526. }
  527. /// <summary>
  528. /// 导入excel转换为datatable
  529. /// </summary>
  530. /// <param name="upfile"></param>
  531. /// <param name="headrow"></param>
  532. /// <returns></returns>
  533. public DataTable ExcelToTable(HttpPostedFileBase upfile, int headrow)
  534. {
  535. DataTable dt = new DataTable();
  536. IWorkbook workbook = null;
  537. Stream stream = upfile.InputStream;
  538. string suffix = upfile.FileName.Substring(upfile.FileName.LastIndexOf(".") + 1).ToLower();
  539. if (suffix == "xlsx") // 2007版本
  540. {
  541. workbook = new XSSFWorkbook(stream);
  542. }
  543. else if (suffix == "xls") // 2003版本
  544. {
  545. workbook = new HSSFWorkbook(stream);
  546. }
  547. //获取excel的第一个sheet
  548. ISheet sheet = workbook.GetSheetAt(0);
  549. //获取sheet的第一行
  550. IRow headerRow = sheet.GetRow(headrow);
  551. //一行最后一个方格的编号 即总的列数
  552. int cellCount = headerRow.LastCellNum;
  553. //最后一列的标号 即总的行数
  554. int rowCount = sheet.LastRowNum;
  555. //列名
  556. for (int i = 0; i < cellCount; i++)
  557. {
  558. dt.Columns.Add(headerRow.GetCell(i).ToString());
  559. }
  560. for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++)
  561. {
  562. DataRow dr = dt.NewRow();
  563. IRow row = sheet.GetRow(i);
  564. for (int j = row.FirstCellNum; j < cellCount; j++)
  565. {
  566. if (row.GetCell(j) != null)
  567. {
  568. dr[j] = row.GetCell(j).ToString().Trim(' ').Trim('\t');
  569. }
  570. }
  571. dt.Rows.Add(dr);
  572. }
  573. sheet = null;
  574. workbook = null;
  575. return dt;
  576. }
  577. private string GetCellValue(DataRow dr,int index,int i )
  578. {
  579. string str = "";
  580. switch (i)
  581. {
  582. case 0:
  583. str = index.ToString();
  584. break;
  585. case 1:
  586. str = dr["deptname"].ToString();
  587. break;
  588. case 2:
  589. str = dr["Undertakers"].ToString();
  590. break;
  591. case 3:
  592. str = dr["Undertakersrate"].ToString();
  593. break;
  594. case 4:
  595. str = dr["Undertakersscore"].ToString();
  596. break;
  597. case 5:
  598. str = dr["overdue"].ToString();
  599. break;
  600. case 6:
  601. str = dr["overduerate"].ToString();
  602. break;
  603. case 7:
  604. str = dr["overduescore"].ToString();
  605. break;
  606. case 8:
  607. str = dr["unsuccessful"].ToString();
  608. break;
  609. case 9:
  610. str = dr["unsuccessfulrate"].ToString();
  611. break;
  612. case 10:
  613. str = dr["unsuccessfulscore"].ToString();
  614. break;
  615. case 11:
  616. str = dr["Chargeback"].ToString();
  617. break;
  618. case 12:
  619. str = dr["Chargebackrate"].ToString();
  620. break;
  621. case 13:
  622. str = dr["Chargebackscore"].ToString();
  623. break;
  624. case 14:
  625. str = dr["Comment"].ToString();
  626. break;
  627. case 15:
  628. str = dr["Oncedissatisfied"].ToString();
  629. break;
  630. case 16:
  631. str = dr["twocedissatisfied"].ToString();
  632. break;
  633. case 17:
  634. str = dr["dissatisfied"].ToString();
  635. break;
  636. case 18:
  637. str = dr["satisfiedrate"].ToString();
  638. break;
  639. case 19:
  640. str = dr["satisfiedscore"].ToString();
  641. break;
  642. case 20:
  643. str = dr["total"].ToString();
  644. break;
  645. case 21:
  646. str = index.ToString();
  647. break;
  648. }
  649. return str;
  650. }
  651. public string ExcelMergerHotline(List <MergerHot.MergerHotSource> mergerHotSources
  652. )
  653. {
  654. return "";
  655. }
  656. public class Incoming
  657. {
  658. public string time { set; get; }
  659. public int IncomingCount { set; get; }//呼入量
  660. public int Oncapacity { set; get; }//接通量
  661. public int seatsCount { set; get; }
  662. public string seats { set; get; }
  663. public int newIncomingCount { set; get; }//呼入量
  664. public int newOncapacity { set; get; }//接通量
  665. public string newseats { set; get; }
  666. public int newseatsCount { set; get; }
  667. }
  668. /// <summary>
  669. /// 数据报表导出
  670. /// </summary>
  671. /// <param name="ds"></param>
  672. /// <returns></returns>
  673. public string IncomingCalls(List <Incoming> incomings )
  674. {
  675. try
  676. {
  677. HSSFWorkbook workbook = new HSSFWorkbook();
  678. ISheet sheet = workbook.CreateSheet("12345来电情况统计表");
  679. ICellStyle cellStyle = workbook.CreateCellStyle();
  680. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  681. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  682. cellStyle.SetFont(cellfont);
  683. ICellStyle cellStylebt = workbook.CreateCellStyle();
  684. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  685. cellfontbt.Boldweight = (short)FontBoldWeight.Normal;
  686. cellStylebt.SetFont(cellfontbt);
  687. cellfontbt.FontHeightInPoints = 10;
  688. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  689. cellStylebt.Alignment = HorizontalAlignment.Center;
  690. cellStylebt.WrapText = true;
  691. IRow irow1 = sheet.CreateRow(0);
  692. ICell cell1 = irow1.CreateCell(0);
  693. cell1.SetCellValue("12345来电情况统计表");
  694. cell1.CellStyle = cellStylebt;
  695. irow1.Height = 50 * 20;
  696. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 4));
  697. string[] cols = {"时间段","来电总量","接听总量(呼入已接通)","12345来电量","12345接听量(呼入已接通)",
  698. "12345坐席个数","12345坐席姓名" ,
  699. "专家坐席个数","专家坐席姓名" };
  700. IRow irow4 = sheet.CreateRow(1);
  701. int icolIndex = 0;
  702. foreach (string dc in cols)
  703. {
  704. ICell cell = irow4.CreateCell(icolIndex);
  705. cell.SetCellValue(dc);
  706. cell.CellStyle = cellStylebt;
  707. irow4.Height = 50*15;
  708. icolIndex++;
  709. }
  710. int iRowIndex = 2;
  711. int index = 0;
  712. foreach (var dr in incomings)
  713. {
  714. index++;
  715. IRow irow = sheet.CreateRow(iRowIndex);
  716. ICell cell = irow.CreateCell(0);
  717. cell.SetCellValue(dr.time );
  718. cell.CellStyle = cellStylebt;
  719. ICell cel2 = irow.CreateCell(1);
  720. cel2.SetCellValue(dr.newIncomingCount );
  721. cel2.CellStyle = cellStylebt;
  722. ICell cel3 = irow.CreateCell(2);
  723. cel3.SetCellValue(dr.newOncapacity );
  724. cel3.CellStyle = cellStylebt;
  725. ICell cel5 = irow.CreateCell(3);
  726. cel5.SetCellValue(dr.IncomingCount);
  727. cel5.CellStyle = cellStylebt;
  728. ICell cel6 = irow.CreateCell(4);
  729. cel6.SetCellValue(dr.Oncapacity);
  730. cel6.CellStyle = cellStylebt;
  731. if (dr.time=="共计")
  732. {
  733. ICell cel4 = irow.CreateCell(5);
  734. cel4.SetCellValue("");
  735. cel4.CellStyle = cellStylebt;
  736. }
  737. else
  738. {
  739. ICell cel4 = irow.CreateCell(5);
  740. cel4.SetCellValue(dr.seatsCount);
  741. cel4.CellStyle = cellStylebt;
  742. }
  743. ICell cel8 = irow.CreateCell(6);
  744. cel8.SetCellValue(dr.seats );
  745. cel8.CellStyle = cellStylebt;
  746. if (dr.time == "共计")
  747. {
  748. ICell cel9 = irow.CreateCell(7);
  749. cel9.SetCellValue("");
  750. cel9.CellStyle = cellStylebt;
  751. }
  752. else
  753. {
  754. ICell cel9 = irow.CreateCell(7);
  755. cel9.SetCellValue(dr.newseatsCount);
  756. cel9.CellStyle = cellStylebt;
  757. }
  758. ICell cel0 = irow.CreateCell(8);
  759. cel0.SetCellValue(dr.newseats);
  760. cel0.CellStyle = cellStylebt;
  761. irow.Height = 50*15;
  762. iRowIndex++;
  763. }
  764. //自适应列宽度
  765. for (int i = 0; i < 5; i++)
  766. {
  767. sheet.AutoSizeColumn(i);
  768. // sheet.SetColumnWidth(i, 20 * 256);
  769. }
  770. using (MemoryStream ms = new MemoryStream())
  771. {
  772. workbook.Write(ms);
  773. HttpContext curContext = HttpContext.Current;
  774. // 设置编码和附件格式
  775. curContext.Response.ContentType = "application/vnd.ms-excel";
  776. curContext.Response.ContentEncoding = Encoding.UTF8;
  777. curContext.Response.Charset = "";
  778. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  779. curContext.Response.AppendHeader("Content-Disposition",
  780. "attachment;filename=" + HttpUtility.UrlEncode("12345来电情况统计表" + ".xls", Encoding.UTF8));
  781. curContext.Response.BinaryWrite(ms.GetBuffer());
  782. workbook = null;
  783. ms.Close();
  784. ms.Dispose();
  785. curContext.Response.End();
  786. }
  787. return "";
  788. }
  789. catch
  790. {
  791. return "导出失败!";
  792. }
  793. }
  794. public int Evaluation(string message,DataTable dt ,int index, ISheet sheet, ICellStyle cellStylebt, HSSFWorkbook workbook)
  795. {
  796. IRow irow01 = sheet.CreateRow(index);
  797. ICell cell01 = irow01.CreateCell(0);
  798. cell01.SetCellValue(message);
  799. cell01.CellStyle = cellStylebt;
  800. irow01.Height = 40 * 20;
  801. sheet.AddMergedRegion(new CellRangeAddress(index, index, 0, 14));
  802. index = index + 1;
  803. IRow irow1 = sheet.CreateRow(index);
  804. ICell cell1 = irow1.CreateCell(0);
  805. cell1.SetCellValue("序号");
  806. cell1.CellStyle = cellStylebt;
  807. sheet.AddMergedRegion(new CellRangeAddress(index, index+1, 0, 0));
  808. ICell cell2 = irow1.CreateCell(1);
  809. cell2.SetCellValue("部门名称");
  810. cell2.CellStyle = cellStylebt;
  811. sheet.AddMergedRegion(new CellRangeAddress(index, index + 1, 1, 1));
  812. ICell cell14 = irow1.CreateCell(8);
  813. cell14.SetCellValue("回访量");
  814. cell14.CellStyle = cellStylebt;
  815. sheet.AddMergedRegion(new CellRangeAddress(index, index + 1, 8, 8));
  816. ICell cell15 = irow1.CreateCell(9);
  817. cell15.SetCellValue("满意量");
  818. cell15.CellStyle = cellStylebt;
  819. sheet.AddMergedRegion(new CellRangeAddress(index, index + 1, 9, 9));
  820. ICell cell4 = irow1.CreateCell(10);
  821. cell4.SetCellValue("回访满意度");
  822. cell4.CellStyle = cellStylebt;
  823. sheet.AddMergedRegion(new CellRangeAddress(index, index + 1, 10, 10));
  824. ICell cell5 = irow1.CreateCell(11);
  825. cell5.SetCellValue("知识库更新");
  826. cell5.CellStyle = cellStylebt;
  827. sheet.AddMergedRegion(new CellRangeAddress(index, index + 1, 11, 11));
  828. ICell cell8 = irow1.CreateCell(12);
  829. cell8.SetCellValue("热线整合");
  830. cell8.CellStyle = cellStylebt;
  831. sheet.AddMergedRegion(new CellRangeAddress(index, index + 1, 12, 12));
  832. ICell cell6 = irow1.CreateCell(13);
  833. cell6.SetCellValue("加扣分项");
  834. cell6.CellStyle = cellStylebt;
  835. sheet.AddMergedRegion(new CellRangeAddress(index, index + 1, 13, 13));
  836. ICell cell7 = irow1.CreateCell(14);
  837. cell7.SetCellValue("总分");
  838. cell7.CellStyle = cellStylebt;
  839. sheet.AddMergedRegion(new CellRangeAddress(index, index + 1, 14, 14));
  840. ICell cell9 = irow1.CreateCell(2);
  841. cell9.SetCellValue("工单办理");
  842. cell9.CellStyle = cellStylebt;
  843. sheet.AddMergedRegion(new CellRangeAddress(index, index, 2, 7));
  844. //ICell cell10 = irow1.CreateCell(4);
  845. //cell10.SetCellValue("事项办理");
  846. //cell10.CellStyle = cellStylebt;
  847. //sheet.AddMergedRegion(new CellRangeAddress(index, index, 4, 5));
  848. //ICell cell12 = irow1.CreateCell(6);
  849. //cell12.SetCellValue("办理时限");
  850. //cell12.CellStyle = cellStylebt;
  851. //sheet.AddMergedRegion(new CellRangeAddress(index, index, 6, 7));
  852. //ICell cell13 = irow1.CreateCell(8);
  853. //cell13.SetCellValue("重办事项");
  854. //cell13.CellStyle = cellStylebt;
  855. //sheet.AddMergedRegion(new CellRangeAddress(index, index, 8, 9));
  856. index = index + 1;
  857. IRow irow3 = sheet.CreateRow(index);
  858. //ICell cell11 = irow3.CreateCell(2);
  859. //cell11.SetCellValue("数量");
  860. //cell11.CellStyle = cellStylebt;
  861. //ICell cell16 = irow3.CreateCell(3);
  862. //cell16.SetCellValue("得分");
  863. //cell16.CellStyle = cellStylebt;
  864. ICell cell17 = irow3.CreateCell(2);
  865. cell17.SetCellValue("承办量");
  866. cell17.CellStyle = cellStylebt;
  867. ICell cell18 = irow3.CreateCell(3);
  868. cell18.SetCellValue("事项办理得分");
  869. cell18.CellStyle = cellStylebt;
  870. ICell cell19 = irow3.CreateCell(4);
  871. cell19.SetCellValue("逾期量");
  872. cell19.CellStyle = cellStylebt;
  873. ICell cell20 = irow3.CreateCell(5);
  874. cell20.SetCellValue("办理时限得分");
  875. cell20.CellStyle = cellStylebt;
  876. ICell cell21 = irow3.CreateCell(6);
  877. cell21.SetCellValue("重办量");
  878. cell21.CellStyle = cellStylebt;
  879. ICell cell22 = irow3.CreateCell(7);
  880. cell22.SetCellValue("重办事项得分");
  881. cell22.CellStyle = cellStylebt;
  882. ICellStyle cellStylebt1 = workbook.CreateCellStyle();
  883. NPOI.SS.UserModel.IFont cellfontbt1 = workbook.CreateFont();
  884. cellfontbt1.Boldweight = (short)FontBoldWeight.Normal;
  885. cellStylebt1.SetFont(cellfontbt1);
  886. cellStylebt1.VerticalAlignment = VerticalAlignment.Center;
  887. cellStylebt1.Alignment = HorizontalAlignment.Center;
  888. index = index + 1;
  889. if (dt != null && dt.Rows.Count > 0)
  890. {
  891. int iCellIndex = 0;
  892. foreach (DataRow dr in dt.Rows)
  893. {
  894. IRow irow4 = sheet.CreateRow(index);
  895. index++;
  896. iCellIndex++;
  897. for (int i = 0; i < 15; i++)
  898. {
  899. switch (i)
  900. {
  901. case 0:
  902. ICell cell = irow4.CreateCell(i);
  903. cell.SetCellValue(iCellIndex);
  904. cell.CellStyle = cellStylebt1;
  905. break;
  906. case 1:
  907. ICell cel1 = irow4.CreateCell(i);
  908. cel1.SetCellValue(dr["F_DeptName"].ToString());
  909. cel1.CellStyle = cellStylebt1;
  910. break;
  911. //case 2:
  912. // ICell cel2 = irow4.CreateCell(i);
  913. // cel2.SetCellValue(dr["F_Reporting"].ToString());
  914. // cel2.CellStyle = cellStylebt1;
  915. // break;
  916. //case 3:
  917. // ICell cel12 = irow4.CreateCell(i);
  918. // cel12.SetCellValue(dr["F_Matter"].ToString());
  919. // cel12.CellStyle = cellStylebt1;
  920. // break;
  921. case 2:
  922. ICell cel3 = irow4.CreateCell(i);
  923. cel3.SetCellValue(dr["F_Undertake"].ToString());
  924. cel3.CellStyle = cellStylebt1;
  925. break;
  926. case 3:
  927. ICell cel13 = irow4.CreateCell(i);
  928. cel13.SetCellValue(dr["F_Handling"].ToString());
  929. cel13.CellStyle = cellStylebt1;
  930. break;
  931. case 4:
  932. ICell cel4 = irow4.CreateCell(i);
  933. cel4.SetCellValue(dr["F_Boverdue"].ToString());
  934. cel4.CellStyle = cellStylebt1;
  935. break;
  936. case 5:
  937. ICell cel14 = irow4.CreateCell(i);
  938. cel14.SetCellValue(dr["F_Timelimit"].ToString());
  939. cel14.CellStyle = cellStylebt1;
  940. break;
  941. case 6:
  942. ICell cel15 = irow4.CreateCell(i);
  943. cel15.SetCellValue(dr["F_Redovolume"].ToString());
  944. cel15.CellStyle = cellStylebt1;
  945. break;
  946. case 7:
  947. ICell cel5 = irow4.CreateCell(i);
  948. cel5.SetCellValue(dr["F_Redo"].ToString());
  949. cel5.CellStyle = cellStylebt1;
  950. break;
  951. case 8:
  952. ICell cel16 = irow4.CreateCell(i);
  953. cel16.SetCellValue(dr["F_Returnvisit"].ToString());
  954. cel16.CellStyle = cellStylebt1;
  955. break;
  956. case 9:
  957. ICell cel17 = irow4.CreateCell(i);
  958. cel17.SetCellValue(dr["F_Satisfaction"].ToString());
  959. cel17.CellStyle = cellStylebt1;
  960. break;
  961. case 10:
  962. ICell cel6 = irow4.CreateCell(i);
  963. cel6.SetCellValue(dr["F_Satisfied"].ToString());
  964. cel6.CellStyle = cellStylebt1;
  965. break;
  966. case 11:
  967. ICell cel7 = irow4.CreateCell(i);
  968. cel7.SetCellValue(dr["F_knowledge"].ToString());
  969. cel7.CellStyle = cellStylebt1;
  970. break;
  971. case 12:
  972. ICell cel10 = irow4.CreateCell(i);
  973. cel10.SetCellValue(dr["F_Integration"].ToString());
  974. cel10.CellStyle = cellStylebt1;
  975. break;
  976. case 13:
  977. ICell cel8 = irow4.CreateCell(i);
  978. cel8.SetCellValue(dr["F_Buckle"].ToString());
  979. cel8.CellStyle = cellStylebt1;
  980. break;
  981. case 14:
  982. ICell cel9 = irow4.CreateCell(i);
  983. cel9.SetCellValue(dr["F_Total"].ToString());
  984. cel9.CellStyle = cellStylebt1;
  985. break;
  986. }
  987. }
  988. }
  989. }
  990. return index;
  991. }
  992. /// <summary>
  993. /// 督办数据报表导出
  994. /// </summary>
  995. /// <param name="ds"></param>
  996. /// <returns></returns>
  997. public string ExportPerformance(DataTable dt, DataTable dt1, DataTable dt2, string message)
  998. {
  999. try
  1000. {
  1001. HSSFWorkbook workbook = new HSSFWorkbook();
  1002. ISheet sheet = workbook.CreateSheet("月度评价报表");
  1003. ICellStyle cellStyle = workbook.CreateCellStyle();
  1004. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  1005. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  1006. cellStyle.SetFont(cellfont);
  1007. cellStyle.VerticalAlignment = VerticalAlignment.Center;
  1008. cellStyle.Alignment = HorizontalAlignment.Center;
  1009. ICellStyle cellStylebt1 = workbook.CreateCellStyle();
  1010. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  1011. cellfontbt.Boldweight = (short)FontBoldWeight.Bold;
  1012. cellStylebt1.SetFont(cellfontbt);
  1013. cellStylebt1.VerticalAlignment = VerticalAlignment.Center;
  1014. cellStylebt1.Alignment = HorizontalAlignment.Center;
  1015. IRow headerRow = sheet.CreateRow(0);
  1016. int icolIndex = 0;
  1017. foreach (DataColumn dc in dt.Columns)
  1018. {
  1019. ICell cell = headerRow.CreateCell(icolIndex);
  1020. cell.SetCellValue(dc.ColumnName);
  1021. cell.CellStyle = cellStylebt1;
  1022. icolIndex++;
  1023. }
  1024. ICellStyle cellStylebt = workbook.CreateCellStyle();
  1025. NPOI.SS.UserModel.IFont cellfontbt1 = workbook.CreateFont();
  1026. cellfontbt1.Boldweight = (short)FontBoldWeight.Bold;
  1027. cellStylebt.SetFont(cellfontbt1);
  1028. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  1029. cellStylebt.Alignment = HorizontalAlignment.Center;
  1030. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  1031. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  1032. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1033. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1034. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1035. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1036. cellStyle.SetFont(cellfont);
  1037. //建立内容行
  1038. int iRowIndex = 0;
  1039. iRowIndex++;
  1040. IRow irow01 = sheet.CreateRow(iRowIndex);
  1041. ICell cell01 = irow01.CreateCell(0);
  1042. cell01.SetCellValue("一、县区");
  1043. cell01.CellStyle = cellStylebt;
  1044. irow01.Height = 20 * 20;
  1045. sheet.AddMergedRegion(new CellRangeAddress(iRowIndex , iRowIndex , 0, icolIndex-1));
  1046. foreach (DataRow dr in dt.Rows)
  1047. {
  1048. int iCellIndex = 0;
  1049. IRow irow = sheet.CreateRow(iRowIndex + 1);
  1050. for (int i = 0; i < icolIndex; i++)
  1051. {
  1052. string strsj = string.Empty;
  1053. if (dr[i] != null)
  1054. {
  1055. strsj = dr[i].ToString();
  1056. }
  1057. ICell cell = irow.CreateCell(iCellIndex);
  1058. cell.SetCellValue(strsj);
  1059. cell.CellStyle = cellStyle;
  1060. iCellIndex++;
  1061. }
  1062. iRowIndex++;
  1063. }
  1064. iRowIndex ++;
  1065. IRow irow02 = sheet.CreateRow(iRowIndex);
  1066. ICell cell02 = irow02.CreateCell(0);
  1067. cell02.SetCellValue("二、市直单位");
  1068. cell02.CellStyle = cellStylebt;
  1069. irow02.Height = 20 * 20;
  1070. sheet.AddMergedRegion(new CellRangeAddress(iRowIndex , iRowIndex , 0, icolIndex - 1));
  1071. foreach (DataRow dr in dt1.Rows)
  1072. {
  1073. int iCellIndex = 0;
  1074. IRow irow = sheet.CreateRow(iRowIndex + 1);
  1075. for (int i = 0; i < icolIndex; i++)
  1076. {
  1077. string strsj = string.Empty;
  1078. if (dr[i] != null)
  1079. {
  1080. strsj = dr[i].ToString();
  1081. }
  1082. ICell cell = irow.CreateCell(iCellIndex);
  1083. cell.SetCellValue(strsj);
  1084. cell.CellStyle = cellStyle;
  1085. iCellIndex++;
  1086. }
  1087. iRowIndex++;
  1088. }
  1089. iRowIndex++;
  1090. IRow irow03 = sheet.CreateRow(iRowIndex);
  1091. ICell cell03 = irow03.CreateCell(0);
  1092. cell03.SetCellValue("三、公共服务企业");
  1093. cell03.CellStyle = cellStylebt;
  1094. irow03.Height = 20 * 20;
  1095. sheet.AddMergedRegion(new CellRangeAddress(iRowIndex , iRowIndex, 0, icolIndex - 1));
  1096. foreach (DataRow dr in dt2.Rows)
  1097. {
  1098. int iCellIndex = 0;
  1099. IRow irow = sheet.CreateRow(iRowIndex + 1);
  1100. for (int i = 0; i < icolIndex; i++)
  1101. {
  1102. string strsj = string.Empty;
  1103. if (dr[i] != null)
  1104. {
  1105. strsj = dr[i].ToString();
  1106. }
  1107. ICell cell = irow.CreateCell(iCellIndex);
  1108. cell.SetCellValue(strsj);
  1109. cell.CellStyle = cellStyle;
  1110. iCellIndex++;
  1111. }
  1112. iRowIndex++;
  1113. }
  1114. //自适应列宽度
  1115. for (int i = 0; i < icolIndex; i++)
  1116. {
  1117. // sheet.AutoSizeColumn(i);
  1118. if (i == 0)
  1119. sheet.SetColumnWidth(i, 28 * 256);
  1120. else
  1121. sheet.SetColumnWidth(i, 15 * 256);
  1122. }
  1123. using (MemoryStream ms = new MemoryStream())
  1124. {
  1125. workbook.Write(ms);
  1126. HttpContext curContext = HttpContext.Current;
  1127. // 设置编码和附件格式
  1128. curContext.Response.ContentType = "application/vnd.ms-excel";
  1129. curContext.Response.ContentEncoding = Encoding.UTF8;
  1130. curContext.Response.Charset = "";
  1131. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  1132. curContext.Response.AppendHeader("Content-Disposition",
  1133. "attachment;filename=" + HttpUtility.UrlEncode("月度评价报表" + ".xls", Encoding.UTF8));
  1134. curContext.Response.BinaryWrite(ms.GetBuffer());
  1135. workbook = null;
  1136. ms.Close();
  1137. ms.Dispose();
  1138. curContext.Response.End();
  1139. }
  1140. return "";
  1141. }
  1142. catch
  1143. {
  1144. return "导出失败!";
  1145. }
  1146. }
  1147. /// <summary>
  1148. /// 督办数据报表导出
  1149. /// </summary>
  1150. /// <param name="ds"></param>
  1151. /// <returns></returns>
  1152. public string ExportEvaluation(DataTable dt, DataTable dt1, DataTable dt2, DataTable dt3, string message,string data)
  1153. {
  1154. try
  1155. {
  1156. HSSFWorkbook workbook = new HSSFWorkbook();
  1157. ISheet sheet = workbook.CreateSheet("月度评价报表");
  1158. ICellStyle cellStyle = workbook.CreateCellStyle();
  1159. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  1160. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  1161. cellStyle.SetFont(cellfont);
  1162. ICellStyle cellStylebt = workbook.CreateCellStyle();
  1163. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  1164. cellfontbt.Boldweight = (short)FontBoldWeight.Bold;
  1165. cellStylebt.SetFont(cellfontbt);
  1166. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  1167. cellStylebt.Alignment = HorizontalAlignment.Center;
  1168. IRow irow0 = sheet.CreateRow(0);
  1169. ICell cell0 = irow0.CreateCell(0);
  1170. cell0.SetCellValue(data+"月份市长热线承办单位办理情况统计表");
  1171. cell0.CellStyle = cellStylebt;
  1172. irow0.Height = 50 * 20;
  1173. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 14));
  1174. IRow irow00 = sheet.CreateRow(1);
  1175. ICell cell00 = irow00.CreateCell(0);
  1176. cell00.SetCellValue(message);
  1177. cell00.CellStyle = cellStylebt;
  1178. irow00.Height = 40 * 20;
  1179. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 0, 14));
  1180. int index1 = Evaluation("一、县区", dt, 2, sheet, cellStylebt, workbook);
  1181. int index2 = Evaluation("二、市直单位", dt1, index1+1, sheet, cellStylebt, workbook);
  1182. int index3 = Evaluation("三、公共服务企业", dt2, index2+1, sheet, cellStylebt, workbook);
  1183. int index4 = Evaluation("四、未办件单位", dt3, index3 + 1, sheet, cellStylebt, workbook);
  1184. //自适应列宽度
  1185. for (int i = 0; i < 14; i++)
  1186. {
  1187. // sheet.AutoSizeColumn(i);
  1188. if (i ==1)
  1189. sheet.SetColumnWidth(i, 28 * 256);
  1190. else
  1191. sheet.SetColumnWidth(i, 15 * 256);
  1192. }
  1193. using (MemoryStream ms = new MemoryStream())
  1194. {
  1195. workbook.Write(ms);
  1196. HttpContext curContext = HttpContext.Current;
  1197. // 设置编码和附件格式
  1198. curContext.Response.ContentType = "application/vnd.ms-excel";
  1199. curContext.Response.ContentEncoding = Encoding.UTF8;
  1200. curContext.Response.Charset = "";
  1201. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  1202. curContext.Response.AppendHeader("Content-Disposition",
  1203. "attachment;filename=" + HttpUtility.UrlEncode("月度评价报表" + ".xls", Encoding.UTF8));
  1204. curContext.Response.BinaryWrite(ms.GetBuffer());
  1205. workbook = null;
  1206. ms.Close();
  1207. ms.Dispose();
  1208. curContext.Response.End();
  1209. }
  1210. return "";
  1211. }
  1212. catch
  1213. {
  1214. return "导出失败!";
  1215. }
  1216. }
  1217. /// <summary>
  1218. /// 督办数据报表导出
  1219. /// </summary>
  1220. /// <param name="ds"></param>
  1221. /// <returns></returns>
  1222. public string DBExportToExcel(DataTable dt,string Name,string month,string starttime ,string endtime )
  1223. {
  1224. try
  1225. {
  1226. HSSFWorkbook workbook = new HSSFWorkbook();
  1227. ISheet sheet = workbook.CreateSheet(Name);
  1228. ICellStyle cellStyle = workbook.CreateCellStyle();
  1229. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  1230. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  1231. cellStyle.SetFont(cellfont);
  1232. ICellStyle cellStylebt = workbook.CreateCellStyle();
  1233. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  1234. cellfontbt.Boldweight = (short)FontBoldWeight.Bold;
  1235. cellStylebt.SetFont(cellfontbt);
  1236. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  1237. cellStylebt.Alignment = HorizontalAlignment.Center;
  1238. IRow irow1 = sheet.CreateRow(0);
  1239. ICell cell1 = irow1.CreateCell(0);
  1240. cell1.SetCellValue("12345联动服务工作"+ month + "月份办理情况通报表");
  1241. cell1.CellStyle = cellStylebt;
  1242. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 0, 19));
  1243. IRow irow2 = sheet.CreateRow(2);
  1244. ICell cell2 = irow2.CreateCell(0);
  1245. cell2.SetCellValue("统计周期:"+ starttime+"至"+ endtime+" 统计时间:"+DateTime .Now.ToString ("yyyy年MM月dd日"));
  1246. cell2.CellStyle = cellStylebt;
  1247. sheet.AddMergedRegion(new CellRangeAddress(2, 2, 0, 19));
  1248. IRow irow3 = sheet.CreateRow(3);
  1249. ICell cell3 = irow3.CreateCell(0);
  1250. cell3.SetCellValue("一、县(市、区)联动单位");
  1251. cell3.CellStyle = cellStylebt;
  1252. sheet.AddMergedRegion(new CellRangeAddress(3, 3, 0, 19));
  1253. string[] cols = {"序号","联动单位","承办件",
  1254. "承办率","得分(5分)","超期件","按时反馈率","得分(20分)",
  1255. "未果件","办结率","得分(10分)","退单件","有效回复率",
  1256. "得分(15分)","群众评议总数","一次不满意件","二次不满意件","不满意件","满意率","得分(50分)","总分","排名"};
  1257. IRow irow4 = sheet.CreateRow(4);
  1258. int icolIndex = 0;
  1259. foreach (string dc in cols)
  1260. {
  1261. ICell cell = irow4.CreateCell(icolIndex);
  1262. cell.SetCellValue(dc);
  1263. cell.CellStyle = cellStylebt;
  1264. icolIndex++;
  1265. }
  1266. int iRowIndex = 5;
  1267. DataRow[] rows = dt.Select("category=1");
  1268. int index = 0;
  1269. foreach (DataRow dr in rows)
  1270. {
  1271. index++;
  1272. int iCellIndex = 0;
  1273. IRow irow = sheet.CreateRow(iRowIndex );
  1274. for (int i = 0; i < 22; i++)
  1275. {
  1276. ICell cell = irow.CreateCell(iCellIndex);
  1277. cell.SetCellValue(GetCellValue(dr, index,i ));
  1278. cell.CellStyle = cellStyle;
  1279. iCellIndex++;
  1280. }
  1281. iRowIndex++;
  1282. }
  1283. IRow irow5 = sheet.CreateRow(iRowIndex);
  1284. ICell cell5 = irow5.CreateCell(0);
  1285. cell5.SetCellValue("二、市直机关联动单位一组");
  1286. cell5.CellStyle = cellStylebt;
  1287. sheet.AddMergedRegion(new CellRangeAddress(iRowIndex, iRowIndex, 0, 19));
  1288. iRowIndex++;
  1289. IRow irow6 = sheet.CreateRow(iRowIndex);
  1290. icolIndex = 0;
  1291. foreach (string dc in cols)
  1292. {
  1293. ICell cell = irow6.CreateCell(icolIndex);
  1294. cell.SetCellValue(dc);
  1295. cell.CellStyle = cellStylebt;
  1296. icolIndex++;
  1297. }
  1298. iRowIndex++;
  1299. rows = dt.Select("category=2");
  1300. index = 0;
  1301. foreach (DataRow dr in rows)
  1302. {
  1303. index++;
  1304. int iCellIndex = 0;
  1305. IRow irow = sheet.CreateRow(iRowIndex);
  1306. for (int i = 0; i < 22; i++)
  1307. {
  1308. ICell cell = irow.CreateCell(iCellIndex);
  1309. cell.SetCellValue(GetCellValue(dr, index,i ));
  1310. cell.CellStyle = cellStyle;
  1311. iCellIndex++;
  1312. }
  1313. iRowIndex++;
  1314. }
  1315. IRow irow7 = sheet.CreateRow(iRowIndex);
  1316. ICell cell7 = irow7.CreateCell(0);
  1317. cell7.SetCellValue("三、市直机关联动单位二组");
  1318. cell7.CellStyle = cellStylebt;
  1319. sheet.AddMergedRegion(new CellRangeAddress(iRowIndex, iRowIndex, 0, 19));
  1320. iRowIndex++;
  1321. IRow irow8 = sheet.CreateRow(iRowIndex);
  1322. icolIndex = 0;
  1323. foreach (string dc in cols)
  1324. {
  1325. ICell cell = irow8.CreateCell(icolIndex);
  1326. cell.SetCellValue(dc);
  1327. cell.CellStyle = cellStylebt;
  1328. icolIndex++;
  1329. }
  1330. iRowIndex++;
  1331. rows = dt.Select("category=3");
  1332. index = 0;
  1333. foreach (DataRow dr in rows)
  1334. {
  1335. index++;
  1336. int iCellIndex = 0;
  1337. IRow irow = sheet.CreateRow(iRowIndex);
  1338. for (int i = 0; i < 22; i++)
  1339. {
  1340. ICell cell = irow.CreateCell(iCellIndex);
  1341. cell.SetCellValue(GetCellValue(dr, index,i ));
  1342. cell.CellStyle = cellStyle;
  1343. iCellIndex++;
  1344. }
  1345. iRowIndex++;
  1346. }
  1347. IRow irow9 = sheet.CreateRow(iRowIndex);
  1348. ICell cell9 = irow9.CreateCell(0);
  1349. cell9.SetCellValue("四、公益型企业联动单位");
  1350. cell9.CellStyle = cellStylebt;
  1351. sheet.AddMergedRegion(new CellRangeAddress(iRowIndex, iRowIndex, 0, 19));
  1352. iRowIndex++;
  1353. IRow irow10 = sheet.CreateRow(iRowIndex);
  1354. icolIndex = 0;
  1355. foreach (string dc in cols)
  1356. {
  1357. ICell cell = irow10.CreateCell(icolIndex);
  1358. cell.SetCellValue(dc);
  1359. cell.CellStyle = cellStylebt;
  1360. icolIndex++;
  1361. }
  1362. iRowIndex++;
  1363. rows = dt.Select("category=4");
  1364. index = 0;
  1365. foreach (DataRow dr in rows)
  1366. {
  1367. index++;
  1368. int iCellIndex = 0;
  1369. IRow irow = sheet.CreateRow(iRowIndex);
  1370. for (int i = 0; i < 22; i++)
  1371. {
  1372. ICell cell = irow.CreateCell(iCellIndex);
  1373. cell.SetCellValue(GetCellValue(dr, index,i ));
  1374. cell.CellStyle = cellStyle;
  1375. iCellIndex++;
  1376. }
  1377. iRowIndex++;
  1378. }
  1379. //自适应列宽度
  1380. for (int i = 0; i < 20; i++)
  1381. {
  1382. sheet.AutoSizeColumn(i);
  1383. // sheet.SetColumnWidth(i, 20 * 256);
  1384. }
  1385. using (MemoryStream ms = new MemoryStream())
  1386. {
  1387. workbook.Write(ms);
  1388. HttpContext curContext = HttpContext.Current;
  1389. // 设置编码和附件格式
  1390. curContext.Response.ContentType = "application/vnd.ms-excel";
  1391. curContext.Response.ContentEncoding = Encoding.UTF8;
  1392. curContext.Response.Charset = "";
  1393. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  1394. curContext.Response.AppendHeader("Content-Disposition",
  1395. "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
  1396. curContext.Response.BinaryWrite(ms.GetBuffer());
  1397. workbook = null;
  1398. ms.Close();
  1399. ms.Dispose();
  1400. curContext.Response.End();
  1401. }
  1402. return "";
  1403. }
  1404. catch
  1405. {
  1406. return "导出失败!";
  1407. }
  1408. }
  1409. /// <summary>
  1410. /// 督办数据报表导出
  1411. /// </summary>
  1412. /// <param name="ds"></param>
  1413. /// <returns></returns>
  1414. public string DBEJExportToExcel(DataTable dt, string Name, string month, string starttime, string endtime)
  1415. {
  1416. try
  1417. {
  1418. HSSFWorkbook workbook = new HSSFWorkbook();
  1419. ISheet sheet = workbook.CreateSheet(Name);
  1420. ICellStyle cellStyle = workbook.CreateCellStyle();
  1421. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  1422. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  1423. cellStyle.SetFont(cellfont);
  1424. ICellStyle cellStylebt = workbook.CreateCellStyle();
  1425. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  1426. cellfontbt.Boldweight = (short)FontBoldWeight.Bold;
  1427. cellStylebt.SetFont(cellfontbt);
  1428. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  1429. cellStylebt.Alignment = HorizontalAlignment.Center;
  1430. IRow irow1 = sheet.CreateRow(0);
  1431. ICell cell1 = irow1.CreateCell(0);
  1432. cell1.SetCellValue("12345联动服务工作" + month + "月份办理情况通报表");
  1433. cell1.CellStyle = cellStylebt;
  1434. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 0, 19));
  1435. IRow irow2 = sheet.CreateRow(2);
  1436. ICell cell2 = irow2.CreateCell(0);
  1437. cell2.SetCellValue("统计周期:" + starttime + "至" + endtime + " 统计时间:" + DateTime.Now.ToString("yyyy年MM月dd日"));
  1438. cell2.CellStyle = cellStylebt;
  1439. sheet.AddMergedRegion(new CellRangeAddress(2, 2, 0, 19));
  1440. IRow irow3 = sheet.CreateRow(3);
  1441. ICell cell3 = irow3.CreateCell(0);
  1442. cell3.SetCellValue("一、县(市、区)联动单位");
  1443. cell3.CellStyle = cellStylebt;
  1444. sheet.AddMergedRegion(new CellRangeAddress(3, 3, 0, 19));
  1445. string[] cols = {"序号","联动单位","承办件",
  1446. "承办率","得分(5分)","超期件","按时反馈率","得分(20分)",
  1447. "未果件","办结率","得分(10分)","退单件","有效回复率",
  1448. "得分(15分)","群众评议总数","一次不满意件","不满意件","满意率","得分(50分)","总分","排名"};
  1449. IRow irow4 = sheet.CreateRow(4);
  1450. int icolIndex = 0;
  1451. foreach (string dc in cols)
  1452. {
  1453. ICell cell = irow4.CreateCell(icolIndex);
  1454. cell.SetCellValue(dc);
  1455. cell.CellStyle = cellStylebt;
  1456. icolIndex++;
  1457. }
  1458. int iRowIndex = 5;
  1459. // DataRow[] rows = dt.Select("category=1");
  1460. int index = 0;
  1461. foreach (DataRow dr in dt.Rows )
  1462. {
  1463. index++;
  1464. int iCellIndex = 0;
  1465. IRow irow = sheet.CreateRow(iRowIndex);
  1466. for (int i = 0; i < 21; i++)
  1467. {
  1468. ICell cell = irow.CreateCell(iCellIndex);
  1469. cell.SetCellValue(GetCellValue(dr, index, i));
  1470. cell.CellStyle = cellStyle;
  1471. iCellIndex++;
  1472. }
  1473. iRowIndex++;
  1474. }
  1475. //自适应列宽度
  1476. for (int i = 0; i < 20; i++)
  1477. {
  1478. sheet.AutoSizeColumn(i);
  1479. // sheet.SetColumnWidth(i, 20 * 256);
  1480. }
  1481. using (MemoryStream ms = new MemoryStream())
  1482. {
  1483. workbook.Write(ms);
  1484. HttpContext curContext = HttpContext.Current;
  1485. // 设置编码和附件格式
  1486. curContext.Response.ContentType = "application/vnd.ms-excel";
  1487. curContext.Response.ContentEncoding = Encoding.UTF8;
  1488. curContext.Response.Charset = "";
  1489. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  1490. curContext.Response.AppendHeader("Content-Disposition",
  1491. "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
  1492. curContext.Response.BinaryWrite(ms.GetBuffer());
  1493. workbook = null;
  1494. ms.Close();
  1495. ms.Dispose();
  1496. curContext.Response.End();
  1497. }
  1498. return "";
  1499. }
  1500. catch
  1501. {
  1502. return "导出失败!";
  1503. }
  1504. }
  1505. //
  1506. /// <summary>
  1507. /// 督办数据报表导出
  1508. /// </summary>
  1509. /// <param name="ds"></param>
  1510. /// <returns></returns>
  1511. public string ExportMergerToExcel(List<MergerHot.MergerHotSource> MergerHotSource)
  1512. {
  1513. try
  1514. {
  1515. int lenth = 0;
  1516. if (MergerHotSource!=null&& MergerHotSource.Count>0)
  1517. {
  1518. if (MergerHotSource[0]!=null && MergerHotSource[0].MergerHot !=null
  1519. && MergerHotSource[0].MergerHot.Count >0)
  1520. {
  1521. lenth = MergerHotSource[0].MergerHot.Count * 5+4;
  1522. }
  1523. else
  1524. {
  1525. return "";
  1526. }
  1527. }
  1528. else
  1529. {
  1530. return "";
  1531. }
  1532. HSSFWorkbook workbook = new HSSFWorkbook();
  1533. ISheet sheet = workbook.CreateSheet("归并热线每日数据统计");
  1534. ICellStyle cellStyle = workbook.CreateCellStyle();
  1535. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  1536. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  1537. cellStyle.SetFont(cellfont);
  1538. ICellStyle cellStylebt = workbook.CreateCellStyle();
  1539. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  1540. cellfontbt.Boldweight = (short)FontBoldWeight.Bold;
  1541. cellStylebt.SetFont(cellfontbt);
  1542. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  1543. cellStylebt.Alignment = HorizontalAlignment.Center;
  1544. cellStylebt.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1545. cellStylebt.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1546. cellStylebt.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1547. cellStylebt.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1548. IRow irow1 = sheet.CreateRow(0);
  1549. ICell cell1 = irow1.CreateCell(0);
  1550. cell1.SetCellValue("归并热线每日数据统计");
  1551. cell1.CellStyle=cellStylebt;
  1552. ICellStyle cellStylebt1 = workbook.CreateCellStyle();
  1553. NPOI.SS.UserModel.IFont cellfontbt1 = workbook.CreateFont();
  1554. cellfontbt1.Boldweight = (short)FontBoldWeight.Bold;
  1555. cellfontbt1.FontHeightInPoints = 22;
  1556. cellStylebt1.SetFont(cellfontbt1);
  1557. cellStylebt1.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1558. cellStylebt1.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1559. cellStylebt1.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1560. cellStylebt1.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1561. cellStylebt1.VerticalAlignment = VerticalAlignment.Center;
  1562. cellStylebt1.Alignment = HorizontalAlignment.Center;
  1563. cell1.CellStyle = cellStylebt1;
  1564. sheet.AddMergedRegion(new CellRangeAddress(0, 2, 0, lenth));
  1565. IRow irow2 = sheet.CreateRow(3);
  1566. int icolIndex = 0;
  1567. ICellStyle cellStylebt2 = workbook.CreateCellStyle();
  1568. NPOI.SS.UserModel.IFont cellfontbt2 = workbook.CreateFont();
  1569. cellfontbt2.Boldweight = (short)FontBoldWeight.Bold;
  1570. cellStylebt2.WrapText = true;//设置换行这个要先设置
  1571. cellfontbt2.FontHeightInPoints = 14;
  1572. cellStylebt2.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1573. cellStylebt2.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1574. cellStylebt2.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1575. cellStylebt2.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1576. cellStylebt2.SetFont(cellfontbt2);
  1577. cellStylebt2.VerticalAlignment = VerticalAlignment.Center;
  1578. cellStylebt2.Alignment = HorizontalAlignment.Center;
  1579. HSSFPalette palette = workbook.GetCustomPalette(); //调色板实例
  1580. palette.SetColorAtIndex(8, 237,237, 237);
  1581. NPOI.HSSF.Util.HSSFColor hssFColor = palette.FindColor(237, 237, 237);
  1582. cellStylebt2.FillForegroundColor = hssFColor.Indexed ;
  1583. cellStylebt2.FillPattern = FillPattern.SolidForeground;
  1584. cellStylebt2.FillBackgroundColor = hssFColor.Indexed; ;
  1585. ICellStyle cellStylebt3 = workbook.CreateCellStyle();
  1586. NPOI.SS.UserModel.IFont cellfontbt3 = workbook.CreateFont();
  1587. cellfontbt3.Boldweight = (short)FontBoldWeight.Bold;
  1588. cellStylebt3.WrapText = true;//设置换行这个要先设置
  1589. cellfontbt3.FontHeightInPoints = 14;
  1590. cellStylebt3.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1591. cellStylebt3.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1592. cellStylebt3.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1593. cellStylebt3.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1594. cellStylebt3.SetFont(cellfontbt3);
  1595. cellStylebt3.VerticalAlignment = VerticalAlignment.Center;
  1596. cellStylebt3.Alignment = HorizontalAlignment.Center;
  1597. HSSFPalette palette1 = workbook.GetCustomPalette(); //调色板实例
  1598. palette1.SetColorAtIndex(9, 221,235,247);
  1599. NPOI.HSSF.Util.HSSFColor hssFColor1 = palette1.FindColor(221, 235, 247);
  1600. cellStylebt3.FillForegroundColor = hssFColor1.Indexed ;
  1601. cellStylebt3.FillPattern = FillPattern.SolidForeground;
  1602. cellStylebt3.FillBackgroundColor = hssFColor1.Indexed;
  1603. ICellStyle cellStylebt4 = workbook.CreateCellStyle();
  1604. NPOI.SS.UserModel.IFont cellfontbt4 = workbook.CreateFont();
  1605. cellStylebt4.WrapText = true;//设置换行这个要先设置
  1606. cellfontbt4.Boldweight = (short)FontBoldWeight.Bold;
  1607. cellfontbt4.FontHeightInPoints = 11;
  1608. cellStylebt4.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1609. cellStylebt4.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1610. cellStylebt4.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1611. cellStylebt4.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1612. cellStylebt4.SetFont(cellfontbt4);
  1613. cellStylebt4.VerticalAlignment = VerticalAlignment.Center;
  1614. cellStylebt4.Alignment = HorizontalAlignment.Center;
  1615. ICellStyle cellStylebt5 = workbook.CreateCellStyle();
  1616. NPOI.SS.UserModel.IFont cellfontbt5 = workbook.CreateFont();
  1617. cellfontbt5.Boldweight = (short)FontBoldWeight.Bold;
  1618. cellfontbt5.FontHeightInPoints = 11;
  1619. cellStylebt5.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1620. cellStylebt5.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1621. cellStylebt5.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1622. cellStylebt5.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1623. cellStylebt5.WrapText = true;//设置换行这个要先设置
  1624. cellStylebt5.SetFont(cellfontbt5);
  1625. cellStylebt5.VerticalAlignment = VerticalAlignment.Center;
  1626. cellStylebt5.Alignment = HorizontalAlignment.Center;
  1627. HSSFPalette palette2 = workbook.GetCustomPalette(); //调色板实例
  1628. palette2.SetColorAtIndex(10, 226, 239, 218);
  1629. NPOI.HSSF.Util.HSSFColor hssFColor2 = palette2.FindColor(226, 239, 218);
  1630. cellStylebt5.FillForegroundColor = hssFColor2.Indexed ;
  1631. cellStylebt5.FillPattern = FillPattern.SolidForeground;
  1632. cellStylebt5.FillBackgroundColor = hssFColor2.Indexed;
  1633. ICellStyle cellStylebt8 = workbook.CreateCellStyle();
  1634. NPOI.SS.UserModel.IFont cellfontbt8 = workbook.CreateFont();
  1635. cellfontbt8.FontHeightInPoints = 10;
  1636. cellStylebt8.SetFont(cellfontbt8);
  1637. cellStylebt8.WrapText = true;//设置换行这个要先设置
  1638. cellStylebt8.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1639. cellStylebt8.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1640. cellStylebt8.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1641. cellStylebt8.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1642. cellStylebt8.VerticalAlignment = VerticalAlignment.Center;
  1643. cellStylebt8.Alignment = HorizontalAlignment.Center;
  1644. HSSFPalette palette3 = workbook.GetCustomPalette(); //调色板实例
  1645. palette3.SetColorAtIndex(11, 198, 224, 180);
  1646. NPOI.HSSF.Util.HSSFColor hssFColor3 = palette3.FindColor(198, 224, 180);
  1647. cellStylebt8.FillForegroundColor = hssFColor3.Indexed ;
  1648. cellStylebt8.FillPattern = FillPattern.SolidForeground;
  1649. cellStylebt8.FillBackgroundColor = hssFColor3.Indexed;
  1650. ICellStyle cellStylebt9 = workbook.CreateCellStyle();
  1651. NPOI.SS.UserModel.IFont cellfontbt9 = workbook.CreateFont();
  1652. cellfontbt9.FontHeightInPoints = 11;
  1653. cellfontbt9.Boldweight = (short)FontBoldWeight.Bold;
  1654. cellStylebt9.SetFont(cellfontbt9);
  1655. cellStylebt9.WrapText = true;//设置换行这个要先设置
  1656. cellStylebt9.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1657. cellStylebt9.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1658. cellStylebt9.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1659. cellStylebt9.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1660. cellStylebt9.VerticalAlignment = VerticalAlignment.Center;
  1661. cellStylebt9.Alignment = HorizontalAlignment.Center;
  1662. HSSFPalette palette4 = workbook.GetCustomPalette(); //调色板实例
  1663. palette4.SetColorAtIndex(12, 255, 242, 204);
  1664. NPOI.HSSF.Util.HSSFColor hssFColor4 = palette4.FindColor(255, 242, 204);
  1665. cellStylebt9.FillForegroundColor = hssFColor4.Indexed ; ;
  1666. cellStylebt9.FillPattern = FillPattern.SolidForeground;
  1667. cellStylebt9.FillBackgroundColor = hssFColor4.Indexed; ; ;
  1668. ICellStyle cellStylebt10 = workbook.CreateCellStyle();
  1669. NPOI.SS.UserModel.IFont cellfontbt10 = workbook.CreateFont();
  1670. cellfontbt10.Boldweight = (short)FontBoldWeight.Bold;
  1671. cellfontbt10.FontHeightInPoints = 11;
  1672. cellStylebt10.WrapText = true;//设置换行这个要先设置
  1673. cellStylebt10.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1674. cellStylebt10.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1675. cellStylebt10.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1676. cellStylebt10.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1677. cellStylebt10.SetFont(cellfontbt10);
  1678. cellStylebt10.VerticalAlignment = VerticalAlignment.Center;
  1679. cellStylebt10.Alignment = HorizontalAlignment.Center;
  1680. HSSFPalette palette5 = workbook.GetCustomPalette(); //调色板实例
  1681. palette5.SetColorAtIndex(13, 217, 225, 242);
  1682. NPOI.HSSF.Util.HSSFColor hssFColor5 = palette5.FindColor(217, 225, 242);
  1683. cellStylebt10.FillForegroundColor = hssFColor5.Indexed ;
  1684. cellStylebt10.FillPattern = FillPattern.SolidForeground;
  1685. cellStylebt10.FillBackgroundColor = hssFColor5.Indexed; ;
  1686. ICellStyle cellStylebt6 = workbook.CreateCellStyle();
  1687. NPOI.SS.UserModel.IFont cellfontbt6 = workbook.CreateFont();
  1688. cellfontbt6.Boldweight = (short)FontBoldWeight.Bold;
  1689. cellStylebt6.WrapText = true;//设置换行这个要先设置
  1690. cellfontbt6.FontHeightInPoints = 10;
  1691. cellStylebt6.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1692. cellStylebt6.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1693. cellStylebt6.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1694. cellStylebt6.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1695. cellStylebt6.SetFont(cellfontbt6);
  1696. cellStylebt6.VerticalAlignment = VerticalAlignment.Center;
  1697. cellStylebt6.Alignment = HorizontalAlignment.Center;
  1698. cellStylebt6.FillForegroundColor = hssFColor.Indexed ;
  1699. cellStylebt6.FillPattern = FillPattern.SolidForeground;
  1700. cellStylebt6.FillBackgroundColor = hssFColor.Indexed; ;
  1701. ICellStyle cellStylebt11 = workbook.CreateCellStyle();
  1702. NPOI.SS.UserModel.IFont cellfontbt11 = workbook.CreateFont();
  1703. cellfontbt11.FontHeightInPoints = 10;
  1704. cellStylebt11.WrapText = true;//设置换行这个要先设置
  1705. cellStylebt11.SetFont(cellfontbt11);
  1706. cellStylebt11.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1707. cellStylebt11.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1708. cellStylebt11.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1709. cellStylebt11.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1710. cellStylebt11.VerticalAlignment = VerticalAlignment.Center;
  1711. cellStylebt11.Alignment = HorizontalAlignment.Center;
  1712. ICellStyle cellStylebt12 = workbook.CreateCellStyle();
  1713. NPOI.SS.UserModel.IFont cellfontbt12 = workbook.CreateFont();
  1714. cellfontbt12.Boldweight = (short)FontBoldWeight.Bold;
  1715. cellfontbt12.FontHeightInPoints = 10;
  1716. cellStylebt12.WrapText = true;//设置换行这个要先设置
  1717. cellStylebt12.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1718. cellStylebt12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1719. cellStylebt12.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1720. cellStylebt12.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1721. cellStylebt12.SetFont(cellfontbt12);
  1722. cellStylebt12.VerticalAlignment = VerticalAlignment.Center;
  1723. cellStylebt12.Alignment = HorizontalAlignment.Center;
  1724. cellStylebt12.FillForegroundColor = hssFColor5.Indexed;
  1725. cellStylebt12.FillPattern = FillPattern.SolidForeground;
  1726. cellStylebt12.FillBackgroundColor = hssFColor5.Indexed; ;
  1727. ICellStyle cellStylebt13 = workbook.CreateCellStyle();
  1728. NPOI.SS.UserModel.IFont cellfontbt13 = workbook.CreateFont();
  1729. cellfontbt13.Boldweight = (short)FontBoldWeight.Bold;
  1730. cellStylebt13.WrapText = true;//设置换行这个要先设置
  1731. cellfontbt13.FontHeightInPoints = 11;
  1732. cellStylebt13.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1733. cellStylebt13.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1734. cellStylebt13.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1735. cellStylebt13.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1736. cellStylebt13.SetFont(cellfontbt13);
  1737. cellStylebt13.VerticalAlignment = VerticalAlignment.Center;
  1738. cellStylebt13.Alignment = HorizontalAlignment.Center;
  1739. cellStylebt13.FillForegroundColor = hssFColor.Indexed;
  1740. cellStylebt13.FillPattern = FillPattern.SolidForeground;
  1741. cellStylebt13.FillBackgroundColor = hssFColor.Indexed; ;
  1742. ICellStyle cellStylebt14 = workbook.CreateCellStyle();
  1743. NPOI.SS.UserModel.IFont cellfontbt14 = workbook.CreateFont();
  1744. cellfontbt14.FontHeightInPoints = 10;
  1745. cellStylebt14.SetFont(cellfontbt14);
  1746. cellStylebt14.WrapText = true;//设置换行这个要先设置
  1747. cellStylebt14.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1748. cellStylebt14.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1749. cellStylebt14.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1750. cellStylebt14.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1751. cellStylebt14.VerticalAlignment = VerticalAlignment.Center;
  1752. cellStylebt14.Alignment = HorizontalAlignment.Center;
  1753. cellStylebt14.FillForegroundColor = hssFColor4.Indexed; ;
  1754. cellStylebt14.FillPattern = FillPattern.SolidForeground;
  1755. cellStylebt14.FillBackgroundColor = hssFColor4.Indexed; ; ;
  1756. ICellStyle cellStylebt15 = workbook.CreateCellStyle();
  1757. NPOI.SS.UserModel.IFont cellfontbt15 = workbook.CreateFont();
  1758. cellfontbt15.Boldweight = (short)FontBoldWeight.Bold;
  1759. cellfontbt15.FontHeightInPoints = 10;
  1760. cellStylebt15.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1761. cellStylebt15.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1762. cellStylebt15.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1763. cellStylebt15.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1764. cellStylebt15.WrapText = true;//设置换行这个要先设置
  1765. cellStylebt15.SetFont(cellfontbt15);
  1766. cellStylebt15.VerticalAlignment = VerticalAlignment.Center;
  1767. cellStylebt15.Alignment = HorizontalAlignment.Center;
  1768. cellStylebt15.FillForegroundColor = hssFColor2.Indexed;
  1769. cellStylebt15.FillPattern = FillPattern.SolidForeground;
  1770. cellStylebt15.FillBackgroundColor = hssFColor2.Indexed;
  1771. int indexsource = -1;
  1772. for (int i = 0; i <= MergerHotSource[0].MergerHot.Count
  1773. ;i++)
  1774. {
  1775. if (i == 0)
  1776. {
  1777. ICell cell = irow2.CreateCell(0);
  1778. cell.SetCellValue("来源");
  1779. cell.CellStyle = cellStylebt2;
  1780. indexsource = 0;
  1781. sheet.SetColumnWidth(0, 12 * 300);
  1782. }
  1783. else if (i >0&&i <= MergerHotSource[0].MergerHot.Count-4)
  1784. {
  1785. ICell cell = irow2.CreateCell(indexsource+1);
  1786. cell.SetCellValue(MergerHotSource[0].MergerHot[i - 1].Source);
  1787. cell.CellStyle = cellStylebt2;
  1788. if (i ==1 )
  1789. {
  1790. indexsource = 6;
  1791. for (int z = 2; z <= indexsource - 1; z++)
  1792. {
  1793. ICell cell4 = irow2.CreateCell(z);
  1794. cell4.CellStyle = cellStylebt2;
  1795. }
  1796. sheet.AddMergedRegion(new CellRangeAddress(3, 3, 1, indexsource - 1));
  1797. }
  1798. else
  1799. {
  1800. for (int z = indexsource + 2; z <= indexsource + 5; z++)
  1801. {
  1802. ICell cell4 = irow2.CreateCell(z);
  1803. cell4.CellStyle = cellStylebt2;
  1804. }
  1805. sheet.AddMergedRegion(new CellRangeAddress(3, 3, indexsource + 1, indexsource + 5));
  1806. indexsource += 6;
  1807. }
  1808. }
  1809. else
  1810. {
  1811. ICell cell = irow2.CreateCell(indexsource + 1);
  1812. cell.SetCellValue(MergerHotSource[0].MergerHot[i - 1].Source);
  1813. if (i == MergerHotSource[0].MergerHot.Count - 3)
  1814. {
  1815. for (int z = indexsource + 2; z <= indexsource + 6; z++)
  1816. {
  1817. ICell cell4 = irow2.CreateCell(z);
  1818. cell4.CellStyle = cellStylebt2;
  1819. }
  1820. sheet.AddMergedRegion(new CellRangeAddress(3, 3, indexsource + 1, indexsource + 6));
  1821. cell.CellStyle = cellStylebt3;
  1822. indexsource += 7;
  1823. }
  1824. else
  1825. {
  1826. for (int z = indexsource + 2; z <= indexsource + 5; z++)
  1827. {
  1828. ICell cell4 = irow2.CreateCell(z);
  1829. cell4.CellStyle = cellStylebt2;
  1830. }
  1831. sheet.AddMergedRegion(new CellRangeAddress(3, 3, indexsource + 1, indexsource + 5));
  1832. cell.CellStyle = cellStylebt3;
  1833. indexsource += 6;
  1834. }
  1835. }
  1836. }
  1837. IRow irow3 = sheet.CreateRow(4);
  1838. indexsource = 0;
  1839. sheet.CreateRow(4).Height = 200 * 8;
  1840. for (int i = 0; i <= MergerHotSource[0].MergerHot.Count; i++)
  1841. {
  1842. if (i == 0)
  1843. {
  1844. ICell cell = irow3.CreateCell(0);
  1845. cell.SetCellValue("日期");
  1846. cell.CellStyle = cellStylebt4;
  1847. }
  1848. else if (i > 0 && i <= MergerHotSource[0].MergerHot.Count - 4)
  1849. {
  1850. for (int j = 0; j < 5; j++)
  1851. {
  1852. indexsource += 1;
  1853. ICell cell = irow3.CreateCell(indexsource);
  1854. switch (j)
  1855. {
  1856. case 0:
  1857. cell.SetCellValue("来\n电\n量\n");
  1858. cell.CellStyle = cellStylebt5;
  1859. sheet.AutoSizeColumn(indexsource);
  1860. //sheet.SetColumnWidth(indexsource, 12 * 150);
  1861. break;
  1862. case 1:
  1863. cell.SetCellValue("接\n通\n量\n");
  1864. cell.CellStyle = cellStylebt9;
  1865. sheet.AutoSizeColumn(indexsource);
  1866. // sheet.SetColumnWidth(indexsource, 12 * 150);
  1867. break;
  1868. case 2:
  1869. cell.SetCellValue("未\n接\n通\n");
  1870. cell.CellStyle = cellStylebt4;
  1871. sheet.AutoSizeColumn(indexsource);
  1872. // sheet.SetColumnWidth(indexsource, 12 * 150);
  1873. break;
  1874. case 3:
  1875. cell.SetCellValue("主\n动\n放\n弃\n");
  1876. cell.CellStyle = cellStylebt4;
  1877. sheet.AutoSizeColumn(indexsource);
  1878. // sheet.SetColumnWidth(indexsource, 12 * 150);
  1879. break;
  1880. case 4:
  1881. cell.SetCellValue("接\n通\n率");
  1882. cell.CellStyle = cellStylebt4;
  1883. sheet.AutoSizeColumn(indexsource);
  1884. break;
  1885. }
  1886. }
  1887. indexsource += 1;
  1888. sheet.SetColumnWidth(indexsource, 12 * 35);
  1889. }
  1890. else
  1891. {
  1892. if (MergerHotSource[0].MergerHot[i - 1].Source == "12345")
  1893. {
  1894. for (int j = 0; j < 5; j++)
  1895. {
  1896. indexsource += 1;
  1897. ICell cell = irow3.CreateCell(indexsource);
  1898. switch (j)
  1899. {
  1900. case 0:
  1901. cell.SetCellValue("来\n电\n量\n");
  1902. cell.CellStyle = cellStylebt5;
  1903. sheet.AutoSizeColumn(indexsource);
  1904. // sheet.SetColumnWidth(indexsource, 12 * 150);
  1905. break;
  1906. case 1:
  1907. cell.SetCellValue("接\n通\n量\n");
  1908. cell.CellStyle = cellStylebt9;
  1909. sheet.AutoSizeColumn(indexsource);
  1910. // sheet.SetColumnWidth(indexsource, 12 * 150);
  1911. break;
  1912. case 2:
  1913. cell.SetCellValue("接\n通\n率\n");
  1914. cell.CellStyle = cellStylebt2;
  1915. sheet.AutoSizeColumn(indexsource);
  1916. // sheet.SetColumnWidth(7, 12 * 150);
  1917. break;
  1918. case 3:
  1919. cell.SetCellValue("未\n接\n通\n");
  1920. cell.CellStyle = cellStylebt4;
  1921. sheet.AutoSizeColumn(indexsource);
  1922. // sheet.SetColumnWidth(indexsource, 12 * 150);
  1923. break;
  1924. case 4:
  1925. cell.SetCellValue("主\n动\n放\n弃\n");
  1926. cell.CellStyle = cellStylebt4;
  1927. sheet.AutoSizeColumn(indexsource);
  1928. // sheet.SetColumnWidth(indexsource, 12 * 150);
  1929. break;
  1930. }
  1931. }
  1932. indexsource += 1;
  1933. sheet.SetColumnWidth(indexsource, 12 * 35);
  1934. }
  1935. else if (MergerHotSource[0].MergerHot[i - 1].Source == "归并热线小计")
  1936. {
  1937. for (int j = 0; j < 6; j++)
  1938. {
  1939. indexsource += 1;
  1940. ICell cell = irow3.CreateCell(indexsource);
  1941. switch (j)
  1942. {
  1943. case 0:
  1944. cell.SetCellValue("来\n电\n量\n");
  1945. cell.CellStyle = cellStylebt5;
  1946. sheet.AutoSizeColumn(indexsource);
  1947. //sheet.SetColumnWidth(indexsource, 12 * 150);
  1948. break;
  1949. case 1:
  1950. cell.SetCellValue("接\n通\n量\n");
  1951. cell.CellStyle = cellStylebt9;
  1952. sheet.AutoSizeColumn(indexsource);
  1953. // sheet.SetColumnWidth(indexsource, 12 * 150);
  1954. break;
  1955. case 2:
  1956. cell.SetCellValue("未\n接\n通\n");
  1957. cell.CellStyle = cellStylebt4;
  1958. sheet.AutoSizeColumn(indexsource);
  1959. // sheet.SetColumnWidth(indexsource, 12 * 150);
  1960. break;
  1961. case 3:
  1962. cell.SetCellValue("主\n动\n放\n弃\n");
  1963. cell.CellStyle = cellStylebt4;
  1964. sheet.AutoSizeColumn(indexsource);
  1965. // sheet.SetColumnWidth(indexsource, 12 * 150);
  1966. break;
  1967. case 4:
  1968. cell.SetCellValue("接\n通\n率\n");
  1969. cell.CellStyle = cellStylebt10;
  1970. sheet.AutoSizeColumn(indexsource);
  1971. // sheet.SetColumnWidth(indexsource, 12 * 200);
  1972. break;
  1973. case 5:
  1974. cell.SetCellValue("回\n拨\n量\n");
  1975. cell.CellStyle = cellStylebt4;
  1976. sheet.AutoSizeColumn(indexsource);
  1977. // sheet.SetColumnWidth(indexsource, 12 * 150);
  1978. break;
  1979. }
  1980. }
  1981. indexsource += 1;
  1982. sheet.SetColumnWidth(indexsource, 12 * 35);
  1983. }
  1984. else
  1985. {
  1986. for (int j = 0; j < 5; j++)
  1987. {
  1988. indexsource += 1;
  1989. ICell cell = irow3.CreateCell(indexsource);
  1990. switch (j)
  1991. {
  1992. case 0:
  1993. cell.SetCellValue("来\n电\n量\n");
  1994. cell.CellStyle = cellStylebt5;
  1995. sheet.AutoSizeColumn(indexsource);
  1996. // sheet.SetColumnWidth(indexsource, 12 * 150);
  1997. break;
  1998. case 1:
  1999. cell.SetCellValue("接\n通\n量\n");
  2000. cell.CellStyle = cellStylebt9;
  2001. sheet.AutoSizeColumn(indexsource);
  2002. // sheet.SetColumnWidth(indexsource, 12 * 150);
  2003. break;
  2004. case 2:
  2005. cell.SetCellValue("未\n接\n通\n");
  2006. cell.CellStyle = cellStylebt4;
  2007. sheet.AutoSizeColumn(indexsource);
  2008. // sheet.SetColumnWidth(indexsource, 12 * 150);
  2009. break;
  2010. case 3:
  2011. cell.SetCellValue("主\n动\n放\n弃\n");
  2012. cell.CellStyle = cellStylebt4;
  2013. sheet.AutoSizeColumn(indexsource);
  2014. // sheet.SetColumnWidth(indexsource, 12 * 150);
  2015. break;
  2016. case 4:
  2017. cell.SetCellValue("接\n通\n率\n");
  2018. cell.CellStyle = cellStylebt10;
  2019. sheet.AutoSizeColumn(indexsource);
  2020. // sheet.SetColumnWidth(indexsource, 12 * 200);
  2021. break;
  2022. }
  2023. }
  2024. indexsource += 1;
  2025. sheet.SetColumnWidth(indexsource, 12 * 35);
  2026. }
  2027. }
  2028. }
  2029. for (int i=0;i< MergerHotSource.Count; i++)
  2030. {
  2031. IRow irow4 = sheet.CreateRow(5+i );
  2032. for (int j = 0; j <= MergerHotSource[i].MergerHot.Count; j++)
  2033. {
  2034. if (j == 0)
  2035. {
  2036. ICell cell = irow4.CreateCell(0);
  2037. cell.SetCellValue(MergerHotSource[i].Data);
  2038. cell.CellStyle = cellStylebt11;
  2039. indexsource = 0;
  2040. }
  2041. else
  2042. {
  2043. if (i == MergerHotSource.Count-1)
  2044. {
  2045. if (j == 1)
  2046. {
  2047. for (int z = 0; z < 5; z++)
  2048. {
  2049. indexsource += 1;
  2050. ICell cell = irow4.CreateCell(indexsource);
  2051. switch (z)
  2052. {
  2053. case 0:
  2054. cell.SetCellValue(MergerHotSource[i]
  2055. .MergerHot[j - 1].Call);
  2056. cell.CellStyle = cellStylebt8;
  2057. break;
  2058. case 1:
  2059. cell.SetCellValue(MergerHotSource[i]
  2060. .MergerHot[j - 1].Connect);
  2061. cell.CellStyle = cellStylebt8;
  2062. break;
  2063. case 2:
  2064. cell.SetCellValue(MergerHotSource[i]
  2065. .MergerHot[j - 1].Notconnected);
  2066. cell.CellStyle = cellStylebt8;
  2067. break;
  2068. case 3:
  2069. cell.SetCellValue(MergerHotSource[i]
  2070. .MergerHot[j - 1].voluntarily);
  2071. cell.CellStyle = cellStylebt8;
  2072. break;
  2073. case 4:
  2074. cell.SetCellValue(MergerHotSource[i]
  2075. .MergerHot[j - 1].rate );
  2076. cell.CellStyle = cellStylebt8;
  2077. break;
  2078. }
  2079. }
  2080. indexsource += 1;
  2081. }
  2082. else
  2083. {
  2084. if (MergerHotSource[i].MergerHot[j - 1].Source == "12345")
  2085. {
  2086. for (int z = 0; z < 5; z++)
  2087. {
  2088. indexsource += 1;
  2089. ICell cell = irow4.CreateCell(indexsource);
  2090. switch (z)
  2091. {
  2092. case 0:
  2093. cell.SetCellValue(MergerHotSource[i]
  2094. .MergerHot[j - 1].Call);
  2095. cell.CellStyle = cellStylebt8;
  2096. break;
  2097. case 1:
  2098. cell.SetCellValue(MergerHotSource[i]
  2099. .MergerHot[j - 1].Connect);
  2100. cell.CellStyle = cellStylebt8;
  2101. break;
  2102. case 2:
  2103. cell.SetCellValue(MergerHotSource[i]
  2104. .MergerHot[j - 1].rate);
  2105. cell.CellStyle = cellStylebt8;
  2106. break;
  2107. case 3:
  2108. cell.SetCellValue(MergerHotSource[i]
  2109. .MergerHot[j - 1].Notconnected);
  2110. cell.CellStyle = cellStylebt8;
  2111. break;
  2112. case 4:
  2113. cell.SetCellValue(MergerHotSource[i]
  2114. .MergerHot[j - 1].voluntarily);
  2115. cell.CellStyle = cellStylebt8;
  2116. break;
  2117. }
  2118. }
  2119. indexsource += 1;
  2120. }
  2121. else if (MergerHotSource[i].MergerHot[j - 1].Source == "当日小计"
  2122. || MergerHotSource[i].MergerHot[j - 1].Source == "累计")
  2123. {
  2124. for (int z = 0; z < 5; z++)
  2125. {
  2126. indexsource += 1;
  2127. ICell cell = irow4.CreateCell(indexsource);
  2128. switch (z)
  2129. {
  2130. case 0:
  2131. cell.SetCellValue(MergerHotSource[i]
  2132. .MergerHot[j - 1].Call);
  2133. cell.CellStyle = cellStylebt8;
  2134. break;
  2135. case 1:
  2136. cell.SetCellValue(MergerHotSource[i]
  2137. .MergerHot[j - 1].Connect);
  2138. cell.CellStyle = cellStylebt8;
  2139. break;
  2140. case 2:
  2141. cell.SetCellValue(MergerHotSource[i]
  2142. .MergerHot[j - 1].Notconnected);
  2143. cell.CellStyle = cellStylebt8;
  2144. break;
  2145. case 3:
  2146. cell.SetCellValue(MergerHotSource[i]
  2147. .MergerHot[j - 1].voluntarily);
  2148. cell.CellStyle = cellStylebt8;
  2149. break;
  2150. case 4:
  2151. cell.SetCellValue(MergerHotSource[i]
  2152. .MergerHot[j - 1].rate);
  2153. cell.CellStyle = cellStylebt8;
  2154. break;
  2155. }
  2156. }
  2157. indexsource += 1;
  2158. }
  2159. else if (MergerHotSource[i].MergerHot[j - 1].Source == "归并热线小计"
  2160. )
  2161. {
  2162. for (int z = 0; z < 6; z++)
  2163. {
  2164. indexsource += 1;
  2165. ICell cell = irow4.CreateCell(indexsource);
  2166. switch (z)
  2167. {
  2168. case 0:
  2169. cell.SetCellValue(MergerHotSource[i]
  2170. .MergerHot[j - 1].Call);
  2171. cell.CellStyle = cellStylebt8;
  2172. break;
  2173. case 1:
  2174. cell.SetCellValue(MergerHotSource[i]
  2175. .MergerHot[j - 1].Connect);
  2176. cell.CellStyle = cellStylebt8;
  2177. break;
  2178. case 2:
  2179. cell.SetCellValue(MergerHotSource[i]
  2180. .MergerHot[j - 1].Notconnected);
  2181. cell.CellStyle = cellStylebt8;
  2182. break;
  2183. case 3:
  2184. cell.SetCellValue(MergerHotSource[i]
  2185. .MergerHot[j - 1].voluntarily);
  2186. cell.CellStyle = cellStylebt8;
  2187. break;
  2188. case 4:
  2189. cell.SetCellValue(MergerHotSource[i]
  2190. .MergerHot[j - 1].rate);
  2191. cell.CellStyle = cellStylebt8;
  2192. break;
  2193. case 5:
  2194. cell.SetCellValue(MergerHotSource[i]
  2195. .MergerHot[j - 1].callback );
  2196. cell.CellStyle = cellStylebt8;
  2197. break;
  2198. }
  2199. }
  2200. indexsource += 1;
  2201. }
  2202. else
  2203. {
  2204. for (int z = 0; z < 5; z++)
  2205. {
  2206. indexsource += 1;
  2207. ICell cell = irow4.CreateCell(indexsource);
  2208. switch (z)
  2209. {
  2210. case 0:
  2211. cell.SetCellValue(MergerHotSource[i]
  2212. .MergerHot[j - 1].Call);
  2213. cell.CellStyle = cellStylebt8;
  2214. break;
  2215. case 1:
  2216. cell.SetCellValue(MergerHotSource[i]
  2217. .MergerHot[j - 1].Connect);
  2218. cell.CellStyle = cellStylebt8;
  2219. break;
  2220. case 2:
  2221. cell.SetCellValue(MergerHotSource[i]
  2222. .MergerHot[j - 1].Notconnected);
  2223. cell.CellStyle = cellStylebt8;
  2224. break;
  2225. case 3:
  2226. cell.SetCellValue(MergerHotSource[i]
  2227. .MergerHot[j - 1].voluntarily);
  2228. cell.CellStyle = cellStylebt8;
  2229. break;
  2230. case 4:
  2231. cell.SetCellValue(MergerHotSource[i]
  2232. .MergerHot[j - 1].rate );
  2233. cell.CellStyle = cellStylebt8;
  2234. break;
  2235. }
  2236. }
  2237. indexsource += 1;
  2238. }
  2239. }
  2240. }
  2241. else
  2242. {
  2243. if (j == 1)
  2244. {
  2245. for (int z = 0; z < 5; z++)
  2246. {
  2247. indexsource += 1;
  2248. ICell cell = irow4.CreateCell(indexsource);
  2249. switch (z)
  2250. {
  2251. case 0:
  2252. cell.SetCellValue(MergerHotSource[i]
  2253. .MergerHot[j - 1].Call);
  2254. cell.CellStyle = cellStylebt15;
  2255. break;
  2256. case 1:
  2257. cell.SetCellValue(MergerHotSource[i]
  2258. .MergerHot[j - 1].Connect);
  2259. cell.CellStyle = cellStylebt14;
  2260. break;
  2261. case 2:
  2262. cell.SetCellValue(MergerHotSource[i]
  2263. .MergerHot[j - 1].Notconnected);
  2264. cell.CellStyle = cellStylebt11;
  2265. break;
  2266. case 3:
  2267. cell.SetCellValue(MergerHotSource[i]
  2268. .MergerHot[j - 1].voluntarily);
  2269. cell.CellStyle = cellStylebt11;
  2270. break;
  2271. case 4:
  2272. cell.SetCellValue(MergerHotSource[i]
  2273. .MergerHot[j - 1].rate );
  2274. cell.CellStyle = cellStylebt11;
  2275. break;
  2276. }
  2277. }
  2278. indexsource += 1;
  2279. }
  2280. else
  2281. {
  2282. if (MergerHotSource[i].MergerHot[j - 1].Source == "12345")
  2283. {
  2284. for (int z = 0; z < 5; z++)
  2285. {
  2286. indexsource += 1;
  2287. ICell cell = irow4.CreateCell(indexsource);
  2288. switch (z)
  2289. {
  2290. case 0:
  2291. cell.SetCellValue(MergerHotSource[i]
  2292. .MergerHot[j - 1].Call);
  2293. cell.CellStyle = cellStylebt15;
  2294. break;
  2295. case 1:
  2296. cell.SetCellValue(MergerHotSource[i]
  2297. .MergerHot[j - 1].Connect);
  2298. cell.CellStyle = cellStylebt14;
  2299. break;
  2300. case 2:
  2301. cell.SetCellValue(MergerHotSource[i]
  2302. .MergerHot[j - 1].rate);
  2303. cell.CellStyle = cellStylebt6;
  2304. break;
  2305. case 3:
  2306. cell.SetCellValue(MergerHotSource[i]
  2307. .MergerHot[j - 1].Notconnected);
  2308. cell.CellStyle = cellStylebt11;
  2309. break;
  2310. case 4:
  2311. cell.SetCellValue(MergerHotSource[i]
  2312. .MergerHot[j - 1].voluntarily);
  2313. cell.CellStyle = cellStylebt11;
  2314. break;
  2315. }
  2316. }
  2317. indexsource += 1;
  2318. }
  2319. else if (MergerHotSource[i].MergerHot[j - 1].Source == "当日小计"
  2320. || MergerHotSource[i].MergerHot[j - 1].Source == "累计")
  2321. {
  2322. for (int z = 0; z < 5; z++)
  2323. {
  2324. indexsource += 1;
  2325. ICell cell = irow4.CreateCell(indexsource);
  2326. switch (z)
  2327. {
  2328. case 0:
  2329. cell.SetCellValue(MergerHotSource[i]
  2330. .MergerHot[j - 1].Call);
  2331. cell.CellStyle = cellStylebt15;
  2332. break;
  2333. case 1:
  2334. cell.SetCellValue(MergerHotSource[i]
  2335. .MergerHot[j - 1].Connect);
  2336. cell.CellStyle = cellStylebt14;
  2337. break;
  2338. case 2:
  2339. cell.SetCellValue(MergerHotSource[i]
  2340. .MergerHot[j - 1].Notconnected);
  2341. cell.CellStyle = cellStylebt11;
  2342. break;
  2343. case 3:
  2344. cell.SetCellValue(MergerHotSource[i]
  2345. .MergerHot[j - 1].voluntarily);
  2346. cell.CellStyle = cellStylebt11;
  2347. break;
  2348. case 4:
  2349. cell.SetCellValue(MergerHotSource[i]
  2350. .MergerHot[j - 1].rate);
  2351. cell.CellStyle = cellStylebt12;
  2352. break;
  2353. }
  2354. }
  2355. indexsource += 1;
  2356. }
  2357. else if (MergerHotSource[i].MergerHot[j - 1].Source == "归并热线小计")
  2358. {
  2359. for (int z = 0; z < 6; z++)
  2360. {
  2361. indexsource += 1;
  2362. ICell cell = irow4.CreateCell(indexsource);
  2363. switch (z)
  2364. {
  2365. case 0:
  2366. cell.SetCellValue(MergerHotSource[i]
  2367. .MergerHot[j - 1].Call);
  2368. cell.CellStyle = cellStylebt15;
  2369. break;
  2370. case 1:
  2371. cell.SetCellValue(MergerHotSource[i]
  2372. .MergerHot[j - 1].Connect);
  2373. cell.CellStyle = cellStylebt14;
  2374. break;
  2375. case 2:
  2376. cell.SetCellValue(MergerHotSource[i]
  2377. .MergerHot[j - 1].Notconnected);
  2378. cell.CellStyle = cellStylebt11;
  2379. break;
  2380. case 3:
  2381. cell.SetCellValue(MergerHotSource[i]
  2382. .MergerHot[j - 1].voluntarily);
  2383. cell.CellStyle = cellStylebt11;
  2384. break;
  2385. case 4:
  2386. cell.SetCellValue(MergerHotSource[i]
  2387. .MergerHot[j - 1].rate);
  2388. cell.CellStyle = cellStylebt12;
  2389. break;
  2390. case 5:
  2391. cell.SetCellValue(MergerHotSource[i]
  2392. .MergerHot[j - 1].callback );
  2393. cell.CellStyle = cellStylebt11;
  2394. break;
  2395. }
  2396. }
  2397. indexsource += 1;
  2398. }
  2399. else
  2400. {
  2401. for (int z = 0; z < 5; z++)
  2402. {
  2403. indexsource += 1;
  2404. ICell cell = irow4.CreateCell(indexsource);
  2405. switch (z)
  2406. {
  2407. case 0:
  2408. cell.SetCellValue(MergerHotSource[i]
  2409. .MergerHot[j - 1].Call);
  2410. cell.CellStyle = cellStylebt15;
  2411. break;
  2412. case 1:
  2413. cell.SetCellValue(MergerHotSource[i]
  2414. .MergerHot[j - 1].Connect);
  2415. cell.CellStyle = cellStylebt14;
  2416. break;
  2417. case 2:
  2418. cell.SetCellValue(MergerHotSource[i]
  2419. .MergerHot[j - 1].Notconnected);
  2420. cell.CellStyle = cellStylebt11;
  2421. break;
  2422. case 3:
  2423. cell.SetCellValue(MergerHotSource[i]
  2424. .MergerHot[j - 1].voluntarily);
  2425. cell.CellStyle = cellStylebt11;
  2426. break;
  2427. case 4:
  2428. cell.SetCellValue(MergerHotSource[i]
  2429. .MergerHot[j - 1].rate );
  2430. cell.CellStyle = cellStylebt11;
  2431. break;
  2432. }
  2433. }
  2434. indexsource += 1;
  2435. }
  2436. }
  2437. }
  2438. }
  2439. }
  2440. }
  2441. //自适应列宽度
  2442. //for (int i = 0; i < 20; i++)
  2443. //{
  2444. // sheet.AutoSizeColumn(i);
  2445. // // sheet.SetColumnWidth(i, 20 * 256);
  2446. //}
  2447. using (MemoryStream ms = new MemoryStream())
  2448. {
  2449. workbook.Write(ms);
  2450. HttpContext curContext = HttpContext.Current;
  2451. // 设置编码和附件格式
  2452. curContext.Response.ContentType = "application/vnd.ms-excel";
  2453. curContext.Response.ContentEncoding = Encoding.UTF8;
  2454. curContext.Response.Charset = "";
  2455. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  2456. curContext.Response.AppendHeader("Content-Disposition",
  2457. "attachment;filename=" + HttpUtility.UrlEncode("归并数据统计" + ".xls", Encoding.UTF8));
  2458. curContext.Response.BinaryWrite(ms.GetBuffer());
  2459. workbook = null;
  2460. ms.Close();
  2461. ms.Dispose();
  2462. curContext.Response.End();
  2463. }
  2464. return "";
  2465. }
  2466. catch
  2467. {
  2468. return "导出失败!";
  2469. }
  2470. }
  2471. /// <summary>
  2472. /// 督办数据报表导出
  2473. /// </summary>
  2474. /// <param name="ds"></param>
  2475. /// <returns></returns>
  2476. public string CenterReception(DataSet dt)
  2477. {
  2478. try
  2479. {
  2480. int lenth = 0;
  2481. HSSFWorkbook workbook = new HSSFWorkbook();
  2482. ISheet sheet = workbook.CreateSheet("电话中心受话情况统计表");
  2483. ICellStyle cellStyle = workbook.CreateCellStyle();
  2484. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  2485. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  2486. cellStyle.SetFont(cellfont);
  2487. ICellStyle cellStylebt = workbook.CreateCellStyle();
  2488. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  2489. cellfontbt.Boldweight = (short)FontBoldWeight.Bold;
  2490. cellStylebt.SetFont(cellfontbt);
  2491. cellStylebt.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black .Index ;
  2492. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  2493. cellStylebt.Alignment = HorizontalAlignment.Center;
  2494. cellStylebt.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2495. cellStylebt.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2496. cellStylebt.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2497. cellStylebt.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2498. ICellStyle cellStylebt1 = workbook.CreateCellStyle();
  2499. NPOI.SS.UserModel.IFont cellfontbt1 = workbook.CreateFont();
  2500. cellfontbt1.Boldweight = (short)FontBoldWeight.Bold;
  2501. cellfontbt1.FontHeightInPoints = 22;
  2502. cellStylebt1.SetFont(cellfontbt1);
  2503. cellStylebt1.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
  2504. cellStylebt1.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2505. cellStylebt1.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2506. cellStylebt1.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2507. cellStylebt1.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2508. cellStylebt1.VerticalAlignment = VerticalAlignment.Center;
  2509. cellStylebt1.Alignment = HorizontalAlignment.Center;
  2510. ICellStyle cellStylebt2 = workbook.CreateCellStyle();
  2511. NPOI.SS.UserModel.IFont cellfontbt2 = workbook.CreateFont();
  2512. cellfontbt2.Boldweight = (short)FontBoldWeight.Bold;
  2513. cellStylebt2.WrapText = true;//设置换行这个要先设置
  2514. cellfontbt2.FontHeightInPoints = 14;
  2515. cellStylebt2.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
  2516. cellStylebt2.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2517. cellStylebt2.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2518. cellStylebt2.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2519. cellStylebt2.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2520. cellStylebt2.SetFont(cellfontbt2);
  2521. cellStylebt2.VerticalAlignment = VerticalAlignment.Center;
  2522. cellStylebt2.Alignment = HorizontalAlignment.Center;
  2523. HSSFPalette palette = workbook.GetCustomPalette(); //调色板实例
  2524. palette.SetColorAtIndex(8, 218, 232, 244);
  2525. NPOI.HSSF.Util.HSSFColor hssFColor = palette.FindColor(218, 232, 244);
  2526. cellStylebt2.FillForegroundColor = hssFColor.Indexed;
  2527. cellStylebt2.FillPattern = FillPattern.SolidForeground;
  2528. cellStylebt2.FillBackgroundColor = hssFColor.Indexed; ;
  2529. ICellStyle cellStylebt3 = workbook.CreateCellStyle();
  2530. NPOI.SS.UserModel.IFont cellfontbt3 = workbook.CreateFont();
  2531. cellfontbt3.Boldweight = (short)FontBoldWeight.Bold;
  2532. cellStylebt3.WrapText = true;//设置换行这个要先设置
  2533. cellfontbt3.FontHeightInPoints = 11;
  2534. cellStylebt3.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
  2535. cellStylebt3.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2536. cellStylebt3.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2537. cellStylebt3.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2538. cellStylebt3.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2539. cellStylebt3.SetFont(cellfontbt3);
  2540. cellStylebt3.VerticalAlignment = VerticalAlignment.Center;
  2541. cellStylebt3.Alignment = HorizontalAlignment.Center;
  2542. ICellStyle cellStylebt4 = workbook.CreateCellStyle();
  2543. NPOI.SS.UserModel.IFont cellfontbt4 = workbook.CreateFont();
  2544. cellfontbt4.Boldweight = (short)FontBoldWeight.Bold;
  2545. cellStylebt4.WrapText = true;//设置换行这个要先设置
  2546. cellfontbt4.FontHeightInPoints = 11;
  2547. cellStylebt4.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
  2548. cellStylebt4.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2549. cellStylebt4.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2550. cellStylebt4.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2551. cellStylebt4.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2552. cellStylebt4.SetFont(cellfontbt4);
  2553. cellStylebt4.VerticalAlignment = VerticalAlignment.Center;
  2554. cellStylebt4.Alignment = HorizontalAlignment.Center;
  2555. HSSFPalette palette1 = workbook.GetCustomPalette(); //调色板实例
  2556. palette1.SetColorAtIndex(9, 166, 197, 191);
  2557. NPOI.HSSF.Util.HSSFColor hssFColor1 = palette1.FindColor(166, 197, 191);
  2558. cellStylebt4.FillForegroundColor = hssFColor1.Indexed;
  2559. cellStylebt4.FillPattern = FillPattern.SolidForeground;
  2560. cellStylebt4.FillBackgroundColor = hssFColor1.Indexed; ;
  2561. ICellStyle cellStylebt5 = workbook.CreateCellStyle();
  2562. cellStylebt5.WrapText = true;//设置换行这个要先设置
  2563. cellStylebt5.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
  2564. cellStylebt5.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2565. cellStylebt5.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2566. cellStylebt5.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2567. cellStylebt5.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2568. cellStylebt5.SetFont(cellfontbt4);
  2569. cellStylebt5.VerticalAlignment = VerticalAlignment.Center;
  2570. cellStylebt5.Alignment = HorizontalAlignment.Center;
  2571. HSSFPalette palette2 = workbook.GetCustomPalette(); //调色板实例
  2572. palette2.SetColorAtIndex(10, 216, 243, 198);
  2573. NPOI.HSSF.Util.HSSFColor hssFColor2 = palette2.FindColor(216, 243, 198);
  2574. cellStylebt5.FillForegroundColor = hssFColor2.Indexed;
  2575. cellStylebt5.FillPattern = FillPattern.SolidForeground;
  2576. cellStylebt5.FillBackgroundColor = hssFColor2.Indexed; ;
  2577. ICellStyle cellStylebt6 = workbook.CreateCellStyle();
  2578. cellStylebt6.WrapText = true;//设置换行这个要先设置
  2579. cellStylebt6.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
  2580. cellStylebt6.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2581. cellStylebt6.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2582. cellStylebt6.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2583. cellStylebt6.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2584. cellStylebt6.SetFont(cellfontbt4);
  2585. cellStylebt6.VerticalAlignment = VerticalAlignment.Center;
  2586. cellStylebt6.Alignment = HorizontalAlignment.Center;
  2587. HSSFPalette palette3 = workbook.GetCustomPalette(); //调色板实例
  2588. palette3.SetColorAtIndex(11, 253, 220, 212);
  2589. NPOI.HSSF.Util.HSSFColor hssFColor3 = palette3.FindColor(253, 220, 212);
  2590. cellStylebt6.FillForegroundColor = hssFColor3.Indexed;
  2591. cellStylebt6.FillPattern = FillPattern.SolidForeground;
  2592. cellStylebt6.FillBackgroundColor = hssFColor3.Indexed; ; ;
  2593. ICellStyle cellStylebt7 = workbook.CreateCellStyle();
  2594. cellStylebt7.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
  2595. cellStylebt7.WrapText = true;//设置换行这个要先设置
  2596. cellStylebt7.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2597. cellStylebt7.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2598. cellStylebt7.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2599. cellStylebt7.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2600. cellStylebt7.SetFont(cellfontbt4);
  2601. cellStylebt7.VerticalAlignment = VerticalAlignment.Center;
  2602. cellStylebt7.Alignment = HorizontalAlignment.Center;
  2603. HSSFPalette palette4 = workbook.GetCustomPalette(); //调色板实例
  2604. palette4.SetColorAtIndex(12, 248, 231, 187);
  2605. NPOI.HSSF.Util.HSSFColor hssFColor4 = palette4.FindColor(248, 231, 187);
  2606. cellStylebt7.FillForegroundColor = hssFColor4.Indexed;
  2607. cellStylebt7.FillPattern = FillPattern.SolidForeground;
  2608. cellStylebt7.FillBackgroundColor = hssFColor4.Indexed; ; ;
  2609. IRow irow1 = sheet.CreateRow(0);
  2610. ICell cell1 = irow1.CreateCell(0);
  2611. cell1.SetCellValue("电话中心受话情况统计表");
  2612. cell1.CellStyle = cellStylebt1;
  2613. sheet.AddMergedRegion(new CellRangeAddress(0, 2, 0, 9));
  2614. IRow irow2 = sheet.CreateRow(3);
  2615. string[] cols = { "序号", "班组", "坐席人员", "通话量"
  2616. , "呼入接通量", "呼出接通量", "呼出未接通量", "来电总量"
  2617. , "未接通量" , "待回拨量"};
  2618. for (int i = 0; i < cols.Length
  2619. ; i++)
  2620. {
  2621. ICell cell = irow2.CreateCell(i);
  2622. cell.SetCellValue(cols[i]);
  2623. cell.CellStyle = cellStylebt2;
  2624. }
  2625. IRow irow3 = sheet.CreateRow(4);
  2626. for (int i=0;i< dt.Tables [0].Rows.Count;i ++)
  2627. {
  2628. if (i ==0)
  2629. {
  2630. for (int j =0;j <7;j++)
  2631. {
  2632. ICell cell = irow3.CreateCell(j);
  2633. switch (j )
  2634. {
  2635. case 0:
  2636. cell.SetCellValue(i +1);
  2637. cell.CellStyle = cellStylebt3;
  2638. break;
  2639. case 1:
  2640. cell.SetCellValue(dt.Tables[0].Rows[i ]["groupname"].ToString ());
  2641. cell.CellStyle = cellStylebt3;
  2642. break;
  2643. case 2:
  2644. cell.SetCellValue(dt.Tables[0].Rows[i]["username"].ToString());
  2645. cell.CellStyle = cellStylebt3;
  2646. break;
  2647. case 3:
  2648. cell.SetCellValue(dt.Tables[0].Rows[i]["telcount"].ToString());
  2649. cell.CellStyle = cellStylebt4;
  2650. break;
  2651. case 4:
  2652. cell.SetCellValue(dt.Tables[0].Rows[i]["hrcount"].ToString());
  2653. cell.CellStyle = cellStylebt5;
  2654. break;
  2655. case 5:
  2656. cell.SetCellValue(dt.Tables[0].Rows[i]["hccount"].ToString());
  2657. cell.CellStyle = cellStylebt5;
  2658. break;
  2659. case 6:
  2660. cell.SetCellValue(dt.Tables[0].Rows[i]["hcwjtcount"].ToString());
  2661. cell.CellStyle = cellStylebt3;
  2662. break;
  2663. }
  2664. }
  2665. }
  2666. else
  2667. {
  2668. IRow irow4 = sheet.CreateRow(4+i );
  2669. for (int j = 0; j < 7; j++)
  2670. {
  2671. ICell cell = irow4.CreateCell(j );
  2672. switch (j)
  2673. {
  2674. case 0:
  2675. cell.SetCellValue(i + 1);
  2676. cell.CellStyle = cellStylebt3;
  2677. break;
  2678. case 1:
  2679. cell.SetCellValue(dt.Tables[0].Rows[i]["groupname"].ToString());
  2680. cell.CellStyle = cellStylebt3;
  2681. break;
  2682. case 2:
  2683. cell.SetCellValue(dt.Tables[0].Rows[i]["username"].ToString());
  2684. cell.CellStyle = cellStylebt3;
  2685. break;
  2686. case 3:
  2687. cell.SetCellValue(dt.Tables[0].Rows[i]["telcount"].ToString());
  2688. cell.CellStyle = cellStylebt4;
  2689. break;
  2690. case 4:
  2691. cell.SetCellValue(dt.Tables[0].Rows[i]["hrcount"].ToString());
  2692. cell.CellStyle = cellStylebt5;
  2693. break;
  2694. case 5:
  2695. cell.SetCellValue(dt.Tables[0].Rows[i]["hccount"].ToString());
  2696. cell.CellStyle = cellStylebt5;
  2697. break;
  2698. case 6:
  2699. cell.SetCellValue(dt.Tables[0].Rows[i]["hcwjtcount"].ToString());
  2700. cell.CellStyle = cellStylebt3;
  2701. break;
  2702. }
  2703. }
  2704. }
  2705. }
  2706. ICell cell2 = irow3.CreateCell(7);
  2707. cell2.SetCellValue(dt.Tables[1].Rows[0]["telcount"].ToString());
  2708. cell2.CellStyle = cellStylebt6;
  2709. sheet.AddMergedRegion(new CellRangeAddress(4, dt.Tables[0].Rows.Count+3, 7, 7));
  2710. ICell cell3 = irow3.CreateCell(8);
  2711. cell3.SetCellValue(dt.Tables[2].Rows[0]["wjtcount"].ToString());
  2712. cell3.CellStyle = cellStylebt7;
  2713. sheet.AddMergedRegion(new CellRangeAddress(4, dt.Tables[0].Rows.Count+3, 8, 8));
  2714. ICell cell4 = irow3.CreateCell(9);
  2715. cell4.SetCellValue(dt.Tables[3].Rows[0]["dhbcount"].ToString());
  2716. cell4.CellStyle = cellStylebt3;
  2717. sheet.AddMergedRegion(new CellRangeAddress(4, dt.Tables[0].Rows.Count+3, 9, 9));
  2718. for (int i = 0; i < 10; i++)
  2719. {
  2720. sheet.AutoSizeColumn(i);
  2721. }
  2722. using (MemoryStream ms = new MemoryStream())
  2723. {
  2724. workbook.Write(ms);
  2725. HttpContext curContext = HttpContext.Current;
  2726. // 设置编码和附件格式
  2727. curContext.Response.ContentType = "application/vnd.ms-excel";
  2728. curContext.Response.ContentEncoding = Encoding.UTF8;
  2729. curContext.Response.Charset = "";
  2730. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  2731. curContext.Response.AppendHeader("Content-Disposition",
  2732. "attachment;filename=" + HttpUtility.UrlEncode("电话中心受话情况统计表" + ".xls", Encoding.UTF8));
  2733. curContext.Response.BinaryWrite(ms.GetBuffer());
  2734. workbook = null;
  2735. ms.Close();
  2736. ms.Dispose();
  2737. curContext.Response.End();
  2738. }
  2739. return "";
  2740. }
  2741. catch(Exception e)
  2742. {
  2743. return e.Message;
  2744. }
  2745. }
  2746. /// <summary>
  2747. /// 居委办件统计
  2748. /// </summary>
  2749. /// <param name="ds"></param>
  2750. /// <returns></returns>
  2751. public string BureauHandling(DataSet dt)
  2752. {
  2753. try
  2754. {
  2755. int lenth = 0;
  2756. HSSFWorkbook workbook = new HSSFWorkbook();
  2757. ISheet sheet = workbook.CreateSheet("局委办件统计");
  2758. ICellStyle cellStyle = workbook.CreateCellStyle();
  2759. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  2760. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  2761. cellStyle.SetFont(cellfont);
  2762. ICellStyle cellStylebt1 = workbook.CreateCellStyle();
  2763. NPOI.SS.UserModel.IFont cellfontbt1 = workbook.CreateFont();
  2764. cellfontbt1.Boldweight = (short)FontBoldWeight.Bold;
  2765. cellfontbt1.FontHeightInPoints = 20;
  2766. cellStylebt1.SetFont(cellfontbt1);
  2767. cellStylebt1.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2768. cellStylebt1.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2769. cellStylebt1.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2770. cellStylebt1.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2771. cellStylebt1.VerticalAlignment = VerticalAlignment.Center;
  2772. cellStylebt1.Alignment = HorizontalAlignment.Center;
  2773. ICellStyle cellStylebt2 = workbook.CreateCellStyle();
  2774. NPOI.SS.UserModel.IFont cellfontbt2 = workbook.CreateFont();
  2775. cellfontbt2.Boldweight = (short)FontBoldWeight.Bold;
  2776. cellStylebt2.WrapText = true;//设置换行这个要先设置
  2777. cellfontbt2.FontHeightInPoints = 16;
  2778. cellStylebt2.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
  2779. cellStylebt2.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2780. cellStylebt2.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2781. cellStylebt2.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2782. cellStylebt2.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2783. cellStylebt2.SetFont(cellfontbt2);
  2784. cellStylebt2.VerticalAlignment = VerticalAlignment.Center;
  2785. cellStylebt2.Alignment = HorizontalAlignment.Center;
  2786. ICellStyle cellStylebt3 = workbook.CreateCellStyle();
  2787. NPOI.SS.UserModel.IFont cellfontbt3 = workbook.CreateFont();
  2788. cellfontbt3.Boldweight = (short)FontBoldWeight.Bold;
  2789. cellStylebt3.WrapText = true;//设置换行这个要先设置
  2790. cellfontbt3.FontHeightInPoints = 14;
  2791. cellStylebt3.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
  2792. cellStylebt3.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2793. cellStylebt3.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2794. cellStylebt3.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2795. cellStylebt3.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2796. cellStylebt3.SetFont(cellfontbt3);
  2797. cellStylebt3.VerticalAlignment = VerticalAlignment.Center;
  2798. cellStylebt3.Alignment = HorizontalAlignment.Center;
  2799. IRow irow1 = sheet.CreateRow(0);
  2800. ICell cell1 = irow1.CreateCell(0);
  2801. cell1.SetCellValue("局委办件统计");
  2802. cell1.CellStyle = cellStylebt1;
  2803. sheet.AddMergedRegion(new CellRangeAddress(0, 2, 0, 14));
  2804. IRow irow = sheet.CreateRow(3);
  2805. for (int i = 0; i < 15; i++)
  2806. {
  2807. ICell cell4 = irow.CreateCell(i);
  2808. cell4.CellStyle = cellStylebt2;
  2809. }
  2810. ICell cell2 = irow.CreateCell(5);
  2811. cell2.SetCellValue("未办结");
  2812. cell2.CellStyle = cellStylebt2;
  2813. sheet.AddMergedRegion(new CellRangeAddress(3, 3, 5, 6));
  2814. ICell cell3 = irow.CreateCell(7);
  2815. cell3.SetCellValue("已办结");
  2816. cell3.CellStyle = cellStylebt2;
  2817. sheet.AddMergedRegion(new CellRangeAddress(3, 3, 7, 9));
  2818. IRow irow2 = sheet.CreateRow(4);
  2819. string[] cols = { "序号", "承办单位", "承办件", "办结件"
  2820. , "办结率", "正在办理", "超期未果", "正常回复"
  2821. , "超期回复" , "已办未果", "待审核", "局委退件", "延期件", "重置转发", "驳回"};
  2822. for (int i = 0; i < cols.Length
  2823. ; i++)
  2824. {
  2825. ICell cell = irow2.CreateCell(i);
  2826. cell.SetCellValue(cols[i]);
  2827. cell.CellStyle = cellStylebt2;
  2828. }
  2829. if (dt!=null&& dt.Tables [0].Rows .Count >0)
  2830. {
  2831. for (int i=0; i< dt.Tables[0].Rows.Count;i++)
  2832. {
  2833. IRow irow3 = sheet.CreateRow(5+i);
  2834. for (int j = 0; j < cols.Length
  2835. ; j++)
  2836. {
  2837. ICell cell = irow3.CreateCell(j);
  2838. switch (j )
  2839. {
  2840. case 0:
  2841. cell.SetCellValue(i +1);
  2842. cell.CellStyle = cellStylebt3;
  2843. break;
  2844. case 1:
  2845. cell.SetCellValue(dt.Tables[0].Rows[i]["deptname"].ToString());
  2846. cell.CellStyle = cellStylebt3;
  2847. break;
  2848. case 2:
  2849. cell.SetCellValue(dt.Tables[0].Rows[i]["cbcount"].ToString());
  2850. cell.CellStyle = cellStylebt3;
  2851. break;
  2852. case 3:
  2853. cell.SetCellValue(dt.Tables[0].Rows[i]["bjcount"].ToString());
  2854. cell.CellStyle = cellStylebt3;
  2855. break;
  2856. case 4:
  2857. cell.SetCellValue(dt.Tables[0].Rows[i]["bjrate"].ToString());
  2858. cell.CellStyle = cellStylebt3;
  2859. break;
  2860. case 5:
  2861. cell.SetCellValue(dt.Tables[0].Rows[i]["blcount"].ToString());
  2862. cell.CellStyle = cellStylebt3;
  2863. break;
  2864. case 6:
  2865. cell.SetCellValue(dt.Tables[0].Rows[i]["cqwgcount"].ToString());
  2866. cell.CellStyle = cellStylebt3;
  2867. break;
  2868. case 7:
  2869. cell.SetCellValue(dt.Tables[0].Rows[i]["zchf"].ToString());
  2870. cell.CellStyle = cellStylebt3;
  2871. break;
  2872. case 8:
  2873. cell.SetCellValue(dt.Tables[0].Rows[i]["cqhf"].ToString());
  2874. cell.CellStyle = cellStylebt3;
  2875. break;
  2876. case 9:
  2877. cell.SetCellValue(dt.Tables[0].Rows[i]["ybwg"].ToString());
  2878. cell.CellStyle = cellStylebt3;
  2879. break;
  2880. case 10:
  2881. cell.SetCellValue(dt.Tables[0].Rows[i]["dsh"].ToString());
  2882. cell.CellStyle = cellStylebt3;
  2883. break;
  2884. case 11:
  2885. cell.SetCellValue(dt.Tables[0].Rows[i]["jwtj"].ToString());
  2886. cell.CellStyle = cellStylebt3;
  2887. break;
  2888. case 12:
  2889. cell.SetCellValue(dt.Tables[0].Rows[i]["yqcount"].ToString());
  2890. cell.CellStyle = cellStylebt3;
  2891. break;
  2892. case 13:
  2893. cell.SetCellValue(dt.Tables[0].Rows[i]["czzf"].ToString());
  2894. cell.CellStyle = cellStylebt3;
  2895. break;
  2896. case 14:
  2897. cell.SetCellValue(dt.Tables[0].Rows[i]["bh"].ToString());
  2898. cell.CellStyle = cellStylebt3;
  2899. break;
  2900. }
  2901. }
  2902. }
  2903. }
  2904. for (int i = 0; i < 10; i++)
  2905. {
  2906. sheet.AutoSizeColumn(i);
  2907. }
  2908. using (MemoryStream ms = new MemoryStream())
  2909. {
  2910. workbook.Write(ms);
  2911. HttpContext curContext = HttpContext.Current;
  2912. // 设置编码和附件格式
  2913. curContext.Response.ContentType = "application/vnd.ms-excel";
  2914. curContext.Response.ContentEncoding = Encoding.UTF8;
  2915. curContext.Response.Charset = "";
  2916. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  2917. curContext.Response.AppendHeader("Content-Disposition",
  2918. "attachment;filename=" + HttpUtility.UrlEncode("局委办件统计" + ".xls", Encoding.UTF8));
  2919. curContext.Response.BinaryWrite(ms.GetBuffer());
  2920. workbook = null;
  2921. ms.Close();
  2922. ms.Dispose();
  2923. curContext.Response.End();
  2924. }
  2925. return "";
  2926. }
  2927. catch (Exception e)
  2928. {
  2929. return e.Message;
  2930. }
  2931. }
  2932. /// <summary>
  2933. /// 政民互动综合统计
  2934. /// </summary>
  2935. /// <param name="ds"></param>
  2936. /// <returns></returns>
  2937. public string Government(DataSet dt)
  2938. {
  2939. try
  2940. {
  2941. int lenth = 0;
  2942. HSSFWorkbook workbook = new HSSFWorkbook();
  2943. ISheet sheet = workbook.CreateSheet("政民互动综合统计");
  2944. ICellStyle cellStyle = workbook.CreateCellStyle();
  2945. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  2946. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  2947. cellStyle.SetFont(cellfont);
  2948. ICellStyle cellStylebt1 = workbook.CreateCellStyle();
  2949. NPOI.SS.UserModel.IFont cellfontbt1 = workbook.CreateFont();
  2950. cellfontbt1.Boldweight = (short)FontBoldWeight.Bold;
  2951. cellfontbt1.FontHeightInPoints = 20;
  2952. cellStylebt1.SetFont(cellfontbt1);
  2953. cellStylebt1.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2954. cellStylebt1.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2955. cellStylebt1.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2956. cellStylebt1.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2957. cellStylebt1.VerticalAlignment = VerticalAlignment.Center;
  2958. cellStylebt1.Alignment = HorizontalAlignment.Center;
  2959. ICellStyle cellStylebt2 = workbook.CreateCellStyle();
  2960. NPOI.SS.UserModel.IFont cellfontbt2 = workbook.CreateFont();
  2961. cellfontbt2.Boldweight = (short)FontBoldWeight.Bold;
  2962. cellStylebt2.WrapText = true;//设置换行这个要先设置
  2963. cellfontbt2.FontHeightInPoints = 16;
  2964. cellStylebt2.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
  2965. cellStylebt2.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2966. cellStylebt2.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2967. cellStylebt2.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2968. cellStylebt2.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2969. cellStylebt2.SetFont(cellfontbt2);
  2970. cellStylebt2.VerticalAlignment = VerticalAlignment.Center;
  2971. cellStylebt2.Alignment = HorizontalAlignment.Center;
  2972. ICellStyle cellStylebt3 = workbook.CreateCellStyle();
  2973. NPOI.SS.UserModel.IFont cellfontbt3 = workbook.CreateFont();
  2974. cellfontbt3.Boldweight = (short)FontBoldWeight.Bold;
  2975. cellStylebt3.WrapText = true;//设置换行这个要先设置
  2976. cellfontbt3.FontHeightInPoints = 14;
  2977. cellStylebt3.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
  2978. cellStylebt3.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2979. cellStylebt3.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2980. cellStylebt3.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2981. cellStylebt3.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2982. cellStylebt3.SetFont(cellfontbt3);
  2983. cellStylebt3.VerticalAlignment = VerticalAlignment.Center;
  2984. cellStylebt3.Alignment = HorizontalAlignment.Center;
  2985. IRow irow1 = sheet.CreateRow(0);
  2986. ICell cell1 = irow1.CreateCell(0);
  2987. cell1.SetCellValue("政民互动综合统计");
  2988. cell1.CellStyle = cellStylebt1;
  2989. IRow irow = sheet.CreateRow(3);
  2990. sheet.AddMergedRegion(new CellRangeAddress(0, 2, 0, 14));
  2991. for (int i = 0; i < 15; i++)
  2992. {
  2993. ICell cell4 = irow.CreateCell(i );
  2994. cell4.CellStyle = cellStylebt2;
  2995. }
  2996. ICell cell2 = irow.CreateCell(6);
  2997. cell2.SetCellValue("状态");
  2998. cell2.CellStyle = cellStylebt2;
  2999. sheet.AddMergedRegion(new CellRangeAddress(3, 3, 6, 9));
  3000. ICell cell3 = irow.CreateCell(10);
  3001. cell3.SetCellValue("已办结");
  3002. cell3.CellStyle = cellStylebt2;
  3003. sheet.AddMergedRegion(new CellRangeAddress(3, 3, 10, 14));
  3004. IRow irow2 = sheet.CreateRow(4);
  3005. string[] cols = { "序号", "工单来源", "工单总量", "转办量"
  3006. , "退件量", "退件率", "已办结", "已办未果"
  3007. , "超期未果" , "办理率", "评议量", "满意量", "不满意量", "满意率", "重新转办量"};
  3008. for (int i = 0; i < cols.Length
  3009. ; i++)
  3010. {
  3011. ICell cell = irow2.CreateCell(i);
  3012. cell.SetCellValue(cols[i]);
  3013. cell.CellStyle = cellStylebt2;
  3014. }
  3015. if (dt != null && dt.Tables[0].Rows.Count > 0)
  3016. {
  3017. for (int i = 0; i < dt.Tables[0].Rows.Count; i++)
  3018. {
  3019. IRow irow3 = sheet.CreateRow(5 + i);
  3020. for (int j = 0; j < cols.Length
  3021. ; j++)
  3022. {
  3023. ICell cell = irow3.CreateCell(j);
  3024. switch (j)
  3025. {
  3026. case 0:
  3027. cell.SetCellValue(i + 1);
  3028. cell.CellStyle = cellStylebt3;
  3029. break;
  3030. case 1:
  3031. cell.SetCellValue(dt.Tables[0].Rows[i]["name"].ToString());
  3032. cell.CellStyle = cellStylebt3;
  3033. break;
  3034. case 2:
  3035. cell.SetCellValue(dt.Tables[0].Rows[i]["totalcount"].ToString());
  3036. cell.CellStyle = cellStylebt3;
  3037. break;
  3038. case 3:
  3039. cell.SetCellValue(dt.Tables[0].Rows[i]["zb"].ToString());
  3040. cell.CellStyle = cellStylebt3;
  3041. break;
  3042. case 4:
  3043. cell.SetCellValue(dt.Tables[0].Rows[i]["td"].ToString());
  3044. cell.CellStyle = cellStylebt3;
  3045. break;
  3046. case 5:
  3047. cell.SetCellValue(dt.Tables[0].Rows[i]["tdrate"].ToString());
  3048. cell.CellStyle = cellStylebt3;
  3049. break;
  3050. case 6:
  3051. cell.SetCellValue(dt.Tables[0].Rows[i]["bj"].ToString());
  3052. cell.CellStyle = cellStylebt3;
  3053. break;
  3054. case 7:
  3055. cell.SetCellValue(dt.Tables[0].Rows[i]["ybwg"].ToString());
  3056. cell.CellStyle = cellStylebt3;
  3057. break;
  3058. case 8:
  3059. cell.SetCellValue(dt.Tables[0].Rows[i]["cqwg"].ToString());
  3060. cell.CellStyle = cellStylebt3;
  3061. break;
  3062. case 9:
  3063. cell.SetCellValue(dt.Tables[0].Rows[i]["blrate"].ToString());
  3064. cell.CellStyle = cellStylebt3;
  3065. break;
  3066. case 10:
  3067. cell.SetCellValue(dt.Tables[0].Rows[i]["py"].ToString());
  3068. cell.CellStyle = cellStylebt3;
  3069. break;
  3070. case 11:
  3071. cell.SetCellValue(dt.Tables[0].Rows[i]["my"].ToString());
  3072. cell.CellStyle = cellStylebt3;
  3073. break;
  3074. case 12:
  3075. cell.SetCellValue(dt.Tables[0].Rows[i]["bmy"].ToString());
  3076. cell.CellStyle = cellStylebt3;
  3077. break;
  3078. case 13:
  3079. cell.SetCellValue(dt.Tables[0].Rows[i]["myrate"].ToString());
  3080. cell.CellStyle = cellStylebt3;
  3081. break;
  3082. case 14:
  3083. cell.SetCellValue(dt.Tables[0].Rows[i]["cxzb"].ToString());
  3084. cell.CellStyle = cellStylebt3;
  3085. break;
  3086. }
  3087. }
  3088. }
  3089. }
  3090. for (int i = 0; i < 10; i++)
  3091. {
  3092. sheet.AutoSizeColumn(i);
  3093. }
  3094. using (MemoryStream ms = new MemoryStream())
  3095. {
  3096. workbook.Write(ms);
  3097. HttpContext curContext = HttpContext.Current;
  3098. // 设置编码和附件格式
  3099. curContext.Response.ContentType = "application/vnd.ms-excel";
  3100. curContext.Response.ContentEncoding = Encoding.UTF8;
  3101. curContext.Response.Charset = "";
  3102. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  3103. curContext.Response.AppendHeader("Content-Disposition",
  3104. "attachment;filename=" + HttpUtility.UrlEncode("政民互动综合统计" + ".xls", Encoding.UTF8));
  3105. curContext.Response.BinaryWrite(ms.GetBuffer());
  3106. workbook = null;
  3107. ms.Close();
  3108. ms.Dispose();
  3109. curContext.Response.End();
  3110. }
  3111. return "";
  3112. }
  3113. catch (Exception e)
  3114. {
  3115. return e.Message;
  3116. }
  3117. }
  3118. /// <summary>
  3119. /// 弹出下载框导出excel
  3120. /// </summary>
  3121. /// <param name="Name"></param>
  3122. /// <param name="dt"></param>
  3123. /// <returns></returns>
  3124. public string TSExportToExcel(DataTable dt, int tscount)
  3125. {
  3126. try
  3127. {
  3128. //if (dt.Rows.Count > 0)
  3129. //{
  3130. HSSFWorkbook workbook = new HSSFWorkbook();
  3131. ISheet sheet = workbook.CreateSheet("Sheet1");
  3132. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  3133. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  3134. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  3135. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  3136. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  3137. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  3138. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  3139. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  3140. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  3141. //字体
  3142. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  3143. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  3144. headerfont.FontHeightInPoints = 12;
  3145. HeadercellStyle.SetFont(headerfont);
  3146. //用column name 作为列名
  3147. int icolIndex = 0;
  3148. IRow headerRow = sheet.CreateRow(0);
  3149. foreach (DataColumn dc in dt.Columns)
  3150. {
  3151. ICell cell = headerRow.CreateCell(icolIndex);
  3152. cell.SetCellValue(dc.ColumnName);
  3153. cell.CellStyle = HeadercellStyle;
  3154. icolIndex++;
  3155. }
  3156. ICellStyle cellStyle = workbook.CreateCellStyle();
  3157. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  3158. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  3159. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  3160. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  3161. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  3162. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  3163. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  3164. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  3165. cellStyle.SetFont(cellfont);
  3166. //建立内容行
  3167. int iRowIndex = 0;
  3168. foreach (DataRow dr in dt.Rows)
  3169. {
  3170. int iCellIndex = 0;
  3171. IRow irow = sheet.CreateRow(iRowIndex + 1);
  3172. for (int i = 0; i < dt.Columns.Count; i++)
  3173. {
  3174. string strsj = string.Empty;
  3175. if (dr[i] != null)
  3176. {
  3177. strsj = dr[i].ToString();
  3178. }
  3179. ICell cell = irow.CreateCell(iCellIndex);
  3180. cell.SetCellValue(strsj);
  3181. cell.CellStyle = cellStyle;
  3182. iCellIndex++;
  3183. }
  3184. iRowIndex++;
  3185. }
  3186. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 1));
  3187. sheet.AddMergedRegion(new CellRangeAddress(1, tscount, 0, 0));
  3188. for (int i = iRowIndex; i > tscount; i--)
  3189. {
  3190. sheet.AddMergedRegion(new CellRangeAddress(i, i, 0, 1));
  3191. }
  3192. //自适应列宽度
  3193. for (int i = 0; i < icolIndex; i++)
  3194. {
  3195. sheet.AutoSizeColumn(i);
  3196. }
  3197. using (MemoryStream ms = new MemoryStream())
  3198. {
  3199. workbook.Write(ms);
  3200. HttpContext curContext = HttpContext.Current;
  3201. // 设置编码和附件格式
  3202. curContext.Response.ContentType = "application/vnd.ms-excel";
  3203. curContext.Response.ContentEncoding = Encoding.UTF8;
  3204. curContext.Response.Charset = "";
  3205. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  3206. curContext.Response.AppendHeader("Content-Disposition",
  3207. "attachment;filename=" + HttpUtility.UrlEncode("投诉统计_导出文件_" + DateTime.Now.Ticks + ".xls", Encoding.UTF8));
  3208. curContext.Response.BinaryWrite(ms.GetBuffer());
  3209. workbook = null;
  3210. ms.Close();
  3211. ms.Dispose();
  3212. curContext.Response.End();
  3213. }
  3214. //}
  3215. return "";
  3216. }
  3217. catch
  3218. {
  3219. return "导出失败!";
  3220. }
  3221. }
  3222. /// <summary>
  3223. /// 生成excel到路径
  3224. /// </summary>
  3225. /// <param name="Name"></param>
  3226. /// <param name="dt"></param>
  3227. /// <returns></returns>
  3228. public string CreateExcelFile(string Name, DataTable dt, string Path,string[] cols = null)
  3229. {
  3230. try
  3231. {
  3232. if (dt.Rows.Count > 0)
  3233. {
  3234. HSSFWorkbook workbook = new HSSFWorkbook();
  3235. ISheet sheet = workbook.CreateSheet(Name);
  3236. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  3237. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  3238. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  3239. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  3240. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  3241. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  3242. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  3243. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  3244. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  3245. //字体
  3246. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  3247. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  3248. headerfont.FontHeightInPoints = 12;
  3249. HeadercellStyle.SetFont(headerfont);
  3250. //用column name 作为列名
  3251. int icolIndex = 0;
  3252. IRow headerRow = sheet.CreateRow(0);
  3253. if (cols == null || (cols != null && cols.Length == 0))
  3254. {
  3255. foreach (DataColumn dc in dt.Columns)
  3256. {
  3257. ICell cell = headerRow.CreateCell(icolIndex);
  3258. cell.SetCellValue(dc.ColumnName);
  3259. cell.CellStyle = HeadercellStyle;
  3260. icolIndex++;
  3261. }
  3262. }
  3263. else
  3264. {
  3265. foreach (string dc in cols)
  3266. {
  3267. ICell cell = headerRow.CreateCell(icolIndex);
  3268. cell.SetCellValue(dc);
  3269. cell.CellStyle = HeadercellStyle;
  3270. icolIndex++;
  3271. }
  3272. }
  3273. ICellStyle cellStyle = workbook.CreateCellStyle();
  3274. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  3275. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  3276. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  3277. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  3278. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  3279. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  3280. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  3281. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  3282. cellStyle.SetFont(cellfont);
  3283. //建立内容行
  3284. int iRowIndex = 0;
  3285. foreach (DataRow dr in dt.Rows)
  3286. {
  3287. int iCellIndex = 0;
  3288. IRow irow = sheet.CreateRow(iRowIndex + 1);
  3289. for (int i = 0; i < dt.Columns.Count; i++)
  3290. {
  3291. string strsj = string.Empty;
  3292. if (dr[i] != null)
  3293. {
  3294. strsj = dr[i].ToString();
  3295. }
  3296. ICell cell = irow.CreateCell(iCellIndex);
  3297. cell.SetCellValue(strsj);
  3298. cell.CellStyle = cellStyle;
  3299. iCellIndex++;
  3300. }
  3301. iRowIndex++;
  3302. }
  3303. //自适应列宽度
  3304. for (int i = 0; i < icolIndex; i++)
  3305. {
  3306. sheet.AutoSizeColumn(i);
  3307. }
  3308. Name = HttpUtility.UrlEncode(Name + "_导出文件_" + DateTime.Now.Ticks + ".xls");
  3309. Path = HttpContext.Current.Server.MapPath("..") + Path;
  3310. if (!Directory.Exists(Path)) Directory.CreateDirectory(Path);
  3311. FileStream fileHSSF = new FileStream(Path+ Name, FileMode.Create);
  3312. workbook.Write(fileHSSF);
  3313. fileHSSF.Close();
  3314. fileHSSF.Dispose();
  3315. workbook = null;
  3316. }
  3317. return "";
  3318. }
  3319. catch
  3320. {
  3321. return "生成失败!";
  3322. }
  3323. }
  3324. /// <summary>
  3325. /// 弹出下载框导出excel
  3326. /// </summary>
  3327. /// <param name="Name"></param>
  3328. /// <param name="dt"></param>
  3329. /// <returns></returns>
  3330. public string ExportToExcel64(string name, string base64url)
  3331. {
  3332. try
  3333. {
  3334. int delLength = base64url.IndexOf(',') + 1;
  3335. string str = base64url.Substring(delLength, base64url.Length - delLength);
  3336. byte[] bData = Convert.FromBase64String(str);
  3337. HttpContext curContext = HttpContext.Current;
  3338. // 设置编码和附件格式
  3339. curContext.Response.ContentType = "application/vnd.ms-excel";
  3340. curContext.Response.ContentEncoding = Encoding.UTF8;
  3341. curContext.Response.Charset = "";
  3342. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  3343. curContext.Response.AppendHeader("Content-Disposition",
  3344. "attachment;filename=" + HttpUtility.UrlEncode(name + "_导出文件_" + DateTime.Now.Ticks + ".xls", Encoding.UTF8));
  3345. curContext.Response.BinaryWrite(bData);
  3346. curContext.Response.End();
  3347. return "";
  3348. }
  3349. catch
  3350. {
  3351. return "导出失败!";
  3352. }
  3353. }
  3354. /// <summary>
  3355. /// 简报导出
  3356. /// </summary>
  3357. /// <param name="ds"></param>
  3358. /// <returns></returns>
  3359. public string SimpleExportToExcel(DataSet ds)
  3360. {
  3361. try
  3362. {
  3363. HSSFWorkbook workbook = new HSSFWorkbook();
  3364. ISheet sheet = workbook.CreateSheet("Sheet1");
  3365. ICellStyle cellStyle = workbook.CreateCellStyle();
  3366. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  3367. //cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  3368. //cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  3369. //cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  3370. //cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  3371. //cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  3372. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  3373. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  3374. cellStyle.SetFont(cellfont);
  3375. ICellStyle cellStylebt = workbook.CreateCellStyle();
  3376. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  3377. cellfontbt.Boldweight = (short)FontBoldWeight.Bold;
  3378. cellStylebt.SetFont(cellfontbt);
  3379. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  3380. cellStylebt.Alignment = HorizontalAlignment.Center;
  3381. IRow irow1 = sheet.CreateRow(1);
  3382. ICell cell1 = irow1.CreateCell(0);
  3383. cell1.SetCellValue("时间:"+ ds.Tables[5].Rows[0]["sdate"].ToString()+ " 至 "+ ds.Tables[5].Rows[0]["edate"].ToString());
  3384. cell1.CellStyle = cellStylebt;
  3385. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 0, 7));
  3386. //((HSSFSheet)sheet).SetEnclosedBorderOfRegion(new CellRangeAddress(1, 1, 0, 7), BorderStyle.Thin, NPOI.HSSF.Util.HSSFColor.Black.Index);
  3387. #region 话务受理情况
  3388. DataTable dt1 = ds.Tables[0];
  3389. IRow irow2 = sheet.CreateRow(2);
  3390. ICell cell2 = irow2.CreateCell(0);
  3391. cell2.SetCellValue("话务受理情况");
  3392. cell2.CellStyle = cellStylebt;
  3393. sheet.AddMergedRegion(new CellRangeAddress(2, 2, 0, 7));
  3394. IRow irow3 = sheet.CreateRow(3);
  3395. ICell cell31 = irow3.CreateCell(0);
  3396. cell31.SetCellValue("来电");
  3397. cell31.CellStyle = cellStyle;
  3398. ICell cell32 = irow3.CreateCell(1);
  3399. cell32.SetCellValue(Int32.Parse(dt1.Rows[0]["ldcount"].ToString()));
  3400. cell32.CellStyle = cellStyle;
  3401. ICell cell33 = irow3.CreateCell(2);
  3402. cell33.SetCellValue("接听");
  3403. cell33.CellStyle = cellStyle;
  3404. ICell cell34 = irow3.CreateCell(3);
  3405. cell34.SetCellValue(Int32.Parse(dt1.Rows[0]["jtcount"].ToString()));
  3406. cell34.CellStyle = cellStyle;
  3407. ICell cell35 = irow3.CreateCell(4);
  3408. cell35.SetCellValue("有效接听");
  3409. cell35.CellStyle = cellStyle;
  3410. ICell cell36 = irow3.CreateCell(5);
  3411. cell36.SetCellValue(Int32.Parse(dt1.Rows[0]["yxcount"].ToString()));
  3412. cell36.CellStyle = cellStyle;
  3413. #endregion
  3414. #region 工单受理情况
  3415. DataTable dt2 = ds.Tables[1];
  3416. IRow irow4 = sheet.CreateRow(4);
  3417. ICell cell4 = irow4.CreateCell(0);
  3418. cell4.SetCellValue("工单受理情况");
  3419. cell4.CellStyle = cellStylebt;
  3420. sheet.AddMergedRegion(new CellRangeAddress(4, 4, 0, 7));
  3421. IRow irow5 = sheet.CreateRow(5);
  3422. ICell cell51 = irow5.CreateCell(0);
  3423. cell51.SetCellValue("受理");
  3424. cell51.CellStyle = cellStyle;
  3425. ICell cell52 = irow5.CreateCell(1);
  3426. cell52.SetCellValue(Int32.Parse(dt2.Rows[0]["slcount"].ToString()));
  3427. cell52.CellStyle = cellStyle;
  3428. ICell cell53 = irow5.CreateCell(2);
  3429. cell53.SetCellValue("待提交");
  3430. cell53.CellStyle = cellStyle;
  3431. ICell cell54 = irow5.CreateCell(3);
  3432. cell54.SetCellValue(Int32.Parse(dt2.Rows[0]["dtjcount"].ToString()));
  3433. cell54.CellStyle = cellStyle;
  3434. ICell cell55 = irow5.CreateCell(4);
  3435. cell55.SetCellValue("待交办");
  3436. cell55.CellStyle = cellStyle;
  3437. ICell cell56 = irow5.CreateCell(5);
  3438. cell56.SetCellValue(Int32.Parse(dt2.Rows[0]["djbcount"].ToString()));
  3439. cell56.CellStyle = cellStyle;
  3440. ICell cell57 = irow5.CreateCell(6);
  3441. cell57.SetCellValue("待查收");
  3442. cell57.CellStyle = cellStyle;
  3443. ICell cell58 = irow5.CreateCell(7);
  3444. cell58.SetCellValue(Int32.Parse(dt2.Rows[0]["dcscount"].ToString()));
  3445. cell58.CellStyle = cellStyle;
  3446. IRow irow6 = sheet.CreateRow(6);
  3447. ICell cell61 = irow6.CreateCell(0);
  3448. cell61.SetCellValue("待审核退回");
  3449. cell61.CellStyle = cellStyle;
  3450. ICell cell62 = irow6.CreateCell(1);
  3451. cell62.SetCellValue(Int32.Parse(dt2.Rows[0]["dshthcount"].ToString()));
  3452. cell62.CellStyle = cellStyle;
  3453. ICell cell63 = irow6.CreateCell(2);
  3454. cell63.SetCellValue("待办理");
  3455. cell63.CellStyle = cellStyle;
  3456. ICell cell64 = irow6.CreateCell(3);
  3457. cell64.SetCellValue(Int32.Parse(dt2.Rows[0]["dblcount"].ToString()));
  3458. cell64.CellStyle = cellStyle;
  3459. ICell cell65 = irow6.CreateCell(4);
  3460. cell65.SetCellValue("待延时审核");
  3461. cell65.CellStyle = cellStyle;
  3462. ICell cell66 = irow6.CreateCell(5);
  3463. cell66.SetCellValue(Int32.Parse(dt2.Rows[0]["dshyscount"].ToString()));
  3464. cell66.CellStyle = cellStyle;
  3465. ICell cell67 = irow6.CreateCell(6);
  3466. cell67.SetCellValue("待回访");
  3467. cell67.CellStyle = cellStyle;
  3468. ICell cell68 = irow6.CreateCell(7);
  3469. cell68.SetCellValue(Int32.Parse(dt2.Rows[0]["dhfcount"].ToString()));
  3470. cell68.CellStyle = cellStyle;
  3471. IRow irow7 = sheet.CreateRow(7);
  3472. ICell cell71 = irow7.CreateCell(0);
  3473. cell71.SetCellValue("待结案");
  3474. cell71.CellStyle = cellStyle;
  3475. ICell cell72 = irow7.CreateCell(1);
  3476. cell72.SetCellValue(Int32.Parse(dt2.Rows[0]["dwjcount"].ToString()));
  3477. cell72.CellStyle = cellStyle;
  3478. ICell cell73 = irow7.CreateCell(2);
  3479. cell73.SetCellValue("待重办");
  3480. cell73.CellStyle = cellStyle;
  3481. ICell cell74 = irow7.CreateCell(3);
  3482. cell74.SetCellValue(Int32.Parse(dt2.Rows[0]["dcbcount"].ToString()));
  3483. cell74.CellStyle = cellStyle;
  3484. ICell cell75 = irow7.CreateCell(4);
  3485. cell75.SetCellValue("已结案");
  3486. cell75.CellStyle = cellStyle;
  3487. ICell cell76 = irow7.CreateCell(5);
  3488. cell76.SetCellValue(Int32.Parse(dt2.Rows[0]["ywjcount"].ToString()));
  3489. cell76.CellStyle = cellStyle;
  3490. ICell cell77 = irow7.CreateCell(6);
  3491. cell77.SetCellValue("在线办理");
  3492. cell77.CellStyle = cellStyle;
  3493. ICell cell78 = irow7.CreateCell(7);
  3494. cell78.SetCellValue(Int32.Parse(dt2.Rows[0]["zxbjcount"].ToString()));
  3495. cell78.CellStyle = cellStyle;
  3496. IRow irow8 = sheet.CreateRow(8);
  3497. ICell cell81 = irow8.CreateCell(0);
  3498. cell81.SetCellValue("中心转派");
  3499. cell81.CellStyle = cellStyle;
  3500. ICell cell82 = irow8.CreateCell(1);
  3501. cell82.SetCellValue(Int32.Parse(dt2.Rows[0]["zxzpcount"].ToString()));
  3502. cell82.CellStyle = cellStyle;
  3503. ICell cell83 = irow8.CreateCell(2);
  3504. cell83.SetCellValue("在线办理率");
  3505. cell83.CellStyle = cellStyle;
  3506. ICell cell84 = irow8.CreateCell(3);
  3507. cell84.SetCellValue(dt2.Rows[0]["zxbjrate"].ToString());
  3508. cell84.CellStyle = cellStyle;
  3509. #endregion
  3510. #region 政府热线受理情况
  3511. DataTable dt3 = ds.Tables[2];
  3512. IRow irow9 = sheet.CreateRow(9);
  3513. ICell cell9 = irow9.CreateCell(0);
  3514. cell9.SetCellValue("政府热线受理情况");
  3515. cell9.CellStyle = cellStylebt;
  3516. sheet.AddMergedRegion(new CellRangeAddress(9, 9, 0, 7));
  3517. IRow irow10 = sheet.CreateRow(10);
  3518. IRow irow11 = sheet.CreateRow(11);
  3519. int n = 0;
  3520. foreach (DataRow dr3 in dt3.Rows)
  3521. {
  3522. if (n < 4)
  3523. {
  3524. ICell cell101 = irow10.CreateCell(2 * (n + 1) - 2);
  3525. cell101.SetCellValue(dr3["source"].ToString());
  3526. cell101.CellStyle = cellStyle;
  3527. ICell cell102 = irow10.CreateCell(2 * (n + 1) - 1);
  3528. cell102.SetCellValue(Int32.Parse(dr3["count"].ToString()));
  3529. cell102.CellStyle = cellStyle;
  3530. }
  3531. else
  3532. {
  3533. ICell cell111 = irow11.CreateCell(2 * (n -3) - 2);
  3534. cell111.SetCellValue(dr3["source"].ToString());
  3535. cell111.CellStyle = cellStyle;
  3536. ICell cell112 = irow11.CreateCell(2 * (n - 3) - 1);
  3537. cell112.SetCellValue(Int32.Parse(dr3["count"].ToString()));
  3538. cell112.CellStyle = cellStyle;
  3539. }
  3540. n = n + 1;
  3541. }
  3542. #endregion
  3543. #region 工单受理类型情况
  3544. DataTable dt4 = ds.Tables[3];
  3545. IRow irow12 = sheet.CreateRow(12);
  3546. ICell cell12 = irow12.CreateCell(0);
  3547. cell12.SetCellValue("工单受理类型情况");
  3548. cell12.CellStyle = cellStylebt;
  3549. sheet.AddMergedRegion(new CellRangeAddress(12, 12, 0, 7));
  3550. IRow irow13 = sheet.CreateRow(13);
  3551. IRow irow14 = sheet.CreateRow(14);
  3552. int m = 0;
  3553. foreach (DataRow dr4 in dt4.Rows)
  3554. {
  3555. if (m < 4)
  3556. {
  3557. ICell cell131 = irow13.CreateCell(2 * (m + 1) - 2);
  3558. cell131.SetCellValue(dr4["type"].ToString());
  3559. cell131.CellStyle = cellStyle;
  3560. ICell cell132 = irow13.CreateCell(2 * (m + 1) - 1);
  3561. cell132.SetCellValue(Int32.Parse(dr4["count"].ToString()));
  3562. cell132.CellStyle = cellStyle;
  3563. }
  3564. else
  3565. {
  3566. ICell cell141 = irow14.CreateCell(2 * (m - 3) - 2);
  3567. cell141.SetCellValue(dr4["type"].ToString());
  3568. cell141.CellStyle = cellStyle;
  3569. ICell cell152 = irow14.CreateCell(2 * (m - 3) - 1);
  3570. cell152.SetCellValue(Int32.Parse(dr4["count"].ToString()));
  3571. cell152.CellStyle = cellStyle;
  3572. }
  3573. m = m + 1;
  3574. }
  3575. #endregion
  3576. #region 工单事发区域情况
  3577. DataTable dt5 = ds.Tables[4];
  3578. IRow irow15 = sheet.CreateRow(15);
  3579. ICell cell15 = irow15.CreateCell(0);
  3580. cell15.SetCellValue("工单事发区域情况");
  3581. cell15.CellStyle = cellStylebt;
  3582. sheet.AddMergedRegion(new CellRangeAddress(15, 15, 0, 7));
  3583. int rowcount = (dt5.Rows.Count / 4) + 1;
  3584. for (int i = 0; i < rowcount; i++)
  3585. {
  3586. IRow irow = sheet.CreateRow(16 + i);
  3587. for (int j = 0; j < 4; j++)
  3588. {
  3589. int num = i * 4 + j;
  3590. if (num != dt5.Rows.Count)
  3591. {
  3592. var dr = dt5.Rows[num];
  3593. ICell cellname = irow.CreateCell(2 * (j + 1) - 2);
  3594. cellname.SetCellValue(dr["areaname"].ToString());
  3595. cellname.CellStyle = cellStyle;
  3596. ICell cellcount = irow.CreateCell(2 * (j + 1) - 1);
  3597. cellcount.SetCellValue(Int32.Parse(dr["count"].ToString()));
  3598. cellcount.CellStyle = cellStyle;
  3599. }
  3600. else
  3601. {
  3602. break;
  3603. }
  3604. }
  3605. }
  3606. #endregion
  3607. //自适应列宽度
  3608. for (int i = 0; i < 8; i++)
  3609. {
  3610. sheet.AutoSizeColumn(i);
  3611. }
  3612. using (MemoryStream ms = new MemoryStream())
  3613. {
  3614. workbook.Write(ms);
  3615. HttpContext curContext = HttpContext.Current;
  3616. // 设置编码和附件格式
  3617. curContext.Response.ContentType = "application/vnd.ms-excel";
  3618. curContext.Response.ContentEncoding = Encoding.UTF8;
  3619. curContext.Response.Charset = "";
  3620. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  3621. curContext.Response.AppendHeader("Content-Disposition",
  3622. "attachment;filename=" + HttpUtility.UrlEncode("业务简报_导出文件_" + DateTime.Now.Ticks + ".xls", Encoding.UTF8));
  3623. curContext.Response.BinaryWrite(ms.GetBuffer());
  3624. workbook = null;
  3625. ms.Close();
  3626. ms.Dispose();
  3627. curContext.Response.End();
  3628. }
  3629. return "";
  3630. }
  3631. catch
  3632. {
  3633. return "导出失败!";
  3634. }
  3635. }
  3636. /// <summary>
  3637. /// 弹出下载框导出excel(数据第一行为标题)
  3638. /// </summary>
  3639. /// <param name="Name"></param>
  3640. /// <param name="dt"></param>
  3641. /// <returns></returns>
  3642. public string ExportToExcel2(string Name, DataTable dt, string[] cols = null)
  3643. {
  3644. try
  3645. {
  3646. //if (dt.Rows.Count > 0)
  3647. //{
  3648. HSSFWorkbook workbook = new HSSFWorkbook();
  3649. ISheet sheet = workbook.CreateSheet("Sheet1");
  3650. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  3651. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  3652. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  3653. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  3654. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  3655. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  3656. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  3657. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  3658. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  3659. //字体
  3660. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  3661. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  3662. headerfont.FontHeightInPoints = 12;
  3663. HeadercellStyle.SetFont(headerfont);
  3664. //用column name 作为列名
  3665. int icolIndex = 0;
  3666. IRow headerRow = sheet.CreateRow(0);
  3667. if (cols == null || (cols != null && cols.Length == 0))
  3668. {
  3669. //foreach (DataColumn dc in dt.Columns)
  3670. //{
  3671. // ICell cell = headerRow.CreateCell(icolIndex);
  3672. // cell.SetCellValue(dc.ColumnName);
  3673. // cell.CellStyle = HeadercellStyle;
  3674. // icolIndex++;
  3675. //}
  3676. for (int i = 0; i < dt.Columns.Count; i++)
  3677. {
  3678. ICell cell = headerRow.CreateCell(icolIndex);
  3679. cell.SetCellValue(dt.Rows[0][i].ToString());
  3680. cell.CellStyle = HeadercellStyle;
  3681. icolIndex++;
  3682. }
  3683. }
  3684. else
  3685. {
  3686. foreach (string dc in cols)
  3687. {
  3688. ICell cell = headerRow.CreateCell(icolIndex);
  3689. cell.SetCellValue(dc);
  3690. cell.CellStyle = HeadercellStyle;
  3691. icolIndex++;
  3692. }
  3693. }
  3694. ICellStyle cellStyle = workbook.CreateCellStyle();
  3695. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  3696. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  3697. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  3698. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  3699. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  3700. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  3701. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  3702. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  3703. cellStyle.SetFont(cellfont);
  3704. //建立内容行
  3705. int iRowIndex = 0;
  3706. foreach (DataRow dr in dt.Rows)
  3707. {
  3708. if (iRowIndex > 0)
  3709. {
  3710. int iCellIndex = 0;
  3711. IRow irow = sheet.CreateRow(iRowIndex);
  3712. for (int i = 0; i < dt.Columns.Count; i++)
  3713. {
  3714. string strsj = string.Empty;
  3715. if (dr[i] != null)
  3716. {
  3717. strsj = dr[i].ToString();
  3718. }
  3719. ICell cell = irow.CreateCell(iCellIndex);
  3720. cell.SetCellValue(strsj);
  3721. cell.CellStyle = cellStyle;
  3722. iCellIndex++;
  3723. }
  3724. }
  3725. iRowIndex++;
  3726. }
  3727. //自适应列宽度
  3728. for (int i = 0; i < icolIndex; i++)
  3729. {
  3730. sheet.AutoSizeColumn(i);
  3731. }
  3732. using (MemoryStream ms = new MemoryStream())
  3733. {
  3734. workbook.Write(ms);
  3735. HttpContext curContext = HttpContext.Current;
  3736. // 设置编码和附件格式
  3737. curContext.Response.ContentType = "application/vnd.ms-excel";
  3738. curContext.Response.ContentEncoding = Encoding.UTF8;
  3739. curContext.Response.Charset = "";
  3740. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  3741. curContext.Response.AppendHeader("Content-Disposition",
  3742. "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
  3743. curContext.Response.BinaryWrite(ms.GetBuffer());
  3744. workbook = null;
  3745. ms.Close();
  3746. ms.Dispose();
  3747. curContext.Response.End();
  3748. }
  3749. //}
  3750. return "";
  3751. }
  3752. catch
  3753. {
  3754. return "导出失败!";
  3755. }
  3756. }
  3757. /// <summary>
  3758. /// 导出word
  3759. /// </summary>
  3760. /// <returns></returns>
  3761. public byte[] ExportToWordDemo()
  3762. {
  3763. try
  3764. {
  3765. //创建document文档对象对象实例
  3766. NPOI.XWPF.UserModel.XWPFDocument document = new NPOI.XWPF.UserModel.XWPFDocument();
  3767. document.CreateParagraph();
  3768. NPOI.XWPF.UserModel.XWPFParagraph paragraph = document.CreateParagraph();//创建段落对象
  3769. paragraph.Alignment = NPOI.XWPF.UserModel.ParagraphAlignment.CENTER;//文字显示位置,段落排列(左对齐,居中,右对齐)
  3770. NPOI.XWPF.UserModel.XWPFRun xwpfRun = paragraph.CreateRun();//创建段落文本对象
  3771. xwpfRun.IsBold = true;//文字加粗
  3772. xwpfRun.SetText("安阳市12345政务服务热线交办单");//填充内容
  3773. xwpfRun.FontSize = 18;//设置文字大小
  3774. xwpfRun.SetFontFamily("宋体", NPOI.XWPF.UserModel.FontCharRange.None); //设置标题样式如:(微软雅黑,隶书,楷体)根据自己的需求而定
  3775. document.CreateParagraph();
  3776. //创建文档中的表格对象实例
  3777. NPOI.XWPF.UserModel.XWPFTable table = document.CreateTable(9, 8);//显示的行列数rows:3行,cols:4列
  3778. table.Width = 5200;//总宽度
  3779. table.SetColumnWidth(0, 650); /* 设置列宽 */
  3780. table.SetColumnWidth(1, 650);
  3781. table.SetColumnWidth(2, 650);
  3782. table.SetColumnWidth(3, 650);
  3783. table.SetColumnWidth(4, 650);
  3784. table.SetColumnWidth(5, 650);
  3785. table.SetColumnWidth(6, 650);
  3786. table.SetColumnWidth(7, 650);
  3787. for (int i = 0; i < 9; i++)
  3788. {
  3789. for (int j = 0; j < 8; j++)
  3790. {
  3791. var cp = table.GetRow(i).GetCell(j).GetCTTc().AddNewTcPr();
  3792. cp.AddNewVAlign().val = ST_VerticalJc.center;
  3793. }
  3794. }
  3795. //第一行
  3796. table.GetRow(0).GetCTRow().AddNewTrPr().AddNewTrHeight().val= (ulong)300;
  3797. var cp0 = table.GetRow(0).GetCell(0).GetCTTc().AddNewTcPr();
  3798. cp0.AddNewVMerge().val = ST_Merge.restart;
  3799. cp0.AddNewVAlign().val = ST_VerticalJc.center;
  3800. cp0.tcW = new CT_TblWidth();cp0.tcW.w = "650";cp0.tcW.type = ST_TblWidth.dxa;
  3801. table.GetRow(0).GetCell(0).SetParagraph(GetParagraph(table, true, "事项编号"));
  3802. var cp1 = table.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr();
  3803. cp1.AddNewVMerge().val = ST_Merge.restart;
  3804. cp1.AddNewVAlign().val = ST_VerticalJc.center;
  3805. table.GetRow(0).GetCell(1).SetParagraph(GetParagraph(table, false, "DH9941052721041200351"));
  3806. table.GetRow(0).GetCell(2).SetParagraph(GetParagraph(table, true, "工单来源"));
  3807. table.GetRow(0).GetCell(3).SetParagraph(GetParagraph(table, false, "市长电话"));
  3808. table.GetRow(0).GetCell(4).SetParagraph(GetParagraph(table, true, "办理时限"));
  3809. table.GetRow(0).GetCell(5).SetParagraph(GetParagraph(table, false, "2021-04-21"));
  3810. table.GetRow(0).GetCell(6).SetParagraph(GetParagraph(table, true, "联系电话"));
  3811. table.GetRow(0).GetCell(7).SetParagraph(GetParagraph(table, false, "017185328292"));
  3812. //第二行
  3813. table.GetRow(1).GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)300;
  3814. var cp2 = table.GetRow(1).GetCell(0).GetCTTc().AddNewTcPr();
  3815. cp2.AddNewVMerge().val = ST_Merge.@continue;
  3816. var cp3 = table.GetRow(1).GetCell(1).GetCTTc().AddNewTcPr();
  3817. cp3.AddNewVMerge().val = ST_Merge.@continue;
  3818. table.GetRow(1).GetCell(2).SetParagraph(GetParagraph(table, true, "签收时间"));
  3819. table.GetRow(1).GetCell(3).SetParagraph(GetParagraph(table, false, "2021-04-12"));
  3820. table.GetRow(1).GetCell(4).SetParagraph(GetParagraph(table, true, "办理时间"));
  3821. table.GetRow(1).GetCell(5).SetParagraph(GetParagraph(table, false, "2021-04-12"));
  3822. table.GetRow(1).GetCell(6).SetParagraph(GetParagraph(table, true, "来电人"));
  3823. table.GetRow(1).GetCell(7).SetParagraph(GetParagraph(table, false, "徐青田"));
  3824. //第三行
  3825. table.GetRow(2).GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)300;
  3826. table.GetRow(2).MergeCells(1, 7);//合并列
  3827. table.GetRow(2).GetCell(0).SetParagraph(GetParagraph(table, true, "事项区域"));
  3828. table.GetRow(2).GetCell(1).SetParagraph(GetParagraph(table, false, "河南省郑州市高新技术开发区大学科技园东区", false));
  3829. //第四行
  3830. table.GetRow(3).GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)2000;
  3831. table.GetRow(3).MergeCells(1, 7);//合并列
  3832. table.GetRow(3).GetCell(0).SetParagraph(GetParagraph(table, true, "内容摘要"));
  3833. table.GetRow(3).GetCell(1).SetParagraph(GetParagraph(table, false, "来电人反映:内黄县城关镇宛庄村第四生产队第二、三小组土地至今无法确权(遗留问题),村委会和镇政府承诺给解决,至今未解决,请政府责成相关部门调查处理。", false));
  3834. //第五行
  3835. table.GetRow(4).GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)300;
  3836. table.GetRow(4).MergeCells(1, 4);//合并列
  3837. table.GetRow(4).MergeCells(3, 4);//合并列
  3838. table.GetRow(4).GetCell(0).SetParagraph(GetParagraph(table, true, "承办单位"));
  3839. table.GetRow(4).GetCell(1).SetParagraph(GetParagraph(table, false, "内黄县政府"));
  3840. table.GetRow(4).GetCell(2).SetParagraph(GetParagraph(table, true, "协办单位"));
  3841. table.GetRow(4).GetCell(3).SetParagraph(GetParagraph(table, false, "内黄县政府"));
  3842. //第六行
  3843. table.GetRow(5).GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)2000;
  3844. table.GetRow(5).MergeCells(1, 7);//合并列
  3845. table.GetRow(5).GetCell(0).SetParagraph(GetParagraph(table, true, "调度意见"));
  3846. table.GetRow(5).GetCell(1).SetParagraph(GetParagraph(table, false, "请按领导批示认真调查处理并答复来电人,在5个工作日内通过网络反馈市市长热线办公室。要求注明主办人、主管领导姓名和联系电话,同时提供有关处理依据。", false));
  3847. //第七行
  3848. table.GetRow(6).GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)1500;
  3849. table.GetRow(6).MergeCells(1, 7);//合并列
  3850. table.GetRow(6).GetCell(0).SetParagraph(GetParagraph(table, true, "承办意见"));
  3851. table.GetRow(6).GetCell(1).SetParagraph(GetParagraph(table, false, "请按领导批示认真调查处理并答复来电人,在5个工作日内通过网络反馈市市长热线办公室。要求注明主办人、主管领导姓名和联系电话,同时提供有关处理依据。", false));
  3852. //第八行
  3853. table.GetRow(7).GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)1500;
  3854. table.GetRow(7).MergeCells(1, 7);//合并列
  3855. table.GetRow(7).GetCell(0).SetParagraph(GetParagraph(table, true, "领导批示"));
  3856. table.GetRow(7).GetCell(1).SetParagraph(GetParagraph(table, false, "请按领导批示认真调查处理并答复来电人,在5个工作日内通过网络反馈市市长热线办公室。要求注明主办人、主管领导姓名和联系电话,同时提供有关处理依据。", false));
  3857. //第九行
  3858. table.GetRow(8).GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)2000;
  3859. table.GetRow(8).MergeCells(1, 7);//合并列
  3860. table.GetRow(8).GetCell(0).SetParagraph(GetParagraph(table, true, "处理结果"));
  3861. table.GetRow(8).GetCell(1).SetParagraph(GetParagraph(table, false, "2021-04-12 10:19:14 内黄县政府部门接到市长电话来源工单(编号:DH9941052721041200351)反映土地确权问题,已联系郑州第三方中标土地确权公司负责人待工作人员到我县后,就可以确权 [办理结果:已办理,承办人:刘志强,职务:城关镇人大主席,联系电话:15824608111,刘志强向当事人进行了反馈,当事人对结果表示未评价。]", false));
  3862. document.CreateParagraph();
  3863. NPOI.XWPF.UserModel.XWPFParagraph paragraph1 = document.CreateParagraph();//创建段落对象
  3864. paragraph1.Alignment = NPOI.XWPF.UserModel.ParagraphAlignment.CENTER;//文字显示位置,段落排列(左对齐,居中,右对齐)
  3865. NPOI.XWPF.UserModel.XWPFRun xwpfRun1 = paragraph1.CreateRun();//创建段落文本对象
  3866. xwpfRun1.IsBold = true;//文字加粗
  3867. xwpfRun1.SetText("联系电话:(0372) 12345审核人:8000 调度员:8065");//填充内容
  3868. xwpfRun1.FontSize = 9;//设置文字大小
  3869. xwpfRun1.SetFontFamily("宋体", NPOI.XWPF.UserModel.FontCharRange.None); //设置标题样式如:(微软雅黑,隶书,楷体)根据自己的需求而定
  3870. NPOI.XWPF.UserModel.XWPFParagraph paragraph2 = document.CreateParagraph();//创建段落对象
  3871. paragraph2.Alignment = NPOI.XWPF.UserModel.ParagraphAlignment.RIGHT;//文字显示位置,段落排列(左对齐,居中,右对齐)
  3872. NPOI.XWPF.UserModel.XWPFRun xwpfRun2 = paragraph2.CreateRun();//创建段落文本对象
  3873. using (FileStream picData = new FileStream(HttpContext.Current.Server.MapPath("/Upload/Word/word.png"), FileMode.Open, FileAccess.Read))
  3874. {
  3875. //图片的文件流 图片类型 图片名称 设置的宽度以及高度
  3876. xwpfRun2.AddPicture(picData, (int)PictureType.PNG, "word.png", NPOI.Util.Units.ToEMU(100), NPOI.Util.Units.ToEMU(100));
  3877. }
  3878. using (MemoryStream ms = new MemoryStream())
  3879. {
  3880. document.Write(ms);
  3881. return ms.GetBuffer();
  3882. }
  3883. }
  3884. catch(Exception ex)
  3885. {
  3886. return null;
  3887. }
  3888. }
  3889. /// <summary>
  3890. ///
  3891. /// </summary>
  3892. /// <param name="document"></param>
  3893. /// <param name="isbold"></param>
  3894. /// <param name="content"></param>
  3895. /// <returns></returns>
  3896. private NPOI.XWPF.UserModel.XWPFParagraph GetParagraph(NPOI.XWPF.UserModel.XWPFTable table,bool isbold,string content, bool iscenter=true)
  3897. {
  3898. var para = new CT_P();
  3899. //设置单元格文本对齐
  3900. para.AddNewPPr().AddNewTextAlignment();
  3901. NPOI.XWPF.UserModel.XWPFParagraph paragraph = new NPOI.XWPF.UserModel.XWPFParagraph(para,table.Body);
  3902. paragraph.VerticalAlignment = NPOI.XWPF.UserModel.TextAlignment.CENTER;
  3903. if (iscenter)
  3904. {
  3905. paragraph.Alignment = NPOI.XWPF.UserModel.ParagraphAlignment.CENTER;
  3906. }
  3907. else
  3908. {
  3909. paragraph.Alignment = NPOI.XWPF.UserModel.ParagraphAlignment.LEFT;
  3910. }
  3911. NPOI.XWPF.UserModel.XWPFRun xwpfRun = paragraph.CreateRun();//创建段落文本对象
  3912. xwpfRun.IsBold = isbold;//文字加粗
  3913. xwpfRun.SetText(content);//填充内容
  3914. xwpfRun.FontSize = 9;//设置文字大小
  3915. xwpfRun.SetFontFamily("宋体", NPOI.XWPF.UserModel.FontCharRange.None); //设置标题样式如:(微软雅黑,隶书,楷体)根据自己的需求而定
  3916. return paragraph;
  3917. }
  3918. /// <summary>
  3919. /// 插入图片
  3920. /// </summary>
  3921. /// <param name="table"></param>
  3922. /// <returns></returns>
  3923. private NPOI.XWPF.UserModel.XWPFParagraph GetParagraphImg(NPOI.XWPF.UserModel.XWPFTable table, bool isbold, string content, bool iscenter = true)
  3924. {
  3925. var para = new CT_P();
  3926. //设置单元格文本对齐
  3927. para.AddNewPPr().AddNewTextAlignment();
  3928. NPOI.XWPF.UserModel.XWPFParagraph paragraph = new NPOI.XWPF.UserModel.XWPFParagraph(para, table.Body);
  3929. paragraph.VerticalAlignment = NPOI.XWPF.UserModel.TextAlignment.CENTER;
  3930. if (iscenter)
  3931. {
  3932. paragraph.Alignment = NPOI.XWPF.UserModel.ParagraphAlignment.CENTER;
  3933. }
  3934. else
  3935. {
  3936. paragraph.Alignment = NPOI.XWPF.UserModel.ParagraphAlignment.LEFT;
  3937. }
  3938. NPOI.XWPF.UserModel.XWPFRun xwpfRun = paragraph.CreateRun();//创建段落文本对象
  3939. xwpfRun.IsBold = isbold;//文字加粗
  3940. xwpfRun.SetText(content);//填充内容
  3941. xwpfRun.FontSize = 9;//设置文字大小
  3942. xwpfRun.SetFontFamily("宋体", NPOI.XWPF.UserModel.FontCharRange.None); //设置标题样式如:(微软雅黑,隶书,楷体)根据自己的需求而定
  3943. using (FileStream picData = new FileStream(HttpContext.Current.Server.MapPath("/Upload/Word/word.png"), FileMode.Open, FileAccess.Read))
  3944. {
  3945. //图片的文件流 图片类型 图片名称 设置的宽度以及高度
  3946. xwpfRun.AddPicture(picData, (int)PictureType.PNG, "word.png", NPOI.Util.Units.ToEMU(100), NPOI.Util.Units.ToEMU(100));
  3947. }
  3948. return paragraph;
  3949. }
  3950. }
  3951. }