EPPLUS AutoFit细胞

And*_*aul 3 c# excel epplus visual-studio-2013

如何根据一个输入的最大长度在我的单元格上设置自动调整大小.

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);

}
Run Code Online (Sandbox Code Playgroud)

AutoFitColumn唯一要做的就是将单元格设置为标题的大小,就好像我的标题为"STH",输入为"Something good","增加单元格大小的东西"比AutoFitColumn将设置基于"STH"的大小不是"增加细胞大小的东西".在此先感谢您的帮助.

Ern*_*e S 16

看看你的台词:

using (rng = workSheet.Cells["A1:G1"])
...
rng.AutoFitColumns();
Run Code Online (Sandbox Code Playgroud)

请注意,您调用AutoFitColumns了标题的范围,A1:G1因此EPPlus仅使用这些单元格来确定列的宽度.

只需这样做:

workSheet.Cells.AutoFitColumns();
Run Code Online (Sandbox Code Playgroud)

因为Cells在Epplus中只包含具有实际值的单元格,所以对效率没有真正的关注.