1
1

ExcelExtension.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.Text;
  8. namespace Masuit.Tools.Excel
  9. {
  10. public static class ExcelExtension
  11. {
  12. static ExcelExtension()
  13. {
  14. ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
  15. }
  16. /// <summary>
  17. /// 将内存表自动填充到Excel
  18. /// </summary>
  19. /// <param name="sheetTables">sheet名和内存表的映射</param>
  20. /// <param name="password">密码</param>
  21. /// <returns>内存流</returns>
  22. public static MemoryStream DataTableToExcel(this Dictionary<string, DataTable> sheetTables, string password = null)
  23. {
  24. using (var pkg = new ExcelPackage())
  25. {
  26. foreach (var pair in sheetTables)
  27. {
  28. pair.Value.TableName = pair.Key;
  29. CreateWorksheet(pkg, pair.Value);
  30. }
  31. return SaveAsStream(pkg, password);
  32. }
  33. }
  34. /// <summary>
  35. /// 将内存表自动填充到Excel
  36. /// </summary>
  37. /// <param name="tables">内存表</param>
  38. /// <param name="password">密码</param>
  39. /// <returns>内存流</returns>
  40. public static MemoryStream DataTableToExcel(this List<DataTable> tables, string password = null)
  41. {
  42. using var pkg = new ExcelPackage();
  43. foreach (var table in tables)
  44. {
  45. CreateWorksheet(pkg, table);
  46. }
  47. return SaveAsStream(pkg, password);
  48. }
  49. /// <summary>
  50. /// 将内存表自动填充到Excel
  51. /// </summary>
  52. /// <param name="table">内存表</param>
  53. /// <param name="password">密码</param>
  54. /// <returns>内存流</returns>
  55. public static MemoryStream ToExcel(this DataTable table, string password = null)
  56. {
  57. using var pkg = new ExcelPackage();
  58. CreateWorksheet(pkg, table);
  59. return SaveAsStream(pkg, password);
  60. }
  61. private static MemoryStream SaveAsStream(ExcelPackage pkg, string password)
  62. {
  63. var ms = new MemoryStream();
  64. if (!string.IsNullOrEmpty(password))
  65. {
  66. pkg.SaveAs(ms, password);
  67. }
  68. else
  69. {
  70. pkg.SaveAs(ms);
  71. }
  72. return ms;
  73. }
  74. public static void CreateWorksheet(this ExcelPackage pkg, DataTable table)
  75. {
  76. if (string.IsNullOrEmpty(table.TableName))
  77. {
  78. table.TableName = "Sheet1";
  79. }
  80. pkg.Workbook.Worksheets.Add(table.TableName);
  81. var sheet = pkg.Workbook.Worksheets[table.TableName];
  82. // 填充表头
  83. for (var j = 0; j < table.Columns.Count; j++)
  84. {
  85. sheet.SetValue(1, j + 1, table.Columns[j].ColumnName);
  86. }
  87. sheet.Row(1).Style.Font.Bold = true; // 表头设置为粗体
  88. sheet.Row(1).Style.Font.Size = sheet.Row(1).Style.Font.Size * 1.11f; // 表头字号放大1.11倍
  89. sheet.Row(1).CustomHeight = true; // 自动调整行高
  90. sheet.Cells.AutoFitColumns(); // 表头自适应列宽
  91. sheet.Cells.Style.WrapText = true;
  92. // 填充内容
  93. for (var i = 0; i < table.Rows.Count; i++)
  94. {
  95. sheet.Row(i + 2).CustomHeight = true; // 自动调整行高
  96. for (var j = 0; j < table.Columns.Count; j++)
  97. {
  98. if (table.Rows[i][j] is Stream s)
  99. {
  100. if (s.Length > 0)
  101. {
  102. using var img = Image.FromStream(s);
  103. using var bmp = new Bitmap(img);
  104. bmp.SetResolution(96, 96);
  105. using var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp);
  106. picture.SetPosition(i + 1, 3, j, 5); //设置图片显示位置
  107. sheet.Row(i + 2).Height = 90;
  108. sheet.Column(j + 1).Width = 30;
  109. }
  110. }
  111. else
  112. {
  113. sheet.SetValue(i + 2, j + 1, table.Rows[i][j] ?? "");
  114. // 根据单元格内容长度来自适应调整列宽
  115. var maxWidth = Encoding.UTF8.GetBytes(table.Rows[i][j].ToString()).Length;
  116. if (sheet.Column(j + 1).Width < maxWidth)
  117. {
  118. sheet.Cells[i + 2, j + 1].AutoFitColumns(18, 110); // 自适应最大列宽,最小18,最大110
  119. }
  120. }
  121. }
  122. }
  123. //打印方向:纵向
  124. sheet.PrinterSettings.Orientation = eOrientation.Landscape;
  125. //集中在一页里打印
  126. sheet.PrinterSettings.FitToPage = true;
  127. //使用A4纸
  128. sheet.PrinterSettings.PaperSize = ePaperSize.A4;
  129. }
  130. }
  131. }