excel导出的表格行高根据单元格内容自适应

master
xiaochanghai 10 months ago
parent 6c91d8dd39
commit 086694595d
  1. 13
      Tiobon.Core.Common/Helper/NPOIHelper.cs

@ -308,7 +308,6 @@ public class NPOIHelper
try try
{ {
sheet.SetColumnWidth(columnNum, columnWidth * 300); // 256 sheet.SetColumnWidth(columnNum, columnWidth * 300); // 256
} }
catch (Exception e) catch (Exception e)
{ {
@ -316,6 +315,18 @@ public class NPOIHelper
} }
} }
for (int rowNum = 2; rowNum <= sheet.LastRowNum; rowNum++)
{
IRow currentRow = sheet.GetRow(rowNum);
int length = 25;
foreach (var item in currentRow.Cells)
{
if (item != null)
length = Encoding.UTF8.GetBytes(item.ToString()).Length > length ? Encoding.UTF8.GetBytes(item.ToString()).Length : length;
}
currentRow.HeightInPoints = 35 * (length / 150 + 1);
}
using (MemoryStream ms = new MemoryStream()) using (MemoryStream ms = new MemoryStream())
{ {
workbook.Write(ms); workbook.Write(ms);

Loading…
Cancel
Save