ExportService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. using System.Text.RegularExpressions;
  12. namespace MySystem
  13. {
  14. public class ExportService
  15. {
  16. public readonly static ExportService Instance = new ExportService();
  17. private ExportService()
  18. { }
  19. public void Start()
  20. {
  21. Thread th = new Thread(StartDo);
  22. th.IsBackground = true;
  23. th.Start();
  24. }
  25. public void StartDo()
  26. {
  27. while (true)
  28. {
  29. string content = RedisDbconn.Instance.RPop<string>("ExportQueue");
  30. if(!string.IsNullOrEmpty(content))
  31. {
  32. try
  33. {
  34. JsonData jsonObj = JsonMapper.ToObject(content);
  35. string FileName = jsonObj["FileName"].ToString();
  36. if(FileName == "执行")
  37. {
  38. StartExcute(content);
  39. }
  40. else
  41. {
  42. StartExport(content);
  43. }
  44. }
  45. catch(Exception ex)
  46. {
  47. function.WriteLog(ex.ToString() + "\n\n", "导出日志异常");
  48. }
  49. }
  50. else
  51. {
  52. Thread.Sleep(5000);
  53. }
  54. }
  55. }
  56. public void StartExcute(string content)
  57. {
  58. function.WriteLog(DateTime.Now.ToString() + "\n" + content, "执行日志");
  59. JsonData jsonObj = JsonMapper.ToObject(content);
  60. string Operater = jsonObj["Operater"].ToString();
  61. string SqlString = jsonObj["SqlString"].ToString();
  62. string FileName = jsonObj["FileName"].ToString();
  63. int pageSize = int.Parse(jsonObj["MaxCount"].ToString());
  64. string ConnectStr = "";
  65. if(content.Contains("\"ConnectStr\""))
  66. {
  67. ConnectStr = jsonObj[prop_name: "ConnectStr"].ToString();
  68. }
  69. else
  70. {
  71. ConnectStr = MysqlConn.connstr;
  72. }
  73. //执行
  74. int page = 0;
  75. bool op = true;
  76. while(op)
  77. {
  78. int skip = page * pageSize;
  79. string sql = SqlString + " limit " + skip + "," + pageSize;
  80. function.WriteLog(sql, "执行日志");
  81. DataTable dt = CustomerSqlConn.dtable(sql, ConnectStr);
  82. if(dt.Rows.Count > 0)
  83. {
  84. string dosql = "";
  85. foreach (DataRow dr in dt.Rows)
  86. {
  87. dosql += dr[0].ToString();
  88. }
  89. CustomerSqlConn.op(dosql, ConnectStr);
  90. page += 1;
  91. }
  92. else
  93. {
  94. op = false;
  95. }
  96. }
  97. }
  98. public void StartExport(string content)
  99. {
  100. function.WriteLog(DateTime.Now.ToString() + "\n" + content, "导出日志");
  101. JsonData jsonObj = JsonMapper.ToObject(content);
  102. string Operater = jsonObj["Operater"].ToString();
  103. string SqlString = jsonObj["SqlString"].ToString();
  104. string FileName = jsonObj["FileName"].ToString();
  105. int MaxCount = int.Parse(jsonObj["MaxCount"].ToString());
  106. string ConnectStr = "";
  107. if(content.Contains("\"ConnectStr\""))
  108. {
  109. ConnectStr = jsonObj["ConnectStr"].ToString();
  110. }
  111. else
  112. {
  113. ConnectStr = MysqlConn.connstr;
  114. }
  115. bool SubSqlFlag = content.Contains("\"SubSqlString\"");
  116. //创建工作薄
  117. var workbook = new XSSFWorkbook();
  118. //单元格样式
  119. ICellStyle style = workbook.CreateCellStyle();
  120. style.BorderBottom = BorderStyle.Thin;
  121. style.BorderTop = BorderStyle.Thin;
  122. style.BorderLeft = BorderStyle.Thin;
  123. style.BorderRight = BorderStyle.Thin;
  124. style.BottomBorderColor = HSSFColor.Black.Index;
  125. style.TopBorderColor = HSSFColor.Black.Index;
  126. style.LeftBorderColor = HSSFColor.Black.Index;
  127. style.RightBorderColor = HSSFColor.Black.Index;
  128. //创建表
  129. var table = workbook.CreateSheet("Sheet1");
  130. string FilePath = function.ReadInstance("/WebRootPath.txt");
  131. function.WriteLog(FilePath, "导出日志");
  132. //获取子集字典数据
  133. Dictionary<string, Dictionary<string, string>> dicValsJson = new Dictionary<string, Dictionary<string, string>>();
  134. if(SubSqlFlag)
  135. {
  136. JsonData subItems = jsonObj["SubSqlString"];
  137. for (int index = 0; index < subItems.Count; index++)
  138. {
  139. JsonData sub = subItems[index];
  140. string DatabaseConnect = sub["DatabaseConnect"].ToString();
  141. string SqlContent = sub["SqlContent"].ToString();
  142. string DataKey = sub["DataKey"].ToString();
  143. string DataText = sub["DataText"].ToString();
  144. string Alias = sub["Alias"].ToString();
  145. string SubConnectStr = DatabaseConnect;
  146. if(!string.IsNullOrEmpty(SubConnectStr))
  147. {
  148. SubConnectStr = GetConnectStr(DatabaseConnect);
  149. }
  150. else
  151. {
  152. SubConnectStr = MysqlConn.readconnstr;
  153. }
  154. string sqlstr = SqlContent;
  155. DataTable dtsub = CustomerSqlConn.dtable(sqlstr, SubConnectStr);
  156. if(dtsub.Rows.Count > 0)
  157. {
  158. if(!string.IsNullOrEmpty(DataKey))
  159. {
  160. string[] TextFields = DataText.Split(',');
  161. Dictionary<string, string> vals = new Dictionary<string, string>();
  162. foreach(DataRow dr in dtsub.Rows)
  163. {
  164. foreach(string key in TextFields)
  165. {
  166. vals.Add(key, dr[key].ToString());
  167. }
  168. }
  169. dicValsJson.Add(Alias + "." + DataKey, vals);
  170. }
  171. }
  172. }
  173. }
  174. //填充数据
  175. int i = 0;
  176. int page = 0;
  177. int pageSize = 200;
  178. bool op = true;
  179. while(op)
  180. {
  181. var fs = System.IO.File.OpenWrite(FilePath + "/exportfile/" + FileName + ".xlsx");
  182. int skip = page * pageSize;
  183. string sql = SqlString + " limit " + skip + "," + pageSize;
  184. function.WriteLog(sql, "导出日志");
  185. DataTable dt = CustomerSqlConn.dtable(sql, ConnectStr);
  186. if(dt.Rows.Count > 0)
  187. {
  188. foreach (DataRow dr in dt.Rows)
  189. {
  190. i += 1;
  191. if(i <= MaxCount || MaxCount == 0)
  192. {
  193. if(i == 1)
  194. {
  195. //创建表头
  196. var title = table.CreateRow(0);
  197. int j = 0;
  198. foreach (DataColumn dc in dt.Columns)
  199. {
  200. string val = dr[dc.ColumnName].ToString();
  201. Match m = Regex.Match(val, @"\#\{.*?\}\#");
  202. if(m.Success)
  203. {
  204. string fieldName = m.Value.Replace("#{", "").Replace("}#", "");
  205. Dictionary<string, string> fieldVals = dicValsJson[fieldName];
  206. foreach(string field in fieldVals.Keys)
  207. {
  208. var cell = title.CreateCell(j);
  209. cell.CellStyle = style;
  210. cell.SetCellValue(dc.ColumnName + field);
  211. j += 1;
  212. }
  213. }
  214. else
  215. {
  216. var cell = title.CreateCell(j);
  217. cell.CellStyle = style;
  218. cell.SetCellValue(dc.ColumnName);
  219. j += 1;
  220. }
  221. }
  222. }
  223. var row = table.CreateRow(i);
  224. int index = 0;
  225. foreach (DataColumn dc in dt.Columns)
  226. {
  227. string val = dr[dc.ColumnName].ToString();
  228. Match m = Regex.Match(val, @"\#\{.*?\}\#");
  229. if(m.Success)
  230. {
  231. string fieldName = m.Value.Replace("#{", "").Replace("}#", "");
  232. Dictionary<string, string> fieldVals = dicValsJson[fieldName];
  233. foreach(string field in fieldVals.Keys)
  234. {
  235. var cell = row.CreateCell(index);
  236. cell.CellStyle = style;
  237. cell.SetCellValue(fieldVals[field]);
  238. index += 1;
  239. }
  240. }
  241. else
  242. {
  243. var cell = row.CreateCell(index);
  244. cell.CellStyle = style;
  245. cell.SetCellValue(val);
  246. index += 1;
  247. }
  248. }
  249. }
  250. else
  251. {
  252. op = false;
  253. }
  254. }
  255. //打开xls文件,如没有则创建,如存在则在创建是不要打开该文件
  256. workbook.Write(fs); //向打开的这个xls文件中写入mySheet表并保存。
  257. page += 1;
  258. }
  259. else
  260. {
  261. op = false;
  262. }
  263. fs.Dispose();
  264. }
  265. if(i > 0)
  266. {
  267. string FileUrl = MysqlConn.sourceHost + "exportfile/" + FileName + ".xlsx";
  268. function.WriteLog(FileUrl, "导出日志");
  269. CustomerSqlConn.op("insert into ExportExcels (CreateDate,FileName,FileUrl,SysId) values ('" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "','" + FileName + "','" + FileUrl + "'," + Operater + ")", ConnectStr);
  270. function.WriteLog("insert into ExportExcels (CreateDate,FileName,FileUrl,SysId) values ('" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "','" + FileName + "','" + FileUrl + "'," + Operater + ")", "导出日志");
  271. function.WriteLog("end", "导出日志");
  272. }
  273. }
  274. #region 获取链接数据库字符串
  275. public string GetConnectStr(string key)
  276. {
  277. return Library.ConfigurationManager.AppSettings[key].ToString();
  278. }
  279. #endregion
  280. }
  281. }