ExportService.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Collections.Generic;
  3. using Library;
  4. using LitJson;
  5. using System.Data;
  6. using System.Threading;
  7. using NPOI.HSSF.UserModel;
  8. using NPOI.XSSF.UserModel;
  9. using NPOI.SS.UserModel;
  10. using NPOI.HSSF.Util;
  11. namespace MySystem
  12. {
  13. public class ExportService
  14. {
  15. public readonly static ExportService Instance = new ExportService();
  16. private ExportService()
  17. { }
  18. public void Start()
  19. {
  20. Thread th = new Thread(StartDo);
  21. th.IsBackground = true;
  22. th.Start();
  23. }
  24. public void StartDo()
  25. {
  26. while (true)
  27. {
  28. string content = RedisDbconn.Instance.RPop<string>("ExportQueue");
  29. if(!string.IsNullOrEmpty(content))
  30. {
  31. try
  32. {
  33. JsonData jsonObj = JsonMapper.ToObject(content);
  34. string FileName = jsonObj["FileName"].ToString();
  35. if(FileName == "执行")
  36. {
  37. StartExcute(content);
  38. }
  39. else
  40. {
  41. StartExport(content);
  42. }
  43. }
  44. catch(Exception ex)
  45. {
  46. function.WriteLog(ex.ToString() + "\n\n", "导出日志异常");
  47. }
  48. }
  49. else
  50. {
  51. Thread.Sleep(5000);
  52. }
  53. }
  54. }
  55. public void StartExcute(string content)
  56. {
  57. function.WriteLog(DateTime.Now.ToString() + "\n" + content, "执行日志");
  58. JsonData jsonObj = JsonMapper.ToObject(content);
  59. string Operater = jsonObj["Operater"].ToString();
  60. string SqlString = jsonObj["SqlString"].ToString();
  61. string FileName = jsonObj["FileName"].ToString();
  62. int pageSize = int.Parse(jsonObj["MaxCount"].ToString());
  63. //执行
  64. int page = 0;
  65. bool op = true;
  66. while(op)
  67. {
  68. int skip = page * pageSize;
  69. string sql = SqlString + " limit " + skip + "," + pageSize;
  70. function.WriteLog(sql, "执行日志");
  71. DataTable dt = CustomerSqlConn.dtable(sql, MysqlConn.readconnstr);
  72. if(dt.Rows.Count > 0)
  73. {
  74. string dosql = "";
  75. foreach (DataRow dr in dt.Rows)
  76. {
  77. dosql += dr[0].ToString();
  78. }
  79. CustomerSqlConn.op(dosql, MysqlConn.connstr);
  80. page += 1;
  81. }
  82. else
  83. {
  84. op = false;
  85. }
  86. }
  87. }
  88. public void StartExport(string content)
  89. {
  90. function.WriteLog(DateTime.Now.ToString() + "\n" + content, "导出日志");
  91. JsonData jsonObj = JsonMapper.ToObject(content);
  92. string Operater = jsonObj["Operater"].ToString();
  93. string SqlString = jsonObj["SqlString"].ToString();
  94. string FileName = jsonObj["FileName"].ToString();
  95. int MaxCount = int.Parse(jsonObj["MaxCount"].ToString());
  96. //创建工作薄
  97. var workbook = new XSSFWorkbook();
  98. //单元格样式
  99. ICellStyle style = workbook.CreateCellStyle();
  100. style.BorderBottom = BorderStyle.Thin;
  101. style.BorderTop = BorderStyle.Thin;
  102. style.BorderLeft = BorderStyle.Thin;
  103. style.BorderRight = BorderStyle.Thin;
  104. style.BottomBorderColor = HSSFColor.Black.Index;
  105. style.TopBorderColor = HSSFColor.Black.Index;
  106. style.LeftBorderColor = HSSFColor.Black.Index;
  107. style.RightBorderColor = HSSFColor.Black.Index;
  108. //创建表
  109. var table = workbook.CreateSheet("Sheet1");
  110. string FilePath = function.ReadInstance("/WebRootPath.txt");
  111. function.WriteLog(FilePath, "导出日志");
  112. //填充数据
  113. int i = 0;
  114. int page = 0;
  115. int pageSize = 200;
  116. bool op = true;
  117. while(op)
  118. {
  119. var fs = System.IO.File.OpenWrite(FilePath + "/exportfile/" + FileName + ".xlsx");
  120. int skip = page * pageSize;
  121. string sql = SqlString + " limit " + skip + "," + pageSize;
  122. function.WriteLog(sql, "导出日志");
  123. DataTable dt = CustomerSqlConn.dtable(sql, MysqlConn.readconnstr);
  124. if(dt.Rows.Count > 0)
  125. {
  126. foreach (DataRow dr in dt.Rows)
  127. {
  128. i += 1;
  129. if(i <= MaxCount || MaxCount == 0)
  130. {
  131. if(i == 1)
  132. {
  133. //创建表头
  134. var title = table.CreateRow(0);
  135. int j = 0;
  136. foreach (DataColumn dc in dt.Columns)
  137. {
  138. var cell = title.CreateCell(j);
  139. cell.CellStyle = style;
  140. cell.SetCellValue(dc.ColumnName);
  141. j += 1;
  142. }
  143. }
  144. var row = table.CreateRow(i);
  145. int index = 0;
  146. foreach (DataColumn dc in dt.Columns)
  147. {
  148. var cell = row.CreateCell(index);
  149. string val = dr[dc.ColumnName].ToString();
  150. cell.CellStyle = style;
  151. cell.SetCellValue(val);
  152. index += 1;
  153. }
  154. }
  155. else
  156. {
  157. op = false;
  158. }
  159. }
  160. //打开xls文件,如没有则创建,如存在则在创建是不要打开该文件
  161. workbook.Write(fs); //向打开的这个xls文件中写入mySheet表并保存。
  162. page += 1;
  163. }
  164. else
  165. {
  166. op = false;
  167. }
  168. fs.Dispose();
  169. }
  170. if(i > 0)
  171. {
  172. string FileUrl = MysqlConn.sourceHost + "exportfile/" + FileName + ".xlsx";
  173. function.WriteLog(FileUrl, "导出日志");
  174. CustomerSqlConn.op("insert into ExportExcels (CreateDate,FileName,FileUrl,SysId) values ('" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "','" + FileName + "','" + FileUrl + "'," + Operater + ")", MysqlConn.connstr);
  175. function.WriteLog("insert into ExportExcels (CreateDate,FileName,FileUrl,SysId) values ('" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "','" + FileName + "','" + FileUrl + "'," + Operater + ")", "导出日志");
  176. function.WriteLog("end", "导出日志");
  177. }
  178. }
  179. }
  180. }