ExcelExtension.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. using OfficeOpenXml;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. namespace Masuit.Tools.Excel
  10. {
  11. public static class ExcelExtension
  12. {
  13. static ExcelExtension()
  14. {
  15. ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
  16. }
  17. /// <summary>
  18. /// 将内存表自动填充到Excel
  19. /// </summary>
  20. /// <param name="sheetTables">sheet名和内存表的映射</param>
  21. /// <param name="password">密码</param>
  22. /// <returns>内存流</returns>
  23. public static MemoryStream ToExcel(this Dictionary<string, DataTable> sheetTables, string password = null, ColumnSettings settings = null)
  24. {
  25. using var pkg = new ExcelPackage();
  26. foreach (var pair in sheetTables)
  27. {
  28. pair.Value.TableName = pair.Key;
  29. CreateWorksheet(pkg, pair.Value, settings);
  30. }
  31. return SaveAsStream(pkg, password);
  32. }
  33. /// <summary>
  34. /// 将内存表自动填充到Excel
  35. /// </summary>
  36. /// <param name="tables">内存表</param>
  37. /// <param name="password">密码</param>
  38. /// <returns>内存流</returns>
  39. public static MemoryStream ToExcel(this List<DataTable> tables, string password = null, ColumnSettings settings = null)
  40. {
  41. using var pkg = new ExcelPackage();
  42. foreach (var table in tables)
  43. {
  44. CreateWorksheet(pkg, table, settings);
  45. }
  46. return SaveAsStream(pkg, password);
  47. }
  48. /// <summary>
  49. /// 将内存表自动填充到Excel
  50. /// </summary>
  51. /// <param name="table">内存表</param>
  52. /// <param name="password">密码</param>
  53. /// <returns>内存流</returns>
  54. public static MemoryStream ToExcel(this DataTable table, string password = null, ColumnSettings settings = null)
  55. {
  56. using var pkg = new ExcelPackage();
  57. CreateWorksheet(pkg, table, settings);
  58. return SaveAsStream(pkg, password);
  59. }
  60. private static MemoryStream SaveAsStream(ExcelPackage pkg, string password)
  61. {
  62. var ms = new MemoryStream();
  63. if (!string.IsNullOrEmpty(password))
  64. {
  65. pkg.SaveAs(ms, password);
  66. }
  67. else
  68. {
  69. pkg.SaveAs(ms);
  70. }
  71. return ms;
  72. }
  73. public static void CreateWorksheet(this ExcelPackage pkg, DataTable table, ColumnSettings settings = null)
  74. {
  75. if (string.IsNullOrEmpty(table.TableName))
  76. {
  77. table.TableName = "Sheet1";
  78. }
  79. pkg.Workbook.Worksheets.Add(table.TableName);
  80. var sheet = pkg.Workbook.Worksheets[table.TableName];
  81. FillWorksheet(sheet, table, settings);
  82. //打印方向:纵向
  83. sheet.PrinterSettings.Orientation = eOrientation.Landscape;
  84. //集中在一页里打印
  85. sheet.PrinterSettings.FitToPage = true;
  86. //使用A4纸
  87. sheet.PrinterSettings.PaperSize = ePaperSize.A4;
  88. }
  89. /// <summary>
  90. /// 从datatable填充工作簿
  91. /// </summary>
  92. /// <param name="sheet">工作簿</param>
  93. /// <param name="table">数据</param>
  94. /// <param name="settings">列设置</param>
  95. /// <param name="startRow">起始行,默认第一行</param>
  96. /// <param name="startColumn">起始列,默认第一列A列</param>
  97. public static void FillWorksheet(this ExcelWorksheet sheet, DataTable table, ColumnSettings settings = null, int startRow = 1, int startColumn = 1)
  98. {
  99. // 填充表头
  100. var maxWidth = new int[table.Columns.Count];
  101. for (var j = 0; j < table.Columns.Count; j++)
  102. {
  103. sheet.SetValue(startRow, j + startColumn, table.Columns[j].ColumnName);
  104. maxWidth[j] = Encoding.UTF8.GetBytes(table.Columns[j].ColumnName).Length;
  105. }
  106. sheet.Row(startRow).Style.Font.Bold = true; // 表头设置为粗体
  107. sheet.Row(startRow).Style.Font.Size = sheet.Row(startRow).Style.Font.Size * 1.11f; // 表头字号放大1.11倍
  108. sheet.Row(startRow).CustomHeight = true; // 自动调整行高
  109. sheet.Cells.AutoFitColumns(); // 表头自适应列宽
  110. sheet.Cells.Style.WrapText = true;
  111. if (settings != null)
  112. {
  113. foreach (var x in settings.ColumnTypes)
  114. {
  115. sheet.Column(x.Key).Style.Numberformat.Format = x.Value;
  116. }
  117. }
  118. // 填充内容
  119. for (var i = 0; i < table.Rows.Count; i++)
  120. {
  121. sheet.Row(i + startRow + 1).CustomHeight = true; // 自动调整行高
  122. for (var j = 0; j < table.Columns.Count; j++)
  123. {
  124. switch (table.Rows[i][j])
  125. {
  126. case Stream s:
  127. {
  128. if (s.Length > 2)
  129. {
  130. using var bmp = new Bitmap(s);
  131. bmp.SetResolution(96, 96);
  132. var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp);
  133. picture.SetPosition(i + startRow, 3, j + startColumn - 1, 5); //设置图片显示位置
  134. var percent = 11000f / bmp.Height;
  135. picture.SetSize((int)percent);
  136. sheet.Row(i + startRow + 1).Height = 90;
  137. sheet.Column(j + startColumn).Width = Math.Max(sheet.Column(j + startColumn).Width, bmp.Width * percent / 600 > 32 ? bmp.Width * percent / 600 : 32);
  138. }
  139. sheet.SetValue(i + startRow + 1, j + startColumn, "");
  140. break;
  141. }
  142. case Bitmap bmp:
  143. {
  144. if (bmp.Width + bmp.Height > 4)
  145. {
  146. bmp.SetResolution(96, 96);
  147. var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp);
  148. picture.SetPosition(i + startRow, 3, j + startColumn - 1, 5); //设置图片显示位置
  149. var percent = 11000f / bmp.Height;
  150. picture.SetSize((int)percent);
  151. sheet.Row(i + startRow + 1).Height = 90;
  152. sheet.Column(j + startColumn).Width = Math.Max(sheet.Column(j + startColumn).Width, bmp.Width * percent / 600 > 32 ? bmp.Width * percent / 600 : 32);
  153. }
  154. sheet.SetValue(i + startRow + 1, j + startColumn, "");
  155. break;
  156. }
  157. case IEnumerable<Stream> streams:
  158. {
  159. double sumWidth = 0;
  160. foreach (var stream in streams.Where(stream => stream.Length > 2))
  161. {
  162. using var bmp = new Bitmap(stream);
  163. bmp.SetResolution(96, 96);
  164. var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp);
  165. picture.SetPosition(i + startRow, 3, j + startColumn - 1, (int)(5 + sumWidth)); //设置图片显示位置
  166. var percent = 11000f / bmp.Height;
  167. picture.SetSize((int)percent);
  168. sheet.Row(i + startRow + 1).Height = 90;
  169. sumWidth += bmp.Width * 1.0 * percent / 100 + 5;
  170. sheet.Column(j + startColumn).Width = Math.Max(sheet.Column(j + startColumn).Width, sumWidth / 6 > 32 ? sumWidth / 6 : 32);
  171. }
  172. sheet.SetValue(i + startRow + 1, j + startColumn, "");
  173. break;
  174. }
  175. case IEnumerable<Bitmap> bmps:
  176. {
  177. double sumWidth = 0;
  178. foreach (var bmp in bmps.Where(stream => stream.Width + stream.Height > 4))
  179. {
  180. bmp.SetResolution(96, 96);
  181. var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp);
  182. picture.SetPosition(i + startRow, 3, j + startColumn - 1, (int)(5 + sumWidth)); //设置图片显示位置
  183. var percent = 11000f / bmp.Height;
  184. picture.SetSize((int)percent);
  185. sheet.Row(i + startRow + 1).Height = 90;
  186. sumWidth += bmp.Width * 1.0 * percent / 100 + 5;
  187. sheet.Column(j + startColumn).Width = Math.Max(sheet.Column(j + startColumn).Width, sumWidth / 6 > 32 ? sumWidth / 6 : 32);
  188. }
  189. sheet.SetValue(i + startRow + 1, j + startColumn, "");
  190. break;
  191. }
  192. case IDictionary<string, Stream> dic:
  193. {
  194. double sumWidth = 0;
  195. foreach (var kv in dic.Where(kv => kv.Value.Length > 2))
  196. {
  197. using var bmp = new Bitmap(kv.Value);
  198. bmp.SetResolution(96, 96);
  199. var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp, new Uri(kv.Key));
  200. picture.SetPosition(i + startRow, 3, j + startColumn - 1, (int)(5 + sumWidth)); //设置图片显示位置
  201. var percent = 11000f / bmp.Height;
  202. picture.SetSize((int)percent);
  203. sheet.Row(i + startRow + 1).Height = 90;
  204. sumWidth += bmp.Width * 1.0 * percent / 100 + 5;
  205. sheet.Column(j + startColumn).Width = Math.Max(sheet.Column(j + startColumn).Width, sumWidth / 6 > 32 ? sumWidth / 6 : 32);
  206. }
  207. sheet.SetValue(i + startRow + 1, j + startColumn, "");
  208. break;
  209. }
  210. case IDictionary<string, MemoryStream> dic:
  211. {
  212. double sumWidth = 0;
  213. foreach (var kv in dic.Where(kv => kv.Value.Length > 2))
  214. {
  215. using var bmp = new Bitmap(kv.Value);
  216. bmp.SetResolution(96, 96);
  217. var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp, new Uri(kv.Key));
  218. picture.SetPosition(i + startRow, 3, j + startColumn - 1, (int)(5 + sumWidth)); //设置图片显示位置
  219. var percent = 11000f / bmp.Height;
  220. picture.SetSize((int)percent);
  221. sheet.Row(i + startRow + 1).Height = 90;
  222. sumWidth += bmp.Width * 1.0 * percent / 100 + 5;
  223. sheet.Column(j + startColumn).Width = Math.Max(sheet.Column(j + startColumn).Width, sumWidth / 6 > 32 ? sumWidth / 6 : 32);
  224. }
  225. sheet.SetValue(i + startRow + 1, j + startColumn, "");
  226. break;
  227. }
  228. case IDictionary<string, Bitmap> bmps:
  229. {
  230. double sumWidth = 0;
  231. foreach (var kv in bmps.Where(kv => kv.Value.Width + kv.Value.Height > 4))
  232. {
  233. kv.Value.SetResolution(96, 96);
  234. var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), kv.Value, new Uri(kv.Key));
  235. picture.SetPosition(i + startRow, 3, j + startColumn - 1, (int)(5 + sumWidth)); //设置图片显示位置
  236. var percent = 11000f / kv.Value.Height;
  237. picture.SetSize((int)percent);
  238. sheet.Row(i + startRow + 1).Height = 90;
  239. sumWidth += kv.Value.Width * 1.0 * percent / 100 + 5;
  240. sheet.Column(j + startColumn).Width = Math.Max(sheet.Column(j + startColumn).Width, sumWidth / 6 > 32 ? sumWidth / 6 : 32);
  241. }
  242. sheet.SetValue(i + startRow + 1, j + startColumn, "");
  243. break;
  244. }
  245. default:
  246. {
  247. sheet.SetValue(i + startRow + 1, j + startColumn, table.Rows[i][j] ?? "");
  248. if (table.Rows[i][j] is ValueType)
  249. {
  250. sheet.Column(j + startColumn).AutoFit();
  251. }
  252. else
  253. {
  254. // 根据单元格内容长度来自适应调整列宽
  255. sheet.Column(j + startColumn).Width = Math.Max(Encoding.UTF8.GetBytes(table.Rows[i][j].ToString() ?? string.Empty).Length + 2, sheet.Column(j + startColumn).Width);
  256. if (sheet.Column(j + startColumn).Width > 110)
  257. {
  258. sheet.Column(j + startColumn).AutoFit(100, 110);
  259. }
  260. }
  261. break;
  262. }
  263. }
  264. }
  265. }
  266. }
  267. }
  268. }