c# - EPPLUS AutoFit 细胞

标签 c# excel visual-studio-2013 epplus

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

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”,输入是“好东西”,“增加单元格大小的东西”比 AutoFitColumn将根据“STH”而不是“增加单元格大小的东西”来设置大小。 预先感谢您的帮助。

最佳答案

看看你的台词:

using (rng = workSheet.Cells["A1:G1"])
...
rng.AutoFitColumns();

请注意,您正在对 header A1:G1 的范围调用 AutoFitColumns,因此 EPPlus 仅使用这些单元格来确定列的宽度。

只需这样做:

workSheet.Cells.AutoFitColumns();

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

关于c# - EPPLUS AutoFit 细胞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36794133/

相关文章:

c# - System.Data.Sqlite 中的事务处理

c# - 如何在不违反单一职责原则的情况下编写类似的测试用例?

c# - 414 - 请求的 uri 太长 - url 长度为 70 个字符

excel - 如何清除除 X、Y 和 Z 列和 A、B、C 行之外的所有数据?

python - 如何使用 Iron Python 设计 Windows 窗体应用程序

git - Visual Studio 2013 Git 版本控制 - SSH key

c# - 调试从 C# 调用的 VC++ 6 DLL

python - xlrd 读取 xls XLRDError : Unsupported format, 或损坏的文件:预期的 BOF 记录;发现 '\r\n<html>'

c# - User.Identity.Name - 哪个程序集引用将其引入您的项目?

python - 用win32com打开excel提示 "Name cannot be the same as built-in name"我手动打开不提示