EPPLUS AutoFit电池 [英] EPPLUS AutoFit cells
问题描述
如何根据一个输入的最大长度在单元格上设置自动调整大小。
how can i set autosize on my cells, based on the max length of one input.
using (rng = workSheet.Cells["A1:G1"])
{
rng.Style.Font.Bold = true;
rng.Style.Fill.PatternType = ExcelFillStyle.Solid;
rng.Style.Fill.BackgroundColor.SetColor(Color.DarkBlue);
rng.Style.Font.Color.SetColor(Color.White);
}
using (ExcelRange col = workSheet.Cells[2, 6, 7, 7])
{
col.Style.Numberformat.Format = "yyyy-mm-dd HH:mm";
col.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
}
for (int i = 1; i <= a; i++)
{
workSheet.Cells["A1"].Value = "RU_ID";
workSheet.Cells["B1"].Value = "COR_REQ_ID";
workSheet.Cells["C1"].Value = "RU_NAME";
workSheet.Cells["D1"].Value = "PARENT_RU_NAME";
workSheet.Cells["E1"].Value = "ADJUSTMENT_STATE";
workSheet.Cells["F1"].Value = "COR_START";
workSheet.Cells["G1"].Value = "COR_END";
}
...
rng.AutoFitColumns();
string path = @"D:\excel\test.xlsx";
Stream stream = File.Create(path);
excel.SaveAs(stream);
stream.Close();
byte[] data = File.ReadAllBytes(path);
}
AutoFitColumn唯一要做的就是携带单元格到标题的大小,就好像我将标题设为 STH,将输入内容设为 Something good一样, AutofitColumn要比 AutoFitColumn增加大小,而不是 STH来设置大小尺寸。
预先感谢您的帮助。
The only thing that AutoFitColumn is doing is to bring the cell to the size of the header, as if i have the header as "STH" and the inputs as "Something good", "something to increase cell size" than AutoFitColumn will set the size based on "STH" not "something to increase cell size". Thanks in advance for the help.
推荐答案
看一下您的台词:
using (rng = workSheet.Cells["A1:G1"])
...
rng.AutoFitColumns();
注意,您在该范围内称呼 AutoFitColumns
标头的 A1:G1
中,因此EPPlus仅使用这些单元格确定列的宽度。
Notice you are call AutoFitColumns
on the range of of your headers A1:G1
so EPPlus is using only those cells to determine the width of the columns.
只需执行以下操作即可:
Just do this instead:
workSheet.Cells.AutoFitColumns();
因为Epplus中的 Cells
仅包含具有实际值,因此无需担心效率。
since Cells
in Epplus only contain cells with actual values so there is no real concern over efficiency.
这篇关于EPPLUS AutoFit电池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!