ExportService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. if(!string.IsNullOrEmpty(jsonObj["ConnectStr"].ToString()))
  110. {
  111. ConnectStr = GetConnectStr(jsonObj["ConnectStr"].ToString());
  112. }
  113. else
  114. {
  115. ConnectStr = MysqlConn.readconnstr;
  116. }
  117. }
  118. else
  119. {
  120. ConnectStr = MysqlConn.readconnstr;
  121. }
  122. bool SubSqlFlag = content.Contains("\"SubSqlString\"");
  123. //创建工作薄
  124. var workbook = new XSSFWorkbook();
  125. //单元格样式
  126. ICellStyle style = workbook.CreateCellStyle();
  127. style.BorderBottom = BorderStyle.Thin;
  128. style.BorderTop = BorderStyle.Thin;
  129. style.BorderLeft = BorderStyle.Thin;
  130. style.BorderRight = BorderStyle.Thin;
  131. style.BottomBorderColor = HSSFColor.Black.Index;
  132. style.TopBorderColor = HSSFColor.Black.Index;
  133. style.LeftBorderColor = HSSFColor.Black.Index;
  134. style.RightBorderColor = HSSFColor.Black.Index;
  135. //创建表
  136. var table = workbook.CreateSheet("Sheet1");
  137. string FilePath = function.ReadInstance("/WebRootPath.txt");
  138. function.WriteLog(FilePath, "导出日志");
  139. //获取子集字典数据
  140. Dictionary<string, Dictionary<string, string>> dicValsJson = new Dictionary<string, Dictionary<string, string>>();
  141. if(SubSqlFlag)
  142. {
  143. JsonData subItems = jsonObj["SubSqlString"];
  144. for (int index = 0; index < subItems.Count; index++)
  145. {
  146. JsonData sub = subItems[index];
  147. string DatabaseConnect = sub["DatabaseConnect"].ToString();
  148. string SqlContent = sub["SqlContent"].ToString();
  149. string DataKey = sub["DataKey"].ToString();
  150. string DataText = sub["DataText"].ToString();
  151. string Alias = sub["Alias"].ToString();
  152. string SubConnectStr = DatabaseConnect;
  153. if(!string.IsNullOrEmpty(SubConnectStr))
  154. {
  155. SubConnectStr = GetConnectStr(DatabaseConnect);
  156. }
  157. else
  158. {
  159. SubConnectStr = MysqlConn.readconnstr;
  160. }
  161. string sqlstr = SqlContent;
  162. DataTable dtsub = CustomerSqlConn.dtable(sqlstr, SubConnectStr);
  163. if(dtsub.Rows.Count > 0)
  164. {
  165. if(!string.IsNullOrEmpty(DataKey))
  166. {
  167. string[] TextFields = DataText.Split(',');
  168. foreach(DataRow dr in dtsub.Rows)
  169. {
  170. Dictionary<string, string> vals = new Dictionary<string, string>();
  171. foreach (string key in TextFields)
  172. {
  173. vals.Add(key, dr[key].ToString());
  174. }
  175. dicValsJson.Add(Alias + "." + dr[DataKey].ToString(), vals);
  176. }
  177. }
  178. }
  179. }
  180. }
  181. //填充数据
  182. int i = 0;
  183. int page = 0;
  184. int pageSize = 200;
  185. bool op = true;
  186. while(op)
  187. {
  188. var fs = System.IO.File.OpenWrite(FilePath + "/exportfile/" + FileName + ".xlsx");
  189. int skip = page * pageSize;
  190. string sql = SqlString + " limit " + skip + "," + pageSize;
  191. function.WriteLog(sql, "导出日志");
  192. DataTable dt = CustomerSqlConn.dtable(sql, ConnectStr);
  193. if(dt.Rows.Count > 0)
  194. {
  195. foreach (DataRow dr in dt.Rows)
  196. {
  197. i += 1;
  198. if(i <= MaxCount || MaxCount == 0)
  199. {
  200. if(i == 1)
  201. {
  202. //创建表头
  203. var title = table.CreateRow(0);
  204. int j = 0;
  205. foreach (DataColumn dc in dt.Columns)
  206. {
  207. string val = dr[dc.ColumnName].ToString();
  208. Match m = Regex.Match(val, @"\#\{.*?\}\#");
  209. if(m.Success)
  210. {
  211. string fieldName = m.Value.Replace("#{", "").Replace("}#", "");
  212. Dictionary<string, string> fieldVals = dicValsJson[fieldName];
  213. foreach(string field in fieldVals.Keys)
  214. {
  215. var cell = title.CreateCell(j);
  216. cell.CellStyle = style;
  217. cell.SetCellValue(dc.ColumnName + field);
  218. j += 1;
  219. }
  220. }
  221. else
  222. {
  223. var cell = title.CreateCell(j);
  224. cell.CellStyle = style;
  225. cell.SetCellValue(dc.ColumnName);
  226. j += 1;
  227. }
  228. }
  229. }
  230. var row = table.CreateRow(i);
  231. int index = 0;
  232. foreach (DataColumn dc in dt.Columns)
  233. {
  234. string val = dr[dc.ColumnName].ToString();
  235. Match m = Regex.Match(val, @"\#\{.*?\}\#");
  236. if(m.Success)
  237. {
  238. string fieldName = m.Value.Replace("#{", "").Replace("}#", "");
  239. Dictionary<string, string> fieldVals = dicValsJson[fieldName];
  240. foreach(string field in fieldVals.Keys)
  241. {
  242. var cell = row.CreateCell(index);
  243. cell.CellStyle = style;
  244. cell.SetCellValue(fieldVals[field]);
  245. index += 1;
  246. }
  247. }
  248. else
  249. {
  250. var cell = row.CreateCell(index);
  251. cell.CellStyle = style;
  252. cell.SetCellValue(val);
  253. index += 1;
  254. }
  255. }
  256. }
  257. else
  258. {
  259. op = false;
  260. }
  261. }
  262. //打开xls文件,如没有则创建,如存在则在创建是不要打开该文件
  263. workbook.Write(fs); //向打开的这个xls文件中写入mySheet表并保存。
  264. page += 1;
  265. }
  266. else
  267. {
  268. op = false;
  269. }
  270. fs.Dispose();
  271. }
  272. if(i > 0)
  273. {
  274. string FileUrl = MysqlConn.sourceHost + "exportfile/" + FileName + ".xlsx";
  275. function.WriteLog(FileUrl, "导出日志");
  276. CustomerSqlConn.op("insert into ExportExcels (CreateDate,FileName,FileUrl,SysId) values ('" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "','" + FileName + "','" + FileUrl + "'," + Operater + ")", MysqlConn.connstr);
  277. function.WriteLog("insert into ExportExcels (CreateDate,FileName,FileUrl,SysId) values ('" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "','" + FileName + "','" + FileUrl + "'," + Operater + ")", "导出日志");
  278. function.WriteLog("end", "导出日志");
  279. }
  280. }
  281. #region 获取链接数据库字符串
  282. public string GetConnectStr(string key)
  283. {
  284. return Library.ConfigurationManager.AppSettings[key].ToString();
  285. }
  286. #endregion
  287. }
  288. }