ExcelExtension.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. // 填充表头
  82. var maxWidth = new int[table.Columns.Count];
  83. for (var j = 0; j < table.Columns.Count; j++)
  84. {
  85. sheet.SetValue(1, j + 1, table.Columns[j].ColumnName);
  86. maxWidth[j] = Encoding.UTF8.GetBytes(table.Columns[j].ColumnName).Length;
  87. }
  88. sheet.Row(1).Style.Font.Bold = true; // 表头设置为粗体
  89. sheet.Row(1).Style.Font.Size = sheet.Row(1).Style.Font.Size * 1.11f; // 表头字号放大1.11倍
  90. sheet.Row(1).CustomHeight = true; // 自动调整行高
  91. sheet.Cells.AutoFitColumns(); // 表头自适应列宽
  92. sheet.Cells.Style.WrapText = true;
  93. if (settings != null)
  94. {
  95. foreach (var x in settings.ColumnTypes)
  96. {
  97. sheet.Column(x.Key).Style.Numberformat.Format = x.Value;
  98. }
  99. }
  100. // 填充内容
  101. for (var i = 0; i < table.Rows.Count; i++)
  102. {
  103. sheet.Row(i + 2).CustomHeight = true; // 自动调整行高
  104. for (var j = 0; j < table.Columns.Count; j++)
  105. {
  106. switch (table.Rows[i][j])
  107. {
  108. case Stream s:
  109. {
  110. if (s.Length > 2)
  111. {
  112. using var bmp = new Bitmap(s);
  113. bmp.SetResolution(96, 96);
  114. var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp);
  115. picture.SetPosition(i + 1, 3, j, 5); //设置图片显示位置
  116. var percent = 11000f / bmp.Height;
  117. picture.SetSize((int)percent);
  118. sheet.Row(i + 2).Height = 90;
  119. sheet.Column(j + 1).Width = Math.Max(sheet.Column(j + 1).Width, bmp.Width * percent / 600 > 32 ? bmp.Width * percent / 600 : 32);
  120. }
  121. break;
  122. }
  123. case Bitmap bmp:
  124. {
  125. if (bmp.Width + bmp.Height > 4)
  126. {
  127. bmp.SetResolution(96, 96);
  128. var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp);
  129. picture.SetPosition(i + 1, 3, j, 5); //设置图片显示位置
  130. var percent = 11000f / bmp.Height;
  131. picture.SetSize((int)percent);
  132. sheet.Row(i + 2).Height = 90;
  133. sheet.Column(j + 1).Width = Math.Max(sheet.Column(j + 1).Width, bmp.Width * percent / 600 > 32 ? bmp.Width * percent / 600 : 32);
  134. }
  135. break;
  136. }
  137. case IEnumerable<Stream> streams:
  138. {
  139. double sumWidth = 0;
  140. foreach (var stream in streams.Where(stream => stream.Length > 2))
  141. {
  142. using var bmp = new Bitmap(stream);
  143. bmp.SetResolution(96, 96);
  144. var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp);
  145. picture.SetPosition(i + 1, 3, j, (int)(5 + sumWidth)); //设置图片显示位置
  146. var percent = 11000f / bmp.Height;
  147. picture.SetSize((int)percent);
  148. sheet.Row(i + 2).Height = 90;
  149. sumWidth += bmp.Width * 1.0 * percent / 100 + 5;
  150. sheet.Column(j + 1).Width = Math.Max(sheet.Column(j + 1).Width, sumWidth / 6 > 32 ? sumWidth / 6 : 32);
  151. }
  152. break;
  153. }
  154. case IEnumerable<Bitmap> bmps:
  155. {
  156. double sumWidth = 0;
  157. foreach (var bmp in bmps.Where(stream => stream.Width + stream.Height > 4))
  158. {
  159. bmp.SetResolution(96, 96);
  160. var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp);
  161. picture.SetPosition(i + 1, 3, j, (int)(5 + sumWidth)); //设置图片显示位置
  162. var percent = 11000f / bmp.Height;
  163. picture.SetSize((int)percent);
  164. sheet.Row(i + 2).Height = 90;
  165. sumWidth += bmp.Width * 1.0 * percent / 100 + 5;
  166. sheet.Column(j + 1).Width = Math.Max(sheet.Column(j + 1).Width, sumWidth / 6 > 32 ? sumWidth / 6 : 32);
  167. }
  168. break;
  169. }
  170. case IDictionary<string, Stream> dic:
  171. {
  172. double sumWidth = 0;
  173. foreach (var kv in dic.Where(kv => kv.Value.Length > 2))
  174. {
  175. using var bmp = new Bitmap(kv.Value);
  176. bmp.SetResolution(96, 96);
  177. var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp, new Uri(kv.Key));
  178. picture.SetPosition(i + 1, 3, j, (int)(5 + sumWidth)); //设置图片显示位置
  179. var percent = 11000f / bmp.Height;
  180. picture.SetSize((int)percent);
  181. sheet.Row(i + 2).Height = 90;
  182. sumWidth += bmp.Width * 1.0 * percent / 100 + 5;
  183. sheet.Column(j + 1).Width = Math.Max(sheet.Column(j + 1).Width, sumWidth / 6 > 32 ? sumWidth / 6 : 32);
  184. }
  185. break;
  186. }
  187. case IDictionary<string, MemoryStream> dic:
  188. {
  189. double sumWidth = 0;
  190. foreach (var kv in dic.Where(kv => kv.Value.Length > 2))
  191. {
  192. using var bmp = new Bitmap(kv.Value);
  193. bmp.SetResolution(96, 96);
  194. var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp, new Uri(kv.Key));
  195. picture.SetPosition(i + 1, 3, j, (int)(5 + sumWidth)); //设置图片显示位置
  196. var percent = 11000f / bmp.Height;
  197. picture.SetSize((int)percent);
  198. sheet.Row(i + 2).Height = 90;
  199. sumWidth += bmp.Width * 1.0 * percent / 100 + 5;
  200. sheet.Column(j + 1).Width = Math.Max(sheet.Column(j + 1).Width, sumWidth / 6 > 32 ? sumWidth / 6 : 32);
  201. }
  202. break;
  203. }
  204. case IDictionary<string, Bitmap> bmps:
  205. {
  206. double sumWidth = 0;
  207. foreach (var kv in bmps.Where(kv => kv.Value.Width + kv.Value.Height > 4))
  208. {
  209. kv.Value.SetResolution(96, 96);
  210. var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), kv.Value, new Uri(kv.Key));
  211. picture.SetPosition(i + 1, 3, j, (int)(5 + sumWidth)); //设置图片显示位置
  212. var percent = 11000f / kv.Value.Height;
  213. picture.SetSize((int)percent);
  214. sheet.Row(i + 2).Height = 90;
  215. sumWidth += kv.Value.Width * 1.0 * percent / 100 + 5;
  216. sheet.Column(j + 1).Width = Math.Max(sheet.Column(j + 1).Width, sumWidth / 6 > 32 ? sumWidth / 6 : 32);
  217. }
  218. break;
  219. }
  220. default:
  221. {
  222. sheet.SetValue(i + 2, j + 1, table.Rows[i][j] ?? "");
  223. if (table.Rows[i][j] is ValueType)
  224. {
  225. sheet.Column(j + 1).AutoFit();
  226. }
  227. else
  228. {
  229. // 根据单元格内容长度来自适应调整列宽
  230. sheet.Column(j + 1).Width = Math.Max(Encoding.UTF8.GetBytes(table.Rows[i][j].ToString() ?? string.Empty).Length + 2, sheet.Column(j + 1).Width);
  231. if (sheet.Column(j + 1).Width > 110)
  232. {
  233. sheet.Column(j + 1).AutoFit(100, 110);
  234. }
  235. }
  236. break;
  237. }
  238. }
  239. }
  240. }
  241. //打印方向:纵向
  242. sheet.PrinterSettings.Orientation = eOrientation.Landscape;
  243. //集中在一页里打印
  244. sheet.PrinterSettings.FitToPage = true;
  245. //使用A4纸
  246. sheet.PrinterSettings.PaperSize = ePaperSize.A4;
  247. }
  248. }
  249. }