comExports13.aspx.cs.exclude 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. using System.Data;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. using System.Web;
  11. using System.Web.UI;
  12. using System.Web.UI.WebControls;
  13. using Newtonsoft.Json;
  14. using Newtonsoft.Json.Linq;
  15. using NPOI.SS.UserModel;
  16. using SiteCore;
  17. using SiteCore.Handler;
  18. using Utils;
  19. public partial class Reports_comExports13 : ReportBase
  20. {
  21. static int exp_page_size = 9999;
  22. protected void Page_Load(object sender, EventArgs e)
  23. {
  24. if (CurrentUser == null)
  25. {
  26. Response.Write("账户过期无法导出,请重新登录!");
  27. return;
  28. }
  29. CommonHelper.PageExpires();
  30. String type = Request["type"];
  31. try
  32. {
  33. switch (type)
  34. {
  35. case "bexcel":
  36. {
  37. string extName = Request["extname"];
  38. string exData = GetPostString("hExData");
  39. string exSdate = GetPostString("hExSdate");
  40. string exEdate = GetPostString("hExEdate");
  41. //数据来源
  42. List<Dictionary<string, object>> data = Utils.Serialization.JSON.ToDictionaryList(exData);
  43. String json = Request["columns"];
  44. List<Dictionary<string, object>> columns = Utils.Serialization.JSON.ToDictionaryList(json);
  45. ExportExcel(exSdate,exEdate,extName, columns, data);
  46. break;
  47. }
  48. case "excel2":
  49. {
  50. string extName = Request["extname"];
  51. string exSdate = GetPostString("hExSdate");
  52. string exEdate = GetPostString("hExEdate");
  53. //数据来源
  54. List<Dictionary<string, object>> data = SearchData();
  55. String json = Request["columns"];
  56. List<Dictionary<string, object>> columns = Utils.Serialization.JSON.ToDictionaryList(json);
  57. ExportNopi(exSdate, exEdate, extName,ref columns, ref data);
  58. //ExportExcel(exSdate, exEdate, extName, columns, data);
  59. break;
  60. }
  61. case "excel":
  62. {
  63. string extName = Request["extname"];
  64. string exSdate = GetPostString("hExSdate");
  65. string exEdate = GetPostString("hExEdate");
  66. //数据来源
  67. DataTable data = GetDtData();// SearchData();
  68. int c = data.Rows.Count;
  69. String json = Request["columns"];
  70. List<Dictionary<string, object>> columns = Utils.Serialization.JSON.ToDictionaryList(json);
  71. ExportNopi(exSdate, exEdate, extName, ref columns, data);
  72. if (data != null)
  73. {
  74. data.Clear();
  75. data.Dispose();
  76. }
  77. if (c > 1000) GC.Collect();
  78. //ExportExcel(exSdate, exEdate, extName, columns, data);
  79. break;
  80. }
  81. default:
  82. break;
  83. }
  84. }
  85. catch(Exception ex)
  86. {
  87. //Response.Write("无法导出" + ex.Message);
  88. //Response.End();
  89. }
  90. }
  91. private void enableQueryStringWriting()
  92. {
  93. var qs = Request.QueryString;
  94. var queryString = (NameValueCollection)Request.GetType().GetField("_queryString",BindingFlags.NonPublic | BindingFlags.Instance).GetValue(Request);
  95. PropertyInfo readOnlyInfo = queryString.GetType().GetProperty("IsReadOnly",BindingFlags.NonPublic | BindingFlags.Instance);
  96. readOnlyInfo.SetValue(queryString, false, null);
  97. }
  98. public void setQueryString(string parms)
  99. {
  100. //Request.QueryString.Add(;
  101. enableQueryStringWriting();
  102. NameValueCollection nvc = new NameValueCollection();
  103. parms = parms.TrimStart('&');
  104. string[] pArr = parms.Split('&');
  105. string[] tArr;
  106. foreach(string p in pArr)
  107. {
  108. tArr = p.Split('=');
  109. //nvc.Add(tArr[0], tArr[1]);
  110. Request.QueryString.Set(tArr[0], tArr[1]);
  111. }
  112. }
  113. public DataTable GetDtData()
  114. {
  115. string url=GetPostString("url");
  116. url = HttpUtility.UrlDecode(url);
  117. int idx = url.IndexOf("t=");
  118. url = url.Substring(idx + 2);//.Replace("t=", "");
  119. string mname = "";
  120. if (url.IndexOf("&") != -1)
  121. {
  122. int ui = url.IndexOf("&");
  123. string parms = url.Substring(ui+1);
  124. setQueryString(parms);
  125. mname = url.Substring(0,ui).Trim();
  126. }
  127. else mname = url.Trim();
  128. sync sentity = new sync();
  129. Type t = sentity.GetType();
  130. MethodInfo mi = t.GetMethod(mname); //根据字符串名称获得对应的方法
  131. t.GetField("ex_psize").SetValue(sentity, 2000000);
  132. mi.Invoke(sentity, null);
  133. //DataTable dt=(DataTable)t.GetField("ex_dtable").GetValue(sentity);
  134. return sentity.ex_dtable;
  135. //sentity.ex_psize = 2014091;// exp_page_size;
  136. //sentity.get_erp_orderlist();
  137. //return sentity.ex_dtable;
  138. }
  139. public void ExportNopi(string exSdate, string exEdate, string extName,ref List<Dictionary<string, object>> columns,ref List<Dictionary<string, object>> data)
  140. {
  141. excelName = "导出文件";
  142. createExcel();
  143. int ops = 60000;
  144. int dc = data.Count;
  145. int cpg = (dc - 1) / ops + 1;
  146. if (cpg == 0) cpg = 1;
  147. int ps = 0;
  148. List<Dictionary<string, object>> curLst = null;
  149. List<string> cols = new List<string>();
  150. for (int i = 0; i < cpg; i++)
  151. {
  152. if (i + 1 == cpg)//最后一页
  153. {
  154. if (i == 0) ops = dc;
  155. ps = dc - ops * (cpg - 1);
  156. }
  157. else
  158. {
  159. ps = ops;
  160. }
  161. if (i > 0)
  162. {
  163. hssfworkbook.CreateSheet((i+1).ToString());
  164. }
  165. iSheet = hssfworkbook.GetSheetAt(i);
  166. iSheet.DefaultRowHeight = 22 * 20;
  167. iSheet.CreateRow(0);
  168. IRow row = iSheet.CreateRow(1);
  169. int c = 0;
  170. row.Height = 22 * 20;
  171. //row.RowStyle = GetExHeadStyle();
  172. foreach (Dictionary<string, object> dic in columns)
  173. {
  174. if (i == 0)cols.Add(dic["field"].ToString());
  175. createHeaderCell(row, c, dic["header"].ToString());
  176. iSheet.AutoSizeColumn(c);
  177. iSheet.SetColumnWidth(c, Convert.ToInt32(dic["width"].ToString().Replace("px", "")) * 65);
  178. //row.CreateCell(c).SetCellValue(dic["header"].ToString());
  179. c++;
  180. }
  181. int r = 2;
  182. int bc = 0;
  183. iSheet.CreateRow(r);
  184. insertRow(iSheet, r, ps, iSheet.GetRow(1));
  185. curLst = data.Skip(i * ops).Take(ps).ToList();
  186. foreach (Dictionary<string, object> da_dic in curLst)
  187. {
  188. bc = 0;
  189. foreach (string col in cols)
  190. {
  191. createItemCell(iSheet.GetRow(r), bc, getFS(col, da_dic[col]));
  192. //setStringCell(r, bc, );
  193. bc++;
  194. }
  195. r++;
  196. }
  197. curLst.Clear();
  198. }
  199. columns.Clear();
  200. data.Clear();
  201. DownLoadFile_Ex();
  202. }
  203. public void ExportNopi(string exSdate, string exEdate, string extName, ref List<Dictionary<string, object>> columns, DataTable data)
  204. {
  205. excelName = "导出文件";
  206. createExcel();
  207. int ops = 60000;
  208. int dc = data.Rows.Count;
  209. int cpg = (dc - 1) / ops + 1;
  210. if (cpg == 0) cpg = 1;
  211. int ps = 0;
  212. DataTable curDt = null;
  213. List<string> cols = new List<string>();
  214. for (int i = 0; i < cpg; i++)
  215. {
  216. curDt = new DataTable();
  217. if (i + 1 == cpg)//最后一页
  218. {
  219. if (i == 0) ops = dc;
  220. ps = dc - ops * (cpg - 1);
  221. }
  222. else
  223. {
  224. ps = ops;
  225. }
  226. if (i > 0)
  227. {
  228. hssfworkbook.CreateSheet((i + 1).ToString());
  229. }
  230. iSheet = hssfworkbook.GetSheetAt(i);
  231. iSheet.DefaultRowHeight = 22 * 20;
  232. iSheet.CreateRow(0);
  233. IRow row = iSheet.CreateRow(1);
  234. int c = 0;
  235. row.Height = 22 * 20;
  236. //row.RowStyle = GetExHeadStyle();
  237. foreach (Dictionary<string, object> dic in columns)
  238. {
  239. if (i == 0) cols.Add(dic["field"].ToString());
  240. createHeaderCell(row, c, dic["header"].ToString());
  241. iSheet.AutoSizeColumn(c);
  242. iSheet.SetColumnWidth(c, Convert.ToInt32(dic["width"].ToString().Replace("px", "")) * 65);
  243. //row.CreateCell(c).SetCellValue(dic["header"].ToString());
  244. c++;
  245. }
  246. int r = 2;
  247. int bc = 0;
  248. iSheet.CreateRow(r);
  249. insertRow(iSheet, r, ps, iSheet.GetRow(1));
  250. curDt= data.AsEnumerable().Skip(i * ops).Take(ps).CopyToDataTable();
  251. foreach (DataRow da_dic in curDt.Rows)
  252. {
  253. bc = 0;
  254. foreach (string col in cols)
  255. {
  256. createItemCell(iSheet.GetRow(r), bc, getFS(col, da_dic[col]));
  257. //setStringCell(r, bc, );
  258. bc++;
  259. }
  260. r++;
  261. }
  262. }
  263. if (curDt != null)
  264. {
  265. curDt.Clear();
  266. curDt.Dispose();
  267. }
  268. columns.Clear();
  269. data.Clear();
  270. data.Dispose();
  271. DownLoadFile_Ex();
  272. }
  273. public void ExportNopiXLSX(string exSdate, string exEdate, string extName, ref List<Dictionary<string, object>> columns, DataTable data)
  274. {
  275. excelName = "导出文件";
  276. createExcel();
  277. int i = 0;
  278. hssfworkbook.CreateSheet(i.ToString());
  279. iSheet = hssfworkbook.GetSheetAt(i);
  280. iSheet.DefaultRowHeight = 22 * 20;
  281. iSheet.CreateRow(0);
  282. IRow row = iSheet.CreateRow(1);
  283. int c = 0;
  284. row.Height = 22 * 20;
  285. //row.RowStyle = GetExHeadStyle();
  286. List<string> cols = new List<string>();
  287. foreach (Dictionary<string, object> dic in columns)
  288. {
  289. if (i == 0) cols.Add(dic["field"].ToString());
  290. createHeaderCell(row, c, dic["header"].ToString());
  291. iSheet.AutoSizeColumn(c);
  292. iSheet.SetColumnWidth(c, Convert.ToInt32(dic["width"].ToString().Replace("px", "")) * 65);
  293. //row.CreateCell(c).SetCellValue(dic["header"].ToString());
  294. c++;
  295. }
  296. int r = 2;
  297. int bc = 0;
  298. int rc = data.Rows.Count;
  299. iSheet.CreateRow(r);
  300. insertRow(iSheet, r, rc, iSheet.GetRow(1));
  301. foreach (DataRow da_dic in data.Rows)
  302. {
  303. bc = 0;
  304. foreach (string col in cols)
  305. {
  306. createItemCell(iSheet.GetRow(r), bc, getFS(col, da_dic[col]));
  307. //setStringCell(r, bc, );
  308. bc++;
  309. }
  310. r++;
  311. }
  312. DownLoadFile_Ex();
  313. }
  314. public void DownLoadFile_Ex()
  315. {
  316. using (MemoryStream ms = new MemoryStream())
  317. {
  318. hssfworkbook.Write(ms);
  319. Response.Clear();
  320. Response.Buffer = true;
  321. Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
  322. Response.Expires = 0;
  323. Response.AddHeader("pragma", "no-cache");
  324. Response.AddHeader("cache-control", "private");
  325. Response.CacheControl = "no-cache";
  326. //Response.Charset = "GB2312";
  327. Response.Charset = "UTF-8";
  328. //Response.AppendHeader("Content-Disposition", "attachment;filename=" + excelName + ".xls");
  329. Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(excelName + ".xls", System.Text.Encoding.UTF8));
  330. Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
  331. Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
  332. ms.WriteTo(Response.OutputStream);
  333. Response.Flush();
  334. hssfworkbook = null;
  335. iSheet = null;
  336. }
  337. Response.End();
  338. //Response.Write(ExportTable(data, columns));
  339. }
  340. public List<Dictionary<string, object>> SearchData()
  341. {
  342. //查询条件
  343. /*string key = Request["key"];
  344. //字段排序
  345. String sortField = Request["sortField"];
  346. String sortOrder = Request["sortOrder"];
  347. */
  348. string url = Request["url"];
  349. url = HttpUtility.UrlDecode(url);
  350. List<string> parms = new List<string>();
  351. foreach (string key in Request.Form)
  352. {
  353. if (key == "url") continue;
  354. if (key == "columns") continue;
  355. if (key == "pageSize") continue;
  356. parms.Add(key + "=" + Request.Form[key]);
  357. }
  358. string fstr = string.Join("&", parms.ToArray());//.ToString();
  359. fstr = HttpUtility.UrlDecode(fstr);
  360. List<Dictionary<string, object>> lists = getComReportData(getUrl(url), fstr);
  361. //Newtonsoft.Json.JsonConvert.DeserializeObject<Hashtable>()
  362. //Hashtable result =
  363. return lists;// result["data"] as ArrayList;
  364. }
  365. public List<Dictionary<string, object>> getComReportData(string url, string parms)
  366. {
  367. HttpCookie lcCookie = HttpContext.Current.Request.Cookies[webConfig.CookieName];
  368. string name = HttpUtility.UrlDecode(lcCookie.Values["User"]);
  369. string ticket = lcCookie.Values["Ticket"];
  370. if (parms == "") parms = "pageSize=" + exp_page_size;
  371. else parms += "&pageSize=" + exp_page_size;
  372. parms += "&_expt=1";
  373. //parms = HttpUtility.UrlEncode(parms);
  374. HttpHelper http = new HttpHelper();
  375. HttpItem item = new HttpItem()
  376. {
  377. URL = url,
  378. Method = "POST",
  379. ContentType = "application/x-www-form-urlencoded; charset=UTF-8",
  380. Cookie = webConfig.CookieName + "=user=" + name + "&ticket=" + ticket,
  381. Postdata = parms
  382. };
  383. item.PostEncoding = Encoding.UTF8;
  384. HttpResult hresult = http.GetHtml(item);
  385. string html = hresult.Html;
  386. JObject jobj = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(html);
  387. if (jobj["total"] != null)
  388. {
  389. List<Dictionary<string, object>> lists_sec;
  390. List<Dictionary<string, object>> lists = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(jobj["data"].ToString());
  391. int pg = (Convert.ToInt32(jobj["total"]) - 1) / exp_page_size + 1; // exp_page_size;
  392. if (pg > 1)
  393. {
  394. for (int i = 2; i <= pg; i++)
  395. {
  396. if (i == 2) parms = parms.Replace("pageIndex=0", "pageIndex=1");
  397. else parms = parms.Replace("pageIndex=" + (i - 3), "pageIndex=" + (i - 1));
  398. item = new HttpItem()
  399. {
  400. URL = url,
  401. Method = "POST",
  402. ContentType = "application/x-www-form-urlencoded; charset=UTF-8",
  403. Cookie = webConfig.CookieName + "=user=" + name + "&ticket=" + ticket,
  404. Postdata = parms
  405. };
  406. hresult = http.GetHtml(item);
  407. html = hresult.Html;
  408. jobj = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(html);
  409. html = string.Empty;
  410. if (jobj["total"] != null)
  411. {
  412. lists_sec = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(jobj["data"].ToString());
  413. lists.AddRange(lists_sec);
  414. //lists_sec.Clear();
  415. }
  416. jobj = null;
  417. }
  418. }
  419. return lists;
  420. //Hashtable htable= JsonConvert.DeserializeObject<Hashtable>(jobj["data"].ToString());
  421. }
  422. return null;
  423. }
  424. public void ExportExcel(string exSdate, string exEdate, string extName, List<Dictionary<string, object>> columns, List<Dictionary<string, object>> data)
  425. {
  426. Response.Clear();
  427. Response.Buffer = true;
  428. Response.Charset = "GB2312";
  429. //Response.Charset = "UTF-8";
  430. Response.AppendHeader("Content-Disposition", "attachment;filename=" + extName + ".xls");
  431. Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
  432. Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
  433. EnableViewState = false;
  434. string _exdata= "";
  435. try
  436. {
  437. _exdata = ExportTable(exSdate, exEdate, extName, data, columns);
  438. }
  439. catch (Exception ex)
  440. {
  441. string ex2 = ex.Message;
  442. }
  443. byte[] array = Encoding.Default.GetBytes(_exdata);
  444. int dataBufferSize = 5 * 1024 * 1024;
  445. byte[] buffer = null;
  446. long hasRead = 0;
  447. using (MemoryStream stream = new MemoryStream(array))
  448. {
  449. long contentLength = stream.Length;
  450. int currentRead = 0;
  451. while (hasRead < contentLength)
  452. {
  453. buffer = new byte[dataBufferSize];
  454. currentRead = stream.Read(buffer, 0, dataBufferSize);
  455. Response.OutputStream.Write(buffer, 0, currentRead);
  456. Response.Flush();
  457. hasRead += currentRead;
  458. }
  459. }
  460. array = null;
  461. Response.End();
  462. }
  463. #region table
  464. public static string ExportTable(string exSdate, string exEdate, string extName, List<Dictionary<string, object>> data, List<Dictionary<string, object>> columns)
  465. {
  466. ArrayList columnsBottom = getColumnsBottom(columns);
  467. ArrayList columnsTable = getColumnsTable(columns);
  468. StringBuilder sb = new StringBuilder();
  469. //data = ds.DataSetName + "\n";
  470. //dg.Cells[10].Attributes.Add("style", "vnd.ms-excel.numberformat: @;");
  471. //data += tb.TableName + "\n";
  472. sb.AppendLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">");
  473. sb.AppendLine("<table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\" border=\"1\">");
  474. sb.AppendLine("<tr style=\"border-right:none;border-bottom:none\"><td style=\"height:30px;text-align:center;font-size:15.0pt;font-weight:700;border-right:none;border-bottom:none\" colspan=\"" + columnsBottom.Count + "\">" + extName + "</td></tr>");
  475. sb.AppendLine("<tr style=\"border-right:none;border-bottom:none\"><td style=\"height:25px;text-align:center;font-size:10.5pt;border-right:none;border-bottom:none\" colspan=\"" + columnsBottom.Count + "\">" + getTime(exSdate, exEdate) + "</td></tr>");
  476. //写出列名
  477. for (int i = 0; i < columnsTable.Count; i++)
  478. {
  479. ArrayList columnsRow = (ArrayList)columnsTable[i];
  480. sb.AppendLine("<tr style=\"font-weight: bold; white-space: nowrap;height:25px;\">");
  481. foreach (Dictionary<string, object> column in columnsRow)
  482. {
  483. if (!Convert.ToBoolean(column["visible"])) continue;
  484. if (column.ContainsKey("type"))
  485. sb.AppendLine("<td style=\"background-color:#ADD8E6\" > 序号</td>");
  486. if (column.ContainsKey("header"))
  487. sb.AppendLine("<td style=\"background-color:#ADD8E6\" " + (column.ContainsKey("colspan") ? "colspan='" + column["colspan"] + "'" : "") + " " + (column.ContainsKey("rowspan") ? "rowspan='" + column["rowspan"] + "'" : "") + ">" + column["header"] + "</td>");
  488. }
  489. sb.AppendLine("</tr>");
  490. }
  491. //写出数据
  492. int count = 0;
  493. string fv = "";
  494. foreach (Dictionary<string, object> row in data)
  495. {
  496. sb.Append("<tr>");
  497. foreach (Dictionary<string, object> column in columnsBottom)
  498. {
  499. if (!Convert.ToBoolean(column["visible"])) continue;
  500. Object value = "";
  501. if (column.ContainsKey("type"))
  502. {
  503. if (Convert.ToString(column["type"]) == "indexcolumn") value = count + 1;
  504. }
  505. else
  506. {
  507. if (column.ContainsKey("field") && row.ContainsKey(column["field"].ToString()))
  508. {
  509. //renderer
  510. value = row[column["field"].ToString()];
  511. fv = value.ToString();
  512. if (fv.Length == 19 && fv.Substring(10, 1) == "T")
  513. {
  514. value = fv.Replace("T", " ");
  515. }
  516. }
  517. else
  518. {
  519. value = "";
  520. }
  521. }
  522. if (column.ContainsKey("field"))
  523. {
  524. if (column["field"].ToString() == "OrderState")
  525. {
  526. value = getOrderStateStr(Convert.ToInt32(value));
  527. }
  528. else if (column["field"].ToString() == "cashstate")
  529. {
  530. value = getOrderCashStateStr(Convert.ToInt32(value));
  531. }
  532. }
  533. sb.AppendLine("<td style=\"height:25px;vnd.ms-excel.numberformat: @;\" >" + value + "</td>");//;vnd.ms-excel.numberformat: @;
  534. }
  535. sb.AppendLine("</tr>");
  536. count++;
  537. }
  538. sb.AppendLine("</table>");
  539. return sb.ToString();
  540. }
  541. public static ArrayList getColumnsBottom(List<Dictionary<string, object>> columns)
  542. {
  543. ArrayList columnsBottom = new ArrayList();
  544. for (int i = 0; i < columns.Count; i++)
  545. {
  546. Dictionary<string, object> column = (Dictionary<string, object>)columns[i];
  547. if (column.ContainsKey("columns"))// != null
  548. {
  549. List<object> childColumns = (List<object>)column["columns"];
  550. columnsBottom.AddRange(getColumnsBottomArray(childColumns));
  551. }
  552. else
  553. {
  554. columnsBottom.Add(column);
  555. }
  556. }
  557. return columnsBottom;
  558. }
  559. public static ArrayList getColumnsBottomArray(List<object> columns)
  560. {
  561. ArrayList columnsBottom = new ArrayList();
  562. for (int i = 0; i < columns.Count; i++)
  563. {
  564. Dictionary<string, object> column = (Dictionary<string, object>)columns[i];
  565. if (column.ContainsKey("columns"))// != null
  566. {
  567. List<object> childColumns = (List<object>)column["columns"];
  568. columnsBottom.AddRange(getColumnsBottomArray(childColumns));
  569. }
  570. else
  571. {
  572. columnsBottom.Add(column);
  573. }
  574. }
  575. return columnsBottom;
  576. }
  577. public static ArrayList getColumnsTable(List<Dictionary<string, object>> columns)
  578. {
  579. ArrayList table = new ArrayList();
  580. getColumnsRows(columns, 0, table);
  581. createTableSpan(table);
  582. return table;
  583. }
  584. public static void getColumnsRows(List<Dictionary<string, object>> columns, int level, ArrayList table)
  585. {
  586. ArrayList row = null;
  587. if (table.Count > level)
  588. {
  589. row = (ArrayList)table[level];
  590. }
  591. else
  592. {
  593. row = new ArrayList();
  594. table.Add(row);
  595. }
  596. for (int i = 0; i < columns.Count; i++)
  597. {
  598. Dictionary<string, object> column = (Dictionary<string, object>)columns[i];
  599. row.Add(column);
  600. if (column.ContainsKey("columns"))
  601. {
  602. List<object> childColumns = (List<object>)column["columns"];
  603. if (childColumns != null)
  604. {
  605. getColumnsRowsObject(childColumns, level + 1, table);
  606. }
  607. }
  608. }
  609. }
  610. public static void getColumnsRowsObject(List<object> columns, int level, ArrayList table)
  611. {
  612. ArrayList row = null;
  613. if (table.Count > level)
  614. {
  615. row = (ArrayList)table[level];
  616. }
  617. else
  618. {
  619. row = new ArrayList();
  620. table.Add(row);
  621. }
  622. for (int i = 0; i < columns.Count; i++)
  623. {
  624. Dictionary<string, object> column = (Dictionary<string, object>)columns[i];
  625. row.Add(column);
  626. if (column.ContainsKey("columns"))
  627. {
  628. List<object> childColumns = (List<object>)column["columns"];
  629. if (childColumns != null)
  630. {
  631. getColumnsRowsObject(childColumns, level + 1, table);
  632. }
  633. }
  634. }
  635. }
  636. public static void createTableSpan(ArrayList table)
  637. {
  638. for (int i = 0; i < table.Count; i++)
  639. {
  640. ArrayList row = (ArrayList)table[i]; //row
  641. for (int l = 0; l < row.Count; l++)
  642. {
  643. Dictionary<string, object> cell = (Dictionary<string,object>)row[l]; //column
  644. int colSpan = getColSpan(cell);
  645. cell["colspan"] = colSpan;
  646. if (colSpan > 1)
  647. {
  648. cell["rowspan"] = 1;
  649. }
  650. else
  651. {
  652. cell["rowspan"] = table.Count - i;
  653. }
  654. }
  655. }
  656. }
  657. public static int getColSpan(Dictionary<string, object> column)
  658. {
  659. int colSpan = 0;
  660. if (column.ContainsKey("columns"))
  661. {
  662. List<object> childColumns = (List<object>)column["columns"];
  663. if (childColumns != null)
  664. {
  665. for (int i = 0; i < childColumns.Count; i++)
  666. {
  667. Dictionary<string, object> child = (Dictionary<string, object>)childColumns[i];
  668. colSpan += getColSpan(child);
  669. }
  670. }
  671. }
  672. else
  673. {
  674. colSpan = 1;
  675. }
  676. return colSpan;
  677. }
  678. #endregion
  679. #region npoi
  680. public void createHeaderCell(IRow row, int cIdx, object v)
  681. {
  682. ICell cell = row.CreateCell(cIdx);
  683. cell.CellStyle = GetExHeadStyle();
  684. cell.SetCellValue(v.ToString());
  685. }
  686. ICellStyle ex_headStyle = null;
  687. private ICellStyle GetExHeadStyle()
  688. {
  689. if (ex_headStyle == null)
  690. {
  691. ex_headStyle = hssfworkbook.CreateCellStyle();
  692. IFont font = hssfworkbook.CreateFont();
  693. font.FontName = "宋体";
  694. font.FontHeightInPoints = 11;
  695. font.Boldweight = 700;
  696. ex_headStyle.SetFont(font);
  697. ex_headStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;
  698. ex_headStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.THIN;
  699. ex_headStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;
  700. ex_headStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN;
  701. ex_headStyle.Alignment = HorizontalAlignment.CENTER;
  702. ex_headStyle.VerticalAlignment = VerticalAlignment.CENTER;
  703. ex_headStyle.FillForegroundColor = IndexedColors.AQUA.Index;
  704. ex_headStyle.FillPattern = FillPatternType.SOLID_FOREGROUND;
  705. }
  706. return ex_headStyle;
  707. }
  708. public void createItemCell(IRow row, int cIdx, object v)
  709. {
  710. ICell cell = row.CreateCell(cIdx);
  711. cell.CellStyle = GetExItemStyle();
  712. cell.SetCellValue(v.ToString());
  713. }
  714. ICellStyle ex_itemStyle = null;
  715. private ICellStyle GetExItemStyle()
  716. {
  717. if (ex_itemStyle == null)
  718. {
  719. ex_itemStyle = hssfworkbook.CreateCellStyle();
  720. IFont font = hssfworkbook.CreateFont();
  721. font.FontName = "宋体";
  722. font.FontHeightInPoints = 11;
  723. ex_itemStyle.SetFont(font);
  724. ex_itemStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;
  725. ex_itemStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.THIN;
  726. ex_itemStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;
  727. ex_itemStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN;
  728. ex_itemStyle.Alignment = HorizontalAlignment.CENTER;
  729. ex_itemStyle.VerticalAlignment = VerticalAlignment.CENTER;
  730. //ex_itemStyle.FillForegroundColor = IndexedColors.AQUA.Index;
  731. //ex_itemStyle.FillPattern = FillPatternType.SOLID_FOREGROUND;
  732. }
  733. return ex_itemStyle;
  734. }
  735. #endregion
  736. #region private
  737. private string getFS(string col, object v)
  738. {
  739. if (v.ToString() == "") return "";
  740. switch (col.ToLower())
  741. {
  742. case "orderstate":
  743. {
  744. return getOrderStateStr(Convert.ToInt32(v));
  745. }
  746. case "cashstate":
  747. {
  748. return getOrderCashStateStr(Convert.ToInt32(v));
  749. }
  750. case "status":
  751. {
  752. return getTbStatusByState(Convert.ToString(v));
  753. }
  754. case "aftersalestate":
  755. {
  756. return getAfterSaleByState(Convert.ToInt32(v));
  757. }
  758. }
  759. return v.ToString();
  760. }
  761. private string getUrl(string url)
  762. {
  763. url = HttpUtility.UrlDecode(url);
  764. string host = Request.Url.Host;
  765. string port = Request.Url.Port.ToString();
  766. int idx = url.IndexOf("handler/", StringComparison.OrdinalIgnoreCase);
  767. if (idx != -1)
  768. {
  769. url = "http://" + host + ":" + port + "/" + url.Substring(idx);
  770. }
  771. else
  772. {
  773. url = "http://" + host + ":" + port + "/" + url;
  774. }
  775. return url;
  776. }
  777. private static string getTime(string s, string e)
  778. {
  779. if (s != "" && e != "") return "时间:" + s + " 至 " + e;
  780. else if (s != "") return "时间:" + s;
  781. else if (e != "") return "时间:" + e;
  782. return "";
  783. }
  784. private static string getTbStatusByState(string st)
  785. {
  786. string stateStr = "待发货";
  787. if (st == "NOT_SHIPPED")
  788. {
  789. stateStr = "待发货";
  790. }
  791. else if (st == "PART_SHIPPED")
  792. {
  793. stateStr = "部分发货";
  794. }
  795. else if (st == "SHIPPED")
  796. {
  797. stateStr = "已发货";
  798. }
  799. else if (st == "COMPLETE")
  800. {
  801. stateStr = "已完成";
  802. }
  803. else if (st == "CLOSE")
  804. {
  805. stateStr = "已关闭";
  806. }
  807. else if (st == "REFUNDED")
  808. {
  809. stateStr = "退款成功";
  810. }
  811. else if (st == "PART_REFUNDED")
  812. {
  813. stateStr = "部分退款";
  814. }
  815. else if (st == "WAIT_BUYER_RETURN_GOODS")
  816. {
  817. stateStr = "等待买家退货";
  818. }
  819. else if (st == "REJECT_REFUNDED")
  820. {
  821. stateStr = "拒绝退货";
  822. }
  823. return stateStr;
  824. }
  825. private static string getOrderStateStr(int orderState)
  826. {
  827. var stateStr = "";
  828. if (orderState == 0)
  829. {
  830. stateStr = "待领单";
  831. }
  832. else if (orderState == 1)
  833. {
  834. stateStr = "待发布";
  835. }
  836. else if (orderState == 2)
  837. {
  838. stateStr = "待抢单";
  839. }
  840. else if (orderState == 3)
  841. {
  842. stateStr = "待设计";
  843. }
  844. else if (orderState == 4)
  845. {
  846. stateStr = "设计中";
  847. }
  848. else if (orderState == 5)
  849. {
  850. stateStr = "设计完成";
  851. }
  852. else if (orderState == 6)
  853. {
  854. stateStr = "下单完成";
  855. }
  856. else if (orderState == 7)
  857. {
  858. stateStr = "已发货";
  859. }
  860. else if (orderState == 8)
  861. {
  862. stateStr = "已完成";
  863. }
  864. else if (orderState == 9)
  865. {
  866. stateStr = "已关闭";
  867. }
  868. return stateStr;
  869. }
  870. private static string getOrderCashStateStr(int orderState)
  871. {
  872. var stateStr = "";
  873. if (orderState == 0)
  874. {
  875. stateStr = "待审核";
  876. }
  877. else if (orderState == 1)
  878. {
  879. stateStr = "已审核";
  880. }
  881. else if (orderState == 2)
  882. {
  883. stateStr = "已退回";
  884. }
  885. return stateStr;
  886. }
  887. public static string getAfterSaleByState(int orderState)
  888. {
  889. var stateStr = "";
  890. if (orderState == 1)
  891. {
  892. stateStr = "待处理";
  893. }
  894. else if (orderState == 2)
  895. {
  896. stateStr = "售后主管审核";
  897. }
  898. else if (orderState == 3)
  899. {
  900. stateStr = "责任主管审核";
  901. }
  902. else if (orderState == 4)
  903. {
  904. stateStr = "已完成";
  905. }
  906. return stateStr;
  907. }
  908. #endregion
  909. }