人民医院API

NPOIHelper.cs 69KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800
  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 Newtonsoft.Json;
  10. using NPOI.HSSF.UserModel;
  11. using NPOI.OpenXmlFormats.Wordprocessing;
  12. using NPOI.SS.UserModel;
  13. using NPOI.SS.Util;
  14. using NPOI.XSSF.UserModel;
  15. namespace RMYY_CallCenter_Api.Utility
  16. {
  17. public class NPOIHelper
  18. {
  19. /// <summary>
  20. /// 弹出下载框导出excel
  21. /// </summary>
  22. /// <param name="Name"></param>
  23. /// <param name="dt"></param>
  24. /// <returns></returns>
  25. public string ExportToExcel2(string Name, DataTable dt, string[] cols = null)
  26. {
  27. try
  28. {
  29. //if (dt.Rows.Count > 0)
  30. //{
  31. HSSFWorkbook workbook = new HSSFWorkbook();
  32. ISheet sheet = workbook.CreateSheet(Name);
  33. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  34. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  35. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  36. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  37. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  38. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  39. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  40. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  41. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  42. //字体
  43. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  44. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  45. headerfont.FontHeightInPoints = 12;
  46. HeadercellStyle.SetFont(headerfont);
  47. //用column name 作为列名
  48. int icolIndex = 0;
  49. IRow headerRow = sheet.CreateRow(0);
  50. if (cols == null || (cols != null && cols.Length == 0))
  51. {
  52. foreach (DataColumn dc in dt.Columns)
  53. {
  54. ICell cell = headerRow.CreateCell(icolIndex);
  55. cell.SetCellValue(dc.ColumnName);
  56. cell.CellStyle = HeadercellStyle;
  57. icolIndex++;
  58. }
  59. }
  60. else
  61. {
  62. foreach (string dc in cols)
  63. {
  64. ICell cell = headerRow.CreateCell(icolIndex);
  65. cell.SetCellValue(dc);
  66. cell.CellStyle = HeadercellStyle;
  67. icolIndex++;
  68. }
  69. }
  70. ICellStyle cellStyle = workbook.CreateCellStyle();
  71. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  72. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  73. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  74. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  75. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  76. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  77. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  78. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  79. cellStyle.SetFont(cellfont);
  80. //建立内容行
  81. int iRowIndex = 0;
  82. foreach (DataRow dr in dt.Rows)
  83. {
  84. int iCellIndex = 0;
  85. IRow irow = sheet.CreateRow(iRowIndex + 1);
  86. for (int i = 0; i < dt.Columns.Count; i++)
  87. {
  88. string strsj = string.Empty;
  89. if (dr[i] != null)
  90. {
  91. strsj = dr[i].ToString();
  92. }
  93. ICell cell = irow.CreateCell(iCellIndex);
  94. cell.SetCellValue(strsj);
  95. cell.CellStyle = cellStyle;
  96. iCellIndex++;
  97. }
  98. iRowIndex++;
  99. }
  100. //自适应列宽度
  101. for (int i = 0; i < icolIndex; i++)
  102. {
  103. sheet.AutoSizeColumn(i);
  104. }
  105. using (MemoryStream ms = new MemoryStream())
  106. {
  107. workbook.Write(ms);
  108. HttpContext curContext = HttpContext.Current;
  109. // 设置编码和附件格式
  110. curContext.Response.ContentType = "application/vnd.ms-excel";
  111. curContext.Response.ContentEncoding = Encoding.UTF8;
  112. curContext.Response.Charset = "";
  113. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  114. curContext.Response.AppendHeader("Content-Disposition",
  115. "attachment;filename=" + HttpUtility.UrlEncode(Name + "_导出文件_" + DateTime.Now.Ticks + ".xls", Encoding.UTF8));
  116. curContext.Response.BinaryWrite(ms.GetBuffer());
  117. workbook = null;
  118. ms.Close();
  119. ms.Dispose();
  120. curContext.Response.End();
  121. }
  122. //}
  123. return "";
  124. }
  125. catch
  126. {
  127. return "导出失败!";
  128. }
  129. }
  130. /// <summary>
  131. /// 弹出下载框导出excel
  132. /// </summary>
  133. /// <param name="Name"></param>
  134. /// <param name="dt"></param>
  135. /// <returns></returns>
  136. public string ExportToExcel(string Name, DataTable dt, string[] cols = null, int issort = 0)
  137. {
  138. try
  139. {
  140. //if (dt.Rows.Count > 0)
  141. //{
  142. HSSFWorkbook workbook = new HSSFWorkbook();
  143. ISheet sheet = workbook.CreateSheet(Name);
  144. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  145. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  146. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  147. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  148. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  149. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  150. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  151. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  152. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  153. //字体
  154. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  155. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  156. headerfont.FontHeightInPoints = 12;
  157. HeadercellStyle.SetFont(headerfont);
  158. //用column name 作为列名
  159. int icolIndex = 0;
  160. IRow headerRow = sheet.CreateRow(0);
  161. if (issort == 1)
  162. {
  163. ICell cell = headerRow.CreateCell(icolIndex);
  164. cell.SetCellValue("序号");
  165. cell.CellStyle = HeadercellStyle;
  166. icolIndex++;
  167. }
  168. if (cols == null || (cols != null && cols.Length == 0))
  169. {
  170. foreach (DataColumn dc in dt.Columns)
  171. {
  172. ICell cell = headerRow.CreateCell(icolIndex);
  173. cell.SetCellValue(dc.ColumnName);
  174. cell.CellStyle = HeadercellStyle;
  175. icolIndex++;
  176. }
  177. }
  178. else
  179. {
  180. foreach (string dc in cols)
  181. {
  182. ICell cell = headerRow.CreateCell(icolIndex);
  183. cell.SetCellValue(dc);
  184. cell.CellStyle = HeadercellStyle;
  185. icolIndex++;
  186. }
  187. }
  188. ICellStyle cellStyle = workbook.CreateCellStyle();
  189. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  190. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  191. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  192. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  193. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  194. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  195. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  196. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  197. cellStyle.SetFont(cellfont);
  198. //建立内容行
  199. int iRowIndex = 0;
  200. foreach (DataRow dr in dt.Rows)
  201. {
  202. int iCellIndex = 0;
  203. IRow irow = sheet.CreateRow(iRowIndex + 1);
  204. if (issort == 1)
  205. {
  206. ICell cell = irow.CreateCell(iCellIndex);
  207. cell.SetCellValue(iRowIndex + 1);
  208. cell.CellStyle = cellStyle;
  209. iCellIndex++;
  210. }
  211. for (int i = 0; i < dt.Columns.Count; i++)
  212. {
  213. string strsj = string.Empty;
  214. if (dr[i] != null)
  215. {
  216. strsj = dr[i].ToString();
  217. }
  218. ICell cell = irow.CreateCell(iCellIndex);
  219. cell.SetCellValue(strsj);
  220. cell.CellStyle = cellStyle;
  221. iCellIndex++;
  222. }
  223. iRowIndex++;
  224. }
  225. //自适应列宽度
  226. for (int i = 0; i < icolIndex; i++)
  227. {
  228. sheet.AutoSizeColumn(i);
  229. }
  230. using (MemoryStream ms = new MemoryStream())
  231. {
  232. workbook.Write(ms);
  233. HttpContext curContext = HttpContext.Current;
  234. // 设置编码和附件格式
  235. curContext.Response.ContentType = "application/vnd.ms-excel"; //指定返回的是一个不能被客户端读取的流,必须被下载
  236. curContext.Response.ContentEncoding = Encoding.UTF8;
  237. curContext.Response.Charset = "";
  238. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  239. curContext.Response.AppendHeader("Content-Disposition",
  240. "attachment;filename=" + HttpUtility.UrlEncode(Name + "_导出文件_" + DateTime.Now.Ticks + ".xls", Encoding.UTF8));
  241. curContext.Response.BinaryWrite(ms.GetBuffer());
  242. workbook = null;
  243. ms.Close();
  244. ms.Dispose();
  245. curContext.Response.End();
  246. }
  247. //}
  248. return "";
  249. }
  250. catch
  251. {
  252. return "导出失败!";
  253. }
  254. }
  255. /// <summary>
  256. /// 话务工作报表
  257. /// </summary>
  258. /// <param name="Name"></param>
  259. /// <param name="dt"></param>
  260. /// <returns></returns>
  261. public string CallReport(string Name, DataTable dt)
  262. {
  263. try
  264. {
  265. HSSFWorkbook workbook = new HSSFWorkbook();
  266. ISheet sheet = workbook.CreateSheet("话务工作报表");
  267. ICellStyle cellStyle = workbook.CreateCellStyle();
  268. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  269. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  270. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  271. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  272. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  273. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  274. cellStyle.VerticalAlignment = VerticalAlignment.Center;
  275. cellStyle.Alignment = HorizontalAlignment.Center;
  276. cellStyle.SetFont(cellfont);
  277. ICellStyle cellStylebt = workbook.CreateCellStyle();
  278. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  279. cellfontbt.Boldweight = (short)FontBoldWeight.Normal;
  280. cellStylebt.SetFont(cellfontbt);
  281. cellfontbt.FontHeightInPoints = 10;
  282. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  283. cellStylebt.Alignment = HorizontalAlignment.Center;
  284. cellStylebt.WrapText = true;
  285. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  286. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  287. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  288. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  289. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  290. HeadercellStyle.VerticalAlignment = VerticalAlignment.Center;
  291. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  292. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  293. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  294. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  295. //字体
  296. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  297. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  298. headerfont.FontHeightInPoints = 12;
  299. HeadercellStyle.SetFont(headerfont);
  300. IRow irow1 = sheet.CreateRow(0);
  301. ICell cell1 = irow1.CreateCell(0);
  302. cell1.SetCellValue("坐席");
  303. cell1.CellStyle = HeadercellStyle;
  304. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 0, 0));
  305. ICell cell2 = irow1.CreateCell(1);
  306. cell2.SetCellValue("电话总量");
  307. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 1, 1));
  308. cell2.CellStyle = HeadercellStyle;
  309. ICell cell3 = irow1.CreateCell(2);
  310. cell3.SetCellValue("呼入");
  311. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 2, 9));
  312. cell3.CellStyle = HeadercellStyle;
  313. ICell cell4 = irow1.CreateCell(10);
  314. cell4.SetCellValue("呼出");
  315. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 10, 13));
  316. cell4.CellStyle = HeadercellStyle;
  317. ICell cell5 = irow1.CreateCell(14);
  318. cell5.SetCellValue("转接");
  319. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 14, 17));
  320. cell5.CellStyle = HeadercellStyle;
  321. ICell cel8 = irow1.CreateCell(17);
  322. cel8.CellStyle = HeadercellStyle;
  323. IRow irow2 = sheet.CreateRow(1);
  324. string[] cols = {"总量","已接通","未接听","呼损率","接通率",
  325. "平均振铃时长(秒)","通话时长(秒)" ,"平均通话时长(秒)",
  326. "总量","已接通","未接听","接通率","总量","已接通","未接听","接通率" };
  327. int icolIndex = 0;
  328. ICell cel6 = irow2.CreateCell(0);
  329. cel6.CellStyle = HeadercellStyle;
  330. ICell cel7 = irow2.CreateCell(1);
  331. cel7.CellStyle = HeadercellStyle;
  332. foreach (string dc in cols)
  333. {
  334. ICell cell = irow2.CreateCell(icolIndex+2);
  335. cell.SetCellValue(dc);
  336. cell.CellStyle = HeadercellStyle;
  337. icolIndex++;
  338. }
  339. int iRowIndex = 1;
  340. foreach (DataRow dr in dt.Rows)
  341. {
  342. int iCellIndex = 0;
  343. IRow irow = sheet.CreateRow(iRowIndex + 1);
  344. for (int i = 0; i < dt.Columns.Count; i++)
  345. {
  346. string strsj = string.Empty;
  347. if (dr[i] != null)
  348. {
  349. strsj = dr[i].ToString();
  350. }
  351. ICell cell = irow.CreateCell(iCellIndex);
  352. cell.SetCellValue(strsj);
  353. cell.CellStyle = cellStyle;
  354. iCellIndex++;
  355. }
  356. iRowIndex++;
  357. }
  358. //自适应列宽度
  359. for (int i = 0; i < 17; i++)
  360. {
  361. if(i >=7&&i <=9)
  362. sheet.AutoSizeColumn(i);
  363. else
  364. sheet.SetColumnWidth(i, 10 * 256);
  365. }
  366. using (MemoryStream ms = new MemoryStream())
  367. {
  368. workbook.Write(ms);
  369. HttpContext curContext = HttpContext.Current;
  370. // 设置编码和附件格式
  371. curContext.Response.ContentType = "application/vnd.ms-excel";
  372. curContext.Response.ContentEncoding = Encoding.UTF8;
  373. curContext.Response.Charset = "";
  374. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  375. curContext.Response.AppendHeader("Content-Disposition",
  376. "attachment;filename=" + HttpUtility.UrlEncode("话务工作报表" + ".xls", Encoding.UTF8));
  377. curContext.Response.BinaryWrite(ms.GetBuffer());
  378. workbook = null;
  379. ms.Close();
  380. ms.Dispose();
  381. curContext.Response.End();
  382. }
  383. return "";
  384. }
  385. catch
  386. {
  387. return "导出失败!";
  388. }
  389. }
  390. /// <summary>
  391. /// </summary>
  392. /// <param name="Name"></param>
  393. /// <param name="dt"></param>
  394. /// <returns></returns>
  395. public string CarMaintenance(string Name, DataTable dt, bool type = true)
  396. {
  397. try
  398. {
  399. HSSFWorkbook workbook = new HSSFWorkbook();
  400. string message = "车辆调度科室维修情况";
  401. if (!type)
  402. message = "车辆调度工勤维修情况";
  403. ISheet sheet = workbook.CreateSheet(message);
  404. ICellStyle cellStyle = workbook.CreateCellStyle();
  405. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  406. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  407. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  408. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  409. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  410. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  411. cellStyle.VerticalAlignment = VerticalAlignment.Center;
  412. cellStyle.Alignment = HorizontalAlignment.Center;
  413. cellStyle.SetFont(cellfont);
  414. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  415. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  416. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  417. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  418. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  419. HeadercellStyle.VerticalAlignment = VerticalAlignment.Center;
  420. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  421. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  422. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  423. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  424. //字体
  425. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  426. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  427. headerfont.FontHeightInPoints = 12;
  428. HeadercellStyle.SetFont(headerfont);
  429. IRow irow1 = sheet.CreateRow(0);
  430. IRow irow3 = sheet.CreateRow(1);
  431. IRow irow2 = sheet.CreateRow(2);
  432. for (int i = 0; i < 9; i++)
  433. {
  434. ICell cel16 = irow1.CreateCell(i);
  435. cel16.CellStyle = HeadercellStyle;
  436. ICell cel17 = irow2.CreateCell(i);
  437. cel17.CellStyle = HeadercellStyle;
  438. ICell cel18 = irow3.CreateCell(i);
  439. cel18.CellStyle = HeadercellStyle;
  440. }
  441. ICell cell1 = irow1.CreateCell(0);
  442. sheet.AddMergedRegion(new CellRangeAddress(0, 2, 0, 0));
  443. cell1.CellStyle = HeadercellStyle;
  444. cell1.SetCellValue("院区");
  445. ICell cell2 = irow1.CreateCell(1);
  446. sheet.AddMergedRegion(new CellRangeAddress(0, 2, 1, 1));
  447. cell2.SetCellValue("科室");
  448. cell2.CellStyle = HeadercellStyle;
  449. ICell cell3 = irow1.CreateCell(2);
  450. sheet.AddMergedRegion(new CellRangeAddress(0, 2, 2, 2));
  451. if (!type)
  452. cell3.SetCellValue("工勤");
  453. else
  454. cell3.SetCellValue("工单类型");
  455. cell3.CellStyle = HeadercellStyle;
  456. ICell cell4 = irow1.CreateCell(3);
  457. cell4.SetCellValue("工单数量");
  458. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 3, 6));
  459. cell4.CellStyle = HeadercellStyle;
  460. ICell cell5 = irow1.CreateCell(7);
  461. cell5.SetCellValue("不满意量");
  462. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 7, 8));
  463. cell5.CellStyle = HeadercellStyle;
  464. ICell cell9 = irow3.CreateCell(3);
  465. sheet.AddMergedRegion(new CellRangeAddress(1, 2, 3, 3));
  466. cell9.SetCellValue("接单量");
  467. cell9.CellStyle = HeadercellStyle;
  468. ICell cell11 = irow3.CreateCell(4);
  469. cell11.SetCellValue("完成情况");
  470. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 4, 6));
  471. cell11.CellStyle = HeadercellStyle;
  472. // ICell cel8 = irow1.CreateCell(17);
  473. // cel8.CellStyle = HeadercellStyle;
  474. string[] cols = {"完成量","未完成量","完成率",
  475. "数量","不满意率" };
  476. int icolIndex = 0;
  477. //ICell cel6 = irow2.CreateCell(0);
  478. //cel6.CellStyle = HeadercellStyle;
  479. //ICell cel7 = irow2.CreateCell(1);
  480. //cel7.CellStyle = HeadercellStyle;
  481. foreach (string dc in cols)
  482. {
  483. ICell cell = irow2.CreateCell(icolIndex + 4);
  484. cell.SetCellValue(dc);
  485. cell.CellStyle = HeadercellStyle;
  486. icolIndex++;
  487. }
  488. int iRowIndex = 2;
  489. foreach (DataRow dr in dt.Rows)
  490. {
  491. int iCellIndex = 0;
  492. IRow irow = sheet.CreateRow(iRowIndex + 1);
  493. for (int i = 0; i < dt.Columns.Count; i++)
  494. {
  495. string strsj = string.Empty;
  496. if (dr[i] != null)
  497. {
  498. strsj = dr[i].ToString();
  499. }
  500. ICell cell = irow.CreateCell(iCellIndex);
  501. cell.SetCellValue(strsj);
  502. cell.CellStyle = cellStyle;
  503. iCellIndex++;
  504. }
  505. iRowIndex++;
  506. }
  507. int start = 3;
  508. iRowIndex = 2;
  509. int end = 3;
  510. string woname = dt.Rows[0]["woname"].ToString();
  511. for (int i = 0; i < dt.Rows.Count; i++)
  512. {
  513. if (woname == dt.Rows[i]["woname"].ToString())
  514. {
  515. end++;
  516. }
  517. else
  518. {
  519. woname = dt.Rows[i]["woname"].ToString();
  520. sheet.AddMergedRegion(new CellRangeAddress(start, end - 1, 0, 0));
  521. start = end;
  522. end++;
  523. }
  524. }
  525. //自适应列宽度
  526. for (int i = 0; i < 9; i++)
  527. {
  528. if (i == 0)
  529. {
  530. sheet.SetColumnWidth(i, 25 * 256);
  531. }
  532. else if (i < 3)
  533. sheet.AutoSizeColumn(i);
  534. else
  535. sheet.SetColumnWidth(i, 10 * 256);
  536. }
  537. using (MemoryStream ms = new MemoryStream())
  538. {
  539. workbook.Write(ms);
  540. HttpContext curContext = HttpContext.Current;
  541. // 设置编码和附件格式
  542. curContext.Response.ContentType = "application/vnd.ms-excel";
  543. curContext.Response.ContentEncoding = Encoding.UTF8;
  544. curContext.Response.Charset = "";
  545. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  546. curContext.Response.AppendHeader("Content-Disposition",
  547. "attachment;filename=" + HttpUtility.UrlEncode(message + ".xls", Encoding.UTF8));
  548. curContext.Response.BinaryWrite(ms.GetBuffer());
  549. workbook = null;
  550. ms.Close();
  551. ms.Dispose();
  552. curContext.Response.End();
  553. }
  554. return "";
  555. }
  556. catch
  557. {
  558. return "导出失败!";
  559. }
  560. }
  561. /// <summary>
  562. /// </summary>
  563. /// <param name="Name"></param>
  564. /// <param name="dt"></param>
  565. /// <returns></returns>
  566. public string Maintenance(string Name, DataTable dt,bool type=true )
  567. {
  568. try
  569. {
  570. HSSFWorkbook workbook = new HSSFWorkbook();
  571. string message = "科室维修情况";
  572. if (!type)
  573. message = "工勤维修情况";
  574. ISheet sheet = workbook.CreateSheet(message);
  575. ICellStyle cellStyle = workbook.CreateCellStyle();
  576. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  577. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  578. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  579. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  580. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  581. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  582. cellStyle.VerticalAlignment = VerticalAlignment.Center;
  583. cellStyle.Alignment = HorizontalAlignment.Center;
  584. cellStyle.SetFont(cellfont);
  585. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  586. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  587. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  588. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  589. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  590. HeadercellStyle.VerticalAlignment = VerticalAlignment.Center;
  591. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  592. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  593. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  594. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  595. //字体
  596. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  597. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  598. headerfont.FontHeightInPoints = 12;
  599. HeadercellStyle.SetFont(headerfont);
  600. IRow irow1 = sheet.CreateRow(0);
  601. IRow irow3 = sheet.CreateRow(1);
  602. IRow irow2 = sheet.CreateRow(2);
  603. for (int i = 0; i < 15; i++)
  604. {
  605. ICell cel16 = irow1.CreateCell(i);
  606. cel16.CellStyle = HeadercellStyle;
  607. ICell cel17 = irow2.CreateCell(i);
  608. cel17.CellStyle = HeadercellStyle;
  609. ICell cel18 = irow3.CreateCell(i);
  610. cel18.CellStyle = HeadercellStyle;
  611. }
  612. ICell cell1 = irow1.CreateCell(0);
  613. sheet.AddMergedRegion(new CellRangeAddress(0, 2, 0, 0));
  614. cell1.CellStyle = HeadercellStyle;
  615. cell1.SetCellValue("院区");
  616. ICell cell2 = irow1.CreateCell(1);
  617. sheet.AddMergedRegion(new CellRangeAddress(0, 2, 1, 1));
  618. cell2.SetCellValue("科室");
  619. cell2.CellStyle = HeadercellStyle;
  620. ICell cell3 = irow1.CreateCell(2);
  621. sheet.AddMergedRegion(new CellRangeAddress(0, 2, 2, 2));
  622. if (!type )
  623. cell3.SetCellValue("工勤");
  624. else
  625. cell3.SetCellValue("工单类型");
  626. cell3.CellStyle = HeadercellStyle;
  627. ICell cell4 = irow1.CreateCell(3);
  628. cell4.SetCellValue("工单数量");
  629. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 3, 8));
  630. cell4.CellStyle = HeadercellStyle;
  631. ICell cell5 = irow1.CreateCell(9);
  632. cell5.SetCellValue("不满意量");
  633. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 9, 14));
  634. cell5.CellStyle = HeadercellStyle;
  635. ICell cell9 = irow3.CreateCell(3);
  636. sheet.AddMergedRegion(new CellRangeAddress(1, 2, 3, 3));
  637. cell9.SetCellValue("接单量");
  638. cell9.CellStyle = HeadercellStyle;
  639. ICell cell11 = irow3.CreateCell(4);
  640. cell11.SetCellValue("完成情况");
  641. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 4, 6));
  642. cell11.CellStyle = HeadercellStyle;
  643. ICell cell12 = irow3.CreateCell(7);
  644. cell12.SetCellValue("超时情况");
  645. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 7, 8));
  646. cell12.CellStyle = HeadercellStyle;
  647. ICell cell13 = irow3.CreateCell(9);
  648. cell13.SetCellValue("质量");
  649. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 9, 10));
  650. cell13.CellStyle = HeadercellStyle;
  651. ICell cell14 = irow3.CreateCell(11);
  652. cell14.SetCellValue("态度");
  653. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 11, 12));
  654. cell14.CellStyle = HeadercellStyle;
  655. ICell cell15 = irow3.CreateCell(13);
  656. cell15.SetCellValue("效率");
  657. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 13, 14));
  658. cell15.CellStyle = HeadercellStyle;
  659. // ICell cel8 = irow1.CreateCell(17);
  660. // cel8.CellStyle = HeadercellStyle;
  661. string[] cols = {"完成量","未完成量","完成率","数量","超时率",
  662. "数量","不满意率", "数量","不满意率", "数量","不满意率" };
  663. int icolIndex = 0;
  664. //ICell cel6 = irow2.CreateCell(0);
  665. //cel6.CellStyle = HeadercellStyle;
  666. //ICell cel7 = irow2.CreateCell(1);
  667. //cel7.CellStyle = HeadercellStyle;
  668. foreach (string dc in cols)
  669. {
  670. ICell cell = irow2.CreateCell(icolIndex + 4);
  671. cell.SetCellValue(dc);
  672. cell.CellStyle = HeadercellStyle;
  673. icolIndex++;
  674. }
  675. int iRowIndex = 2;
  676. foreach (DataRow dr in dt.Rows)
  677. {
  678. int iCellIndex = 0;
  679. IRow irow = sheet.CreateRow(iRowIndex + 1);
  680. for (int i = 0; i < dt.Columns.Count; i++)
  681. {
  682. string strsj = string.Empty;
  683. if (dr[i] != null)
  684. {
  685. strsj = dr[i].ToString();
  686. }
  687. ICell cell = irow.CreateCell(iCellIndex);
  688. cell.SetCellValue(strsj);
  689. cell.CellStyle = cellStyle;
  690. iCellIndex++;
  691. }
  692. iRowIndex++;
  693. }
  694. int start = 3;
  695. iRowIndex = 2;
  696. int end = 3;
  697. string woname = dt.Rows[0]["woname"].ToString();
  698. for (int i = 0; i < dt.Rows.Count; i++)
  699. {
  700. if (woname ==dt.Rows [i ]["woname"].ToString())
  701. {
  702. end++;
  703. }
  704. else
  705. {
  706. woname = dt.Rows[i]["woname"].ToString();
  707. sheet.AddMergedRegion(new CellRangeAddress(start, end - 1, 0, 0));
  708. start = end;
  709. end++;
  710. }
  711. }
  712. //自适应列宽度
  713. for (int i = 0; i < 15; i++)
  714. { if (i ==0)
  715. {
  716. sheet.SetColumnWidth(i, 25 * 256);
  717. }
  718. else if (i < 3)
  719. sheet.AutoSizeColumn(i);
  720. else
  721. sheet.SetColumnWidth(i, 10 * 256);
  722. }
  723. using (MemoryStream ms = new MemoryStream())
  724. {
  725. workbook.Write(ms);
  726. HttpContext curContext = HttpContext.Current;
  727. // 设置编码和附件格式
  728. curContext.Response.ContentType = "application/vnd.ms-excel";
  729. curContext.Response.ContentEncoding = Encoding.UTF8;
  730. curContext.Response.Charset = "";
  731. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  732. curContext.Response.AppendHeader("Content-Disposition",
  733. "attachment;filename=" + HttpUtility.UrlEncode(message + ".xls", Encoding.UTF8));
  734. curContext.Response.BinaryWrite(ms.GetBuffer());
  735. workbook = null;
  736. ms.Close();
  737. ms.Dispose();
  738. curContext.Response.End();
  739. }
  740. return "";
  741. }
  742. catch
  743. {
  744. return "导出失败!";
  745. }
  746. }
  747. /// <summary>
  748. /// 导入excel转换为datatable
  749. /// </summary>
  750. /// <param name="upfile"></param>
  751. /// <param name="headrow"></param>
  752. /// <returns></returns>
  753. public DataTable ExcelToTable(HttpPostedFileBase upfile, int headrow)
  754. {
  755. DataTable dt = new DataTable();
  756. IWorkbook workbook = null;
  757. Stream stream = upfile.InputStream;
  758. string suffix = upfile.FileName.Substring(upfile.FileName.LastIndexOf(".") + 1).ToLower();
  759. if (suffix == "xlsx") // 2007版本
  760. {
  761. workbook = new XSSFWorkbook(stream);
  762. }
  763. else if (suffix == "xls") // 2003版本
  764. {
  765. workbook = new HSSFWorkbook(stream);
  766. }
  767. //获取excel的第一个sheet
  768. ISheet sheet = workbook.GetSheetAt(0);
  769. //获取sheet的第一行
  770. IRow headerRow = sheet.GetRow(headrow);
  771. //一行最后一个方格的编号 即总的列数
  772. int cellCount = headerRow.LastCellNum;
  773. //最后一列的标号 即总的行数
  774. int rowCount = sheet.LastRowNum;
  775. //列名
  776. for (int i = 0; i < cellCount; i++)
  777. {
  778. dt.Columns.Add(headerRow.GetCell(i).ToString());
  779. }
  780. for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++)
  781. {
  782. DataRow dr = dt.NewRow();
  783. IRow row = sheet.GetRow(i);
  784. for (int j = row.FirstCellNum; j < cellCount; j++)
  785. {
  786. if (row.GetCell(j) != null)
  787. {
  788. dr[j] = row.GetCell(j).ToString();
  789. }
  790. }
  791. dt.Rows.Add(dr);
  792. }
  793. sheet = null;
  794. workbook = null;
  795. return dt;
  796. }
  797. /// <summary>
  798. /// riyuezhou报导出
  799. /// </summary>
  800. /// <param name="ds"></param>
  801. /// <returns></returns>
  802. public string SimpleExportToExcel(DataTable zongshudt, DataTable ywzxdt, DataTable zhdddt, DataTable gzbxdt, DataTable cldddt)
  803. {
  804. try
  805. {
  806. HSSFWorkbook workbook = new HSSFWorkbook();
  807. ISheet sheet = workbook.CreateSheet("Sheet1");
  808. ICellStyle cellStyle = workbook.CreateCellStyle();
  809. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  810. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  811. cellStyle.SetFont(cellfont);
  812. ICellStyle cellStylebt = workbook.CreateCellStyle();
  813. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  814. //cellfontbt.Boldweight = (short)FontBoldWeight.Bold;
  815. cellfontbt.FontHeightInPoints = 14;
  816. cellStylebt.SetFont(cellfontbt);
  817. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  818. cellStylebt.Alignment = HorizontalAlignment.Center;
  819. int rowindex = 0;
  820. #region 业务咨询情况
  821. IRow irow1 = sheet.CreateRow(rowindex);
  822. ICell cell1 = irow1.CreateCell(0);
  823. cell1.SetCellValue(zongshudt.Rows[0]["typename"].ToString() + " ( " + zongshudt.Rows[0]["zscount"].ToString() + ")");
  824. cell1.CellStyle = cellStylebt;
  825. //起始行,结束行,起始列,结束列 从0 开始
  826. // 合并第一行 6个单元格
  827. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 5));
  828. rowindex++;
  829. double xx = ywzxdt.Rows.Count / 3;
  830. int allrow = Convert.ToInt32(Math.Ceiling(xx));
  831. int zzrowindex = 0;
  832. for (int i = 0; i < allrow; i++)
  833. {
  834. IRow irow2 = sheet.CreateRow(rowindex);
  835. for (int j = 0; j < 3; j++)
  836. {
  837. if (zzrowindex < ywzxdt.Rows.Count)
  838. {
  839. ICell cell2 = irow2.CreateCell(j * 2);
  840. cell2.SetCellValue(ywzxdt.Rows[zzrowindex]["F_Name"].ToString());
  841. cell2.CellStyle = cellStylebt;
  842. ICell cell3 = irow2.CreateCell(j * 2 + 1);
  843. cell3.SetCellValue(ywzxdt.Rows[zzrowindex]["COUNT"].ToString());
  844. cell3.CellStyle = cellStylebt;
  845. zzrowindex = zzrowindex + 1;
  846. }
  847. }
  848. rowindex = rowindex + 1;
  849. }
  850. #endregion
  851. #region 故障报修情况
  852. IRow irowgz = sheet.CreateRow(rowindex);
  853. ICell cell1gz = irowgz.CreateCell(0);
  854. cell1gz.SetCellValue(zongshudt.Rows[2]["typename"].ToString() + " ( " + zongshudt.Rows[2]["zscount"].ToString() + ")");
  855. cell1gz.CellStyle = cellStylebt;
  856. //起始行,结束行,起始列,结束列 从0 开始
  857. // 合并第一行 6个单元格
  858. sheet.AddMergedRegion(new CellRangeAddress(rowindex, rowindex, 0, 5));
  859. rowindex++;
  860. double bxxx = gzbxdt.Rows.Count / 3.0;
  861. int bxallrow = Convert.ToInt32(Math.Ceiling(bxxx));
  862. int gzrowindex = 0;
  863. for (int i = 0; i < bxallrow; i++)
  864. {
  865. IRow irow2 = sheet.CreateRow(rowindex);
  866. for (int j = 0; j < 3; j++)
  867. {
  868. if (gzrowindex < gzbxdt.Rows.Count)
  869. {
  870. ICell cell2 = irow2.CreateCell(j * 2);
  871. cell2.SetCellValue(gzbxdt.Rows[gzrowindex]["F_Name"].ToString());
  872. cell2.CellStyle = cellStylebt;
  873. ICell cell3 = irow2.CreateCell(j * 2 + 1);
  874. cell3.SetCellValue(gzbxdt.Rows[gzrowindex]["COUNT"].ToString());
  875. cell3.CellStyle = cellStylebt;
  876. gzrowindex = gzrowindex + 1;
  877. }
  878. }
  879. rowindex = rowindex + 1;
  880. }
  881. #endregion
  882. #region 综合调度情况
  883. IRow irowdd = sheet.CreateRow(rowindex);
  884. ICell cell1gdd = irowdd.CreateCell(0);
  885. cell1gdd.SetCellValue(zongshudt.Rows[1]["typename"].ToString() + " ( " + zongshudt.Rows[1]["zscount"].ToString() + ")");
  886. cell1gdd.CellStyle = cellStylebt;
  887. //起始行,结束行,起始列,结束列 从0 开始
  888. // 合并第一行 6个单元格
  889. sheet.AddMergedRegion(new CellRangeAddress(rowindex, rowindex, 0, 5));
  890. rowindex++;
  891. double zhddxx = zhdddt.Rows.Count / 3.0;
  892. int zhddallrow = Convert.ToInt32(Math.Ceiling(zhddxx));
  893. int zhddrowindex = 0;
  894. for (int i = 0; i < zhddallrow; i++)
  895. {
  896. IRow irow2 = sheet.CreateRow(rowindex);
  897. for (int j = 0; j < 3; j++)
  898. {
  899. if (zhddrowindex < zhdddt.Rows.Count)
  900. {
  901. ICell cell2 = irow2.CreateCell(j * 2);
  902. cell2.SetCellValue(zhdddt.Rows[zhddrowindex]["F_Name"].ToString());
  903. cell2.CellStyle = cellStylebt;
  904. ICell cell3 = irow2.CreateCell(j * 2 + 1);
  905. cell3.SetCellValue(zhdddt.Rows[zhddrowindex]["COUNT"].ToString());
  906. cell3.CellStyle = cellStylebt;
  907. zhddrowindex = zhddrowindex + 1;
  908. }
  909. }
  910. rowindex = rowindex + 1;
  911. }
  912. #endregion
  913. #region 车辆调度情况
  914. IRow irowcldd = sheet.CreateRow(rowindex);
  915. ICell cell1cldd = irowcldd.CreateCell(0);
  916. cell1cldd.SetCellValue(zongshudt.Rows[3]["typename"].ToString() + " ( " + zongshudt.Rows[3]["zscount"].ToString() + ")");
  917. cell1cldd.CellStyle = cellStylebt;
  918. //起始行,结束行,起始列,结束列 从0 开始
  919. // 合并第一行 6个单元格
  920. sheet.AddMergedRegion(new CellRangeAddress(rowindex, rowindex, 0, 5));
  921. rowindex++;
  922. double clddxx = cldddt.Rows.Count / 3.0;
  923. int clddallrow = Convert.ToInt32(Math.Ceiling(clddxx));
  924. int clddrowindex = 0;
  925. for (int i = 0; i < clddallrow; i++)
  926. {
  927. IRow irow2 = sheet.CreateRow(rowindex);
  928. for (int j = 0; j < 3; j++)
  929. {
  930. if (clddrowindex < cldddt.Rows.Count)
  931. {
  932. ICell cell2 = irow2.CreateCell(j * 2);
  933. cell2.SetCellValue(cldddt.Rows[clddrowindex]["F_Name"].ToString());
  934. cell2.CellStyle = cellStylebt;
  935. ICell cell3 = irow2.CreateCell(j * 2 + 1);
  936. cell3.SetCellValue(cldddt.Rows[clddrowindex]["COUNT"].ToString());
  937. cell3.CellStyle = cellStylebt;
  938. clddrowindex = clddrowindex + 1;
  939. }
  940. }
  941. rowindex = rowindex + 1;
  942. }
  943. #endregion
  944. //自适应列宽度
  945. for (int i = 0; i < 8; i++)
  946. {
  947. sheet.SetColumnWidth(i,30*256);
  948. }
  949. using (MemoryStream ms = new MemoryStream())
  950. {
  951. workbook.Write(ms);
  952. HttpContext curContext = HttpContext.Current;
  953. // 设置编码和附件格式
  954. curContext.Response.ContentType = "application/vnd.ms-excel";
  955. curContext.Response.ContentEncoding = Encoding.UTF8;
  956. curContext.Response.Charset = "";
  957. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  958. curContext.Response.AppendHeader("Content-Disposition",
  959. "attachment;filename=" + HttpUtility.UrlEncode("日月周简报_导出文件_" + DateTime.Now.Ticks + ".xls", Encoding.UTF8));
  960. curContext.Response.BinaryWrite(ms.GetBuffer());
  961. workbook = null;
  962. ms.Close();
  963. ms.Dispose();
  964. curContext.Response.End();
  965. }
  966. return "";
  967. }
  968. catch
  969. {
  970. return "导出失败!";
  971. }
  972. }
  973. public class T_InvolvedEquip_RepairProcess
  974. {
  975. public string F_EquipName { set; get; }
  976. public string F_EquipNumber { set; get; }
  977. public string F_EquipAmount { set; get; }
  978. public string F_EquipPrice { set; get; }
  979. public string Allprice { set; get; }
  980. }
  981. public string AboutEquipExportToExcel(string Name, DataTable dt, string[] cols = null)
  982. {
  983. try
  984. {
  985. //if (dt.Rows.Count > 0)
  986. //{
  987. HSSFWorkbook workbook = new HSSFWorkbook();
  988. ISheet sheet = workbook.CreateSheet(Name);
  989. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  990. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  991. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  992. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  993. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  994. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  995. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  996. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  997. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  998. //字体
  999. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  1000. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  1001. headerfont.FontHeightInPoints = 12;
  1002. HeadercellStyle.SetFont(headerfont);
  1003. //用column name 作为列名
  1004. int icolIndex = 0;
  1005. IRow headerRow = sheet.CreateRow(0);
  1006. //if (issort == 1)
  1007. //{
  1008. // ICell cell = headerRow.CreateCell(icolIndex);
  1009. // cell.SetCellValue("序号");
  1010. // cell.CellStyle = HeadercellStyle;
  1011. // icolIndex++;
  1012. //}
  1013. if (cols == null || (cols != null && cols.Length == 0))
  1014. {
  1015. foreach (DataColumn dc in dt.Columns)
  1016. {
  1017. ICell cell = headerRow.CreateCell(icolIndex);
  1018. cell.SetCellValue(dc.ColumnName);
  1019. cell.CellStyle = HeadercellStyle;
  1020. icolIndex++;
  1021. }
  1022. }
  1023. else
  1024. {
  1025. foreach (string dc in cols)
  1026. {
  1027. ICell cell = headerRow.CreateCell(icolIndex);
  1028. cell.SetCellValue(dc);
  1029. cell.CellStyle = HeadercellStyle;
  1030. icolIndex++;
  1031. }
  1032. }
  1033. ICellStyle cellStyle = workbook.CreateCellStyle();
  1034. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  1035. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  1036. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1037. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1038. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1039. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1040. cellStyle.VerticalAlignment = VerticalAlignment.Center;
  1041. cellStyle.Alignment = HorizontalAlignment.Center;
  1042. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  1043. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  1044. cellStyle.SetFont(cellfont);
  1045. //建立内容行
  1046. int iRowIndex = 0;
  1047. int Equipscount = 1;
  1048. foreach (DataRow dr in dt.Rows)
  1049. {
  1050. if (!dr["Equips"].IsNullOrEmpty())
  1051. {
  1052. List < T_InvolvedEquip_RepairProcess > dt1=
  1053. JsonConvert.DeserializeObject<List<T_InvolvedEquip_RepairProcess>>(dr["Equips"].ToJson());
  1054. Equipscount = dt1.Count;
  1055. if (dt1.Count>0)
  1056. {
  1057. int allcount = dt1.Count;
  1058. int first = iRowIndex;
  1059. for (int j = 0; j < dt1.Count; j++)
  1060. {
  1061. IRow irow3 = sheet.CreateRow(iRowIndex + 1);
  1062. for (int i = 0; i < cols.Count(); i++)
  1063. {
  1064. if (i < 6&&j ==0)
  1065. {
  1066. ICell cell = irow3.CreateCell(i);
  1067. cell.SetCellValue(dr[i].ToString());
  1068. cell.CellStyle = cellStyle;
  1069. sheet.AddMergedRegion(new CellRangeAddress(iRowIndex + 1, iRowIndex + allcount, i, i));
  1070. }
  1071. else if (i > 10 && j == 0)
  1072. {
  1073. ICell cell = irow3.CreateCell(i);
  1074. cell.SetCellValue(dr[i - 5].ToString());
  1075. cell.CellStyle = cellStyle;
  1076. sheet.AddMergedRegion(new CellRangeAddress(iRowIndex + 1, iRowIndex + allcount, i, i));
  1077. }
  1078. else
  1079. {
  1080. ICell cell = irow3.CreateCell(i);
  1081. if (i > 5 & i < 11)
  1082. {
  1083. switch (i)
  1084. {
  1085. case 6:
  1086. cell.SetCellValue(dt1[j].F_EquipName);
  1087. break;
  1088. case 7:
  1089. cell.SetCellValue(dt1[j].F_EquipNumber);
  1090. break;
  1091. case 8:
  1092. cell.SetCellValue(dt1[j].F_EquipAmount);
  1093. break;
  1094. case 9:
  1095. cell.SetCellValue(dt1[j].F_EquipPrice);
  1096. break;
  1097. case 10:
  1098. cell.SetCellValue(dt1[j].Allprice);
  1099. break;
  1100. }
  1101. }
  1102. cell.CellStyle = cellStyle;
  1103. }
  1104. }
  1105. iRowIndex++;
  1106. }
  1107. }
  1108. }
  1109. else
  1110. {
  1111. IRow irow = sheet.CreateRow(iRowIndex + 1);
  1112. for (int i = 0; i < cols.Count(); i++)
  1113. {
  1114. string strsj = string.Empty;
  1115. if (i<6)
  1116. {
  1117. ICell cell = irow.CreateCell(i );
  1118. cell.SetCellValue(dr[i].ToString());
  1119. cell.CellStyle = cellStyle;
  1120. }
  1121. else if (i >5&i <11)
  1122. {
  1123. ICell cell = irow.CreateCell(i );
  1124. cell.SetCellValue("");
  1125. cell.CellStyle = cellStyle;
  1126. }
  1127. else
  1128. {
  1129. ICell cell = irow.CreateCell(i );
  1130. cell.SetCellValue(dr[i-5].ToString());
  1131. cell.CellStyle = cellStyle;
  1132. }
  1133. }
  1134. iRowIndex++;
  1135. }
  1136. }
  1137. //自适应列宽度
  1138. for (int i = 0; i < icolIndex; i++)
  1139. {
  1140. sheet.AutoSizeColumn(i);
  1141. }
  1142. using (MemoryStream ms = new MemoryStream())
  1143. {
  1144. workbook.Write(ms);
  1145. HttpContext curContext = HttpContext.Current;
  1146. // 设置编码和附件格式
  1147. curContext.Response.ContentType = "application/vnd.ms-excel"; //指定返回的是一个不能被客户端读取的流,必须被下载
  1148. curContext.Response.ContentEncoding = Encoding.UTF8;
  1149. curContext.Response.Charset = "";
  1150. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  1151. curContext.Response.AppendHeader("Content-Disposition",
  1152. "attachment;filename=" + HttpUtility.UrlEncode(Name + "_导出文件_" + DateTime.Now.Ticks + ".xls", Encoding.UTF8));
  1153. curContext.Response.BinaryWrite(ms.GetBuffer());
  1154. workbook = null;
  1155. ms.Close();
  1156. ms.Dispose();
  1157. curContext.Response.End();
  1158. }
  1159. //}
  1160. return "";
  1161. }
  1162. catch(Exception e)
  1163. {
  1164. return e .Message ;
  1165. }
  1166. }
  1167. public string DeptRepairExportToExcel(string Name, DataTable dt, string[] cols = null, int issort = 0)
  1168. {
  1169. try
  1170. {
  1171. //if (dt.Rows.Count > 0)
  1172. //{
  1173. HSSFWorkbook workbook = new HSSFWorkbook();
  1174. ISheet sheet = workbook.CreateSheet(Name);
  1175. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  1176. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1177. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1178. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1179. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1180. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  1181. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  1182. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  1183. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  1184. HeadercellStyle.VerticalAlignment = VerticalAlignment.Center;
  1185. //字体
  1186. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  1187. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  1188. headerfont.FontHeightInPoints = 12;
  1189. HeadercellStyle.SetFont(headerfont);
  1190. //用column name 作为列名
  1191. int icolIndex = 0;
  1192. IRow headerRow = sheet.CreateRow(0);
  1193. if (issort == 1)
  1194. {
  1195. ICell cell = headerRow.CreateCell(icolIndex);
  1196. cell.SetCellValue("序号");
  1197. cell.CellStyle = HeadercellStyle;
  1198. icolIndex++;
  1199. }
  1200. if (cols == null || (cols != null && cols.Length == 0))
  1201. {
  1202. //ICell cell = headerRow.CreateCell(icolIndex);
  1203. //cell.SetCellValue("院区");
  1204. //cell.CellStyle = HeadercellStyle;
  1205. //icolIndex++;
  1206. //ICell cell2 = headerRow.CreateCell(icolIndex);
  1207. //cell2.SetCellValue("科室");
  1208. //cell2.CellStyle = HeadercellStyle;
  1209. //icolIndex++;
  1210. //ICell cell3 = headerRow.CreateCell(icolIndex);
  1211. //cell3.SetCellValue("工单数量");
  1212. //cell3.CellStyle = HeadercellStyle;
  1213. //icolIndex++;
  1214. IRow headerRow1 = sheet.CreateRow(1);
  1215. IRow headerRow2 = sheet.CreateRow(2);
  1216. IRow headerRow3 = sheet.CreateRow(3);
  1217. ICell cell01 = headerRow.CreateCell(icolIndex);
  1218. cell01.SetCellValue("院区");
  1219. cell01.CellStyle = HeadercellStyle;
  1220. icolIndex++;
  1221. ICell cell02 = headerRow.CreateCell(icolIndex);
  1222. cell02.SetCellValue("科室");
  1223. cell02.CellStyle = HeadercellStyle;
  1224. icolIndex++;
  1225. ICell cell03 = headerRow.CreateCell(icolIndex);
  1226. cell03.SetCellValue("工单数量");
  1227. cell03.CellStyle = HeadercellStyle;
  1228. icolIndex++;
  1229. for (int createicol = icolIndex; createicol < 11; createicol++)
  1230. {
  1231. ICell cell = headerRow.CreateCell(icolIndex);
  1232. cell.SetCellValue("");
  1233. cell.CellStyle = HeadercellStyle;
  1234. icolIndex++;
  1235. }
  1236. ICell c11 = headerRow1.CreateCell(0);
  1237. c11.SetCellValue("");
  1238. c11.CellStyle = HeadercellStyle;
  1239. ICell c12 = headerRow1.CreateCell(1);
  1240. c12.SetCellValue("");
  1241. c12.CellStyle = HeadercellStyle;
  1242. ICell c13 = headerRow1.CreateCell(2);
  1243. c13.SetCellValue("故障报修");
  1244. c13.CellStyle = HeadercellStyle;
  1245. ICell c14 = headerRow1.CreateCell(3);
  1246. c14.SetCellValue("");
  1247. c14.CellStyle = HeadercellStyle;
  1248. ICell c15 = headerRow1.CreateCell(4);
  1249. c15.SetCellValue("");
  1250. c15.CellStyle = HeadercellStyle;
  1251. ICell c16 = headerRow1.CreateCell(5);
  1252. c16.SetCellValue("车辆调度");
  1253. c16.CellStyle = HeadercellStyle;
  1254. ICell c17 = headerRow1.CreateCell(6);
  1255. c17.SetCellValue("");
  1256. c17.CellStyle = HeadercellStyle;
  1257. ICell c19 = headerRow1.CreateCell(7);
  1258. c19.SetCellValue("");
  1259. c19.CellStyle = HeadercellStyle;
  1260. ICell c110 = headerRow1.CreateCell(8);
  1261. c110.SetCellValue("综合调度");
  1262. c110.CellStyle = HeadercellStyle;
  1263. ICell c111 = headerRow1.CreateCell(9);
  1264. c111.SetCellValue("");
  1265. c111.CellStyle = HeadercellStyle;
  1266. ICell c112 = headerRow1.CreateCell(10);
  1267. c112.SetCellValue("");
  1268. c112.CellStyle = HeadercellStyle;
  1269. ICell cc11 = headerRow2.CreateCell(0);
  1270. cc11.SetCellValue("");
  1271. cc11.CellStyle = HeadercellStyle;
  1272. ICell cc12 = headerRow2.CreateCell(1);
  1273. cc12.SetCellValue("");
  1274. cc12.CellStyle = HeadercellStyle;
  1275. ICell cc13 = headerRow2.CreateCell(2);
  1276. cc13.SetCellValue("总数");
  1277. cc13.CellStyle = HeadercellStyle;
  1278. ICell cc14 = headerRow2.CreateCell(3);
  1279. cc14.SetCellValue("已完成");
  1280. cc14.CellStyle = HeadercellStyle;
  1281. ICell cc15 = headerRow2.CreateCell(4);
  1282. cc15.SetCellValue("未完成");
  1283. cc15.CellStyle = HeadercellStyle;
  1284. ICell cc16 = headerRow2.CreateCell(5);
  1285. cc16.SetCellValue("总数");
  1286. cc16.CellStyle = HeadercellStyle;
  1287. ICell cc17 = headerRow2.CreateCell(6);
  1288. cc17.SetCellValue("已完成");
  1289. cc17.CellStyle = HeadercellStyle;
  1290. ICell cc19 = headerRow2.CreateCell(7);
  1291. cc19.SetCellValue("未完成");
  1292. cc19.CellStyle = HeadercellStyle;
  1293. ICell cc110 = headerRow2.CreateCell(8);
  1294. cc110.SetCellValue("总数");
  1295. cc110.CellStyle = HeadercellStyle;
  1296. ICell cc111 = headerRow2.CreateCell(9);
  1297. cc111.SetCellValue("已完成");
  1298. cc111.CellStyle = HeadercellStyle;
  1299. ICell cc112 = headerRow2.CreateCell(10);
  1300. cc112.SetCellValue("未完成");
  1301. cc112.CellStyle = HeadercellStyle;
  1302. //起始行,结束行,起始列,结束列
  1303. sheet.AddMergedRegion(new CellRangeAddress(0, 2, 0, 0));
  1304. sheet.AddMergedRegion(new CellRangeAddress(0, 2, 1, 1));
  1305. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 2, 10));
  1306. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 2, 4));
  1307. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 2, 4));
  1308. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 5, 7));
  1309. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 8, 10));
  1310. }
  1311. ICellStyle cellStyle = workbook.CreateCellStyle();
  1312. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  1313. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  1314. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1315. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1316. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1317. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1318. cellStyle.VerticalAlignment = VerticalAlignment.Center;
  1319. cellStyle.Alignment = HorizontalAlignment.Center;
  1320. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  1321. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  1322. cellStyle.SetFont(cellfont);
  1323. //建立内容行
  1324. int iRowIndex = 0;
  1325. foreach (DataRow dr in dt.Rows)
  1326. {
  1327. int iCellIndex = 0;
  1328. IRow irow = sheet.CreateRow(iRowIndex + 3);
  1329. if (issort == 1)
  1330. {
  1331. ICell cell = irow.CreateCell(iCellIndex);
  1332. cell.SetCellValue(iRowIndex + 1);
  1333. cell.CellStyle = cellStyle;
  1334. iCellIndex++;
  1335. }
  1336. for (int i = 0; i < dt.Columns.Count; i++)
  1337. {
  1338. string strsj = string.Empty;
  1339. if (dr[i] != null)
  1340. {
  1341. strsj = dr[i].ToString();
  1342. }
  1343. ICell cell = irow.CreateCell(iCellIndex);
  1344. cell.SetCellValue(strsj);
  1345. cell.CellStyle = cellStyle;
  1346. iCellIndex++;
  1347. }
  1348. iRowIndex++;
  1349. }
  1350. int start = 3;
  1351. iRowIndex = 2;
  1352. int end = 3;
  1353. string woname = dt.Rows[0]["yqname"].ToString();
  1354. for (int i = 0; i < dt.Rows.Count; i++)
  1355. {
  1356. if (woname == dt.Rows[i]["yqname"].ToString())
  1357. {
  1358. end++;
  1359. }
  1360. else
  1361. {
  1362. woname = dt.Rows[i]["yqname"].ToString();
  1363. sheet.AddMergedRegion(new CellRangeAddress(start, end - 1, 0, 0));
  1364. start = end;
  1365. end++;
  1366. }
  1367. }
  1368. //自适应列宽度
  1369. for (int i = 0; i < icolIndex; i++)
  1370. {
  1371. sheet.AutoSizeColumn(i);
  1372. }
  1373. using (MemoryStream ms = new MemoryStream())
  1374. {
  1375. workbook.Write(ms);
  1376. HttpContext curContext = HttpContext.Current;
  1377. // 设置编码和附件格式
  1378. curContext.Response.ContentType = "application/vnd.ms-excel"; //指定返回的是一个不能被客户端读取的流,必须被下载
  1379. curContext.Response.ContentEncoding = Encoding.UTF8;
  1380. curContext.Response.Charset = "";
  1381. curContext.Response.AppendHeader("Access-Control-Expose-Headers", "Content-Disposition");
  1382. curContext.Response.AppendHeader("Content-Disposition",
  1383. "attachment;filename=" + HttpUtility.UrlEncode(Name + "_导出文件_" + DateTime.Now.Ticks + ".xls", Encoding.UTF8));
  1384. curContext.Response.BinaryWrite(ms.GetBuffer());
  1385. workbook = null;
  1386. ms.Close();
  1387. ms.Dispose();
  1388. curContext.Response.End();
  1389. }
  1390. //}
  1391. return "";
  1392. }
  1393. catch
  1394. {
  1395. return "导出失败!";
  1396. }
  1397. }
  1398. }
  1399. }