ExportService.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. StartExport(content);
  32. }
  33. else
  34. {
  35. Thread.Sleep(5000);
  36. }
  37. }
  38. }
  39. public void StartExport(string content)
  40. {
  41. JsonData jsonObj = JsonMapper.ToObject(content);
  42. string Operater = jsonObj["Operater"].ToString();
  43. string SqlString = jsonObj["SqlString"].ToString();
  44. string FileName = jsonObj["FileName"].ToString();
  45. int MaxCount = int.Parse(jsonObj["MaxCount"].ToString());
  46. //创建工作薄
  47. var workbook = new XSSFWorkbook();
  48. //单元格样式
  49. ICellStyle style = workbook.CreateCellStyle();
  50. style.BorderBottom = BorderStyle.Thin;
  51. style.BorderTop = BorderStyle.Thin;
  52. style.BorderLeft = BorderStyle.Thin;
  53. style.BorderRight = BorderStyle.Thin;
  54. style.BottomBorderColor = HSSFColor.Black.Index;
  55. style.TopBorderColor = HSSFColor.Black.Index;
  56. style.LeftBorderColor = HSSFColor.Black.Index;
  57. style.RightBorderColor = HSSFColor.Black.Index;
  58. //创建表
  59. var table = workbook.CreateSheet("Sheet1");
  60. string FilePath = function.ReadInstance("/WebRootPath.txt");
  61. //填充数据
  62. int i = 0;
  63. int page = 0;
  64. int pageSize = 200;
  65. bool op = true;
  66. while(op)
  67. {
  68. var fs = System.IO.File.OpenWrite(FilePath + "/exportfile/" + FileName + ".xlsx");
  69. int skip = page * pageSize;
  70. DataTable dt = CustomerSqlConn.dtable(SqlString + " limit " + skip + "," + pageSize, MysqlConn.connstr);
  71. if(dt.Rows.Count > 0)
  72. {
  73. foreach (DataRow dr in dt.Rows)
  74. {
  75. i += 1;
  76. if(i <= MaxCount || MaxCount == 0)
  77. {
  78. if(i == 1)
  79. {
  80. //创建表头
  81. var title = table.CreateRow(0);
  82. int j = 0;
  83. foreach (DataColumn dc in dt.Columns)
  84. {
  85. var cell = title.CreateCell(j);
  86. cell.CellStyle = style;
  87. cell.SetCellValue(dc.ColumnName);
  88. j += 1;
  89. }
  90. }
  91. var row = table.CreateRow(i);
  92. int index = 0;
  93. foreach (DataColumn dc in dt.Columns)
  94. {
  95. var cell = row.CreateCell(index);
  96. string val = dr[dc.ColumnName].ToString();
  97. cell.CellStyle = style;
  98. cell.SetCellValue(val);
  99. index += 1;
  100. }
  101. }
  102. else
  103. {
  104. op = false;
  105. }
  106. }
  107. //打开xls文件,如没有则创建,如存在则在创建是不要打开该文件
  108. workbook.Write(fs); //向打开的这个xls文件中写入mySheet表并保存。
  109. page += 1;
  110. }
  111. else
  112. {
  113. op = false;
  114. }
  115. fs.Dispose();
  116. }
  117. if(i > 0)
  118. {
  119. string FileUrl = MysqlConn.sourceHost + "exportfile/" + FileName + ".xlsx";
  120. CustomerSqlConn.op("insert into ExportExcels (CreateDate,FileName,FileUrl,SysId) values ('" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "','" + FileName + "','" + FileUrl + "'," + Operater + ")", MysqlConn.connstr);
  121. }
  122. }
  123. }
  124. }