ExcelExtension.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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)
  24. {
  25. using (var pkg = new ExcelPackage())
  26. {
  27. foreach (var pair in sheetTables)
  28. {
  29. pair.Value.TableName = pair.Key;
  30. CreateWorksheet(pkg, pair.Value);
  31. }
  32. return SaveAsStream(pkg, password);
  33. }
  34. }
  35. /// <summary>
  36. /// 将内存表自动填充到Excel
  37. /// </summary>
  38. /// <param name="tables">内存表</param>
  39. /// <param name="password">密码</param>
  40. /// <returns>内存流</returns>
  41. public static MemoryStream ToExcel(this List<DataTable> tables, string password = null)
  42. {
  43. using var pkg = new ExcelPackage();
  44. foreach (var table in tables)
  45. {
  46. CreateWorksheet(pkg, table);
  47. }
  48. return SaveAsStream(pkg, password);
  49. }
  50. /// <summary>
  51. /// 将内存表自动填充到Excel
  52. /// </summary>
  53. /// <param name="table">内存表</param>
  54. /// <param name="password">密码</param>
  55. /// <returns>内存流</returns>
  56. public static MemoryStream ToExcel(this DataTable table, string password = null)
  57. {
  58. using var pkg = new ExcelPackage();
  59. CreateWorksheet(pkg, table);
  60. return SaveAsStream(pkg, password);
  61. }
  62. private static MemoryStream SaveAsStream(ExcelPackage pkg, string password)
  63. {
  64. var ms = new MemoryStream();
  65. if (!string.IsNullOrEmpty(password))
  66. {
  67. pkg.SaveAs(ms, password);
  68. }
  69. else
  70. {
  71. pkg.SaveAs(ms);
  72. }
  73. return ms;
  74. }
  75. public static void CreateWorksheet(this ExcelPackage pkg, DataTable table)
  76. {
  77. if (string.IsNullOrEmpty(table.TableName))
  78. {
  79. table.TableName = "Sheet1";
  80. }
  81. pkg.Workbook.Worksheets.Add(table.TableName);
  82. var sheet = pkg.Workbook.Worksheets[table.TableName];
  83. // 填充表头
  84. var maxWidth = new int[table.Columns.Count];
  85. for (var j = 0; j < table.Columns.Count; j++)
  86. {
  87. sheet.SetValue(1, j + 1, table.Columns[j].ColumnName);
  88. maxWidth[j] = Encoding.UTF8.GetBytes(table.Columns[j].ColumnName).Length;
  89. }
  90. sheet.Row(1).Style.Font.Bold = true; // 表头设置为粗体
  91. sheet.Row(1).Style.Font.Size = sheet.Row(1).Style.Font.Size * 1.11f; // 表头字号放大1.11倍
  92. sheet.Row(1).CustomHeight = true; // 自动调整行高
  93. sheet.Cells.AutoFitColumns(); // 表头自适应列宽
  94. sheet.Cells.Style.WrapText = true;
  95. // 填充内容
  96. for (var i = 0; i < table.Rows.Count; i++)
  97. {
  98. sheet.Row(i + 2).CustomHeight = true; // 自动调整行高
  99. for (var j = 0; j < table.Columns.Count; j++)
  100. {
  101. switch (table.Rows[i][j])
  102. {
  103. case Stream s:
  104. {
  105. if (s.Length > 2)
  106. {
  107. using var bmp = new Bitmap(s);
  108. bmp.SetResolution(96, 96);
  109. using var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp);
  110. picture.SetPosition(i + 1, 3, j, 5); //设置图片显示位置
  111. var percent = 11000f / bmp.Height;
  112. picture.SetSize((int)percent);
  113. sheet.Row(i + 2).Height = 90;
  114. sheet.Column(j + 1).Width = Math.Max(sheet.Column(j + 1).Width, bmp.Width / 6 > 32 ? bmp.Width / 6 : 32);
  115. }
  116. break;
  117. }
  118. case Bitmap bmp:
  119. {
  120. if (bmp.Width + bmp.Height > 4)
  121. {
  122. bmp.SetResolution(96, 96);
  123. using var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp);
  124. picture.SetPosition(i + 1, 3, j, 5); //设置图片显示位置
  125. var percent = 11000f / bmp.Height;
  126. picture.SetSize((int)percent);
  127. sheet.Row(i + 2).Height = 90;
  128. sheet.Column(j + 1).Width = Math.Max(sheet.Column(j + 1).Width, bmp.Width / 6 > 32 ? bmp.Width / 6 : 32);
  129. }
  130. break;
  131. }
  132. case IEnumerable<Stream> streams:
  133. {
  134. double sumWidth = 0;
  135. foreach (var stream in streams.Where(stream => stream.Length > 2))
  136. {
  137. using var bmp = new Bitmap(stream);
  138. bmp.SetResolution(96, 96);
  139. using var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp);
  140. picture.SetPosition(i + 1, 3, j, (int)(5 + sumWidth)); //设置图片显示位置
  141. var percent = 11000f / bmp.Height;
  142. picture.SetSize((int)percent);
  143. sheet.Row(i + 2).Height = 90;
  144. sumWidth += bmp.Width * 1.0 * percent / 100 + 5;
  145. sheet.Column(j + 1).Width = Math.Max(sheet.Column(j + 1).Width, sumWidth / 6 > 32 ? sumWidth / 6 : 32);
  146. }
  147. break;
  148. }
  149. case IEnumerable<Bitmap> bmps:
  150. {
  151. double sumWidth = 0;
  152. foreach (var bmp in bmps.Where(stream => stream.Width + stream.Height > 4))
  153. {
  154. bmp.SetResolution(96, 96);
  155. using var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp);
  156. picture.SetPosition(i + 1, 3, j, (int)(5 + sumWidth)); //设置图片显示位置
  157. var percent = 11000f / bmp.Height;
  158. picture.SetSize((int)percent);
  159. sheet.Row(i + 2).Height = 90;
  160. sumWidth += bmp.Width * 1.0 * percent / 100 + 5;
  161. sheet.Column(j + 1).Width = Math.Max(sheet.Column(j + 1).Width, sumWidth / 6 > 32 ? sumWidth / 6 : 32);
  162. }
  163. break;
  164. }
  165. case IDictionary<string, Stream> dic:
  166. {
  167. double sumWidth = 0;
  168. foreach (var kv in dic.Where(kv => kv.Value.Length > 2))
  169. {
  170. using var bmp = new Bitmap(kv.Value);
  171. bmp.SetResolution(96, 96);
  172. using var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp, new Uri(kv.Key));
  173. picture.SetPosition(i + 1, 3, j, (int)(5 + sumWidth)); //设置图片显示位置
  174. var percent = 11000f / bmp.Height;
  175. picture.SetSize((int)percent);
  176. sheet.Row(i + 2).Height = 90;
  177. sumWidth += bmp.Width * 1.0 * percent / 100 + 5;
  178. sheet.Column(j + 1).Width = Math.Max(sheet.Column(j + 1).Width, sumWidth / 6 > 32 ? sumWidth / 6 : 32);
  179. }
  180. break;
  181. }
  182. case IDictionary<string, Bitmap> bmps:
  183. {
  184. double sumWidth = 0;
  185. foreach (var kv in bmps.Where(kv => kv.Value.Width + kv.Value.Height > 4))
  186. {
  187. kv.Value.SetResolution(96, 96);
  188. using var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), kv.Value, new Uri(kv.Key));
  189. picture.SetPosition(i + 1, 3, j, (int)(5 + sumWidth)); //设置图片显示位置
  190. var percent = 11000f / kv.Value.Height;
  191. picture.SetSize((int)percent);
  192. sheet.Row(i + 2).Height = 90;
  193. sumWidth += kv.Value.Width * 1.0 * percent / 100 + 5;
  194. sheet.Column(j + 1).Width = Math.Max(sheet.Column(j + 1).Width, sumWidth / 6 > 32 ? sumWidth / 6 : 32);
  195. }
  196. break;
  197. }
  198. default:
  199. {
  200. sheet.SetValue(i + 2, j + 1, table.Rows[i][j] ?? "");
  201. // 根据单元格内容长度来自适应调整列宽
  202. maxWidth[j] = Math.Max(Encoding.UTF8.GetBytes(table.Rows[i][j].ToString() ?? string.Empty).Length, maxWidth[j]);
  203. if (sheet.Column(j + 1).Width < maxWidth[j])
  204. {
  205. sheet.Cells[i + 2, j + 1].AutoFitColumns(18, 110); // 自适应最大列宽,最小18,最大110
  206. }
  207. break;
  208. }
  209. }
  210. }
  211. }
  212. //打印方向:纵向
  213. sheet.PrinterSettings.Orientation = eOrientation.Landscape;
  214. //集中在一页里打印
  215. sheet.PrinterSettings.FitToPage = true;
  216. //使用A4纸
  217. sheet.PrinterSettings.PaperSize = ePaperSize.A4;
  218. }
  219. }
  220. }