EPPlus中的自适应行

5
我无法找到使用EPPlus Excel库自适应行高的方法。当我使用Excel Interop时,可以使用sheet.Rows.AutoFit()。我已经使用ILSpy查看了接口,但是目前为止我没有找到任何有用的东西。有什么解决方法吗?还是我漏掉了什么?
更新:第4行出现问题:
我尝试将sheet.Row(4).CustomHeight设置为false,但没有成功... 我还尝试在加载单元格内容之前和之后设置该属性,但结果仍然相同。它确实会将行高调整为不同的值,但这不是一个好的值。也许EPPlus在测量字符串高度方面存在问题?我知道当我弄乱一些遗留的GDI+代码并需要测量字符串时,我确实遇到了很多问题...
2个回答

12

实际上,如果您查看行对象的属性CustomHeight,您会发现它默认为false。这意味着Excel在打开时将自动设置行高度(应该如此)。如果您想停止这样做,您可以将其设置为false或手动设置行高度,这将自动将其设置为false

唯一的问题是,如果您依赖行的自适应大小,则无法知道文件在EPPlus中构建时该高度将是多少,因为Excel会在打开文件时决定它。这有点像如果您使用AutoFitColumn函数,那么您无法知道列宽度将是多少。

这演示了该属性的逻辑:

[TestMethod]
public void Row_Height_Test()
{
    //https://dev59.com/yY3da4cB1Zd3GeqPvyiv
    //Throw in some data
    var datatable = new DataTable("tblData");
    datatable.Columns.AddRange(new[]
    {
        new DataColumn("Col1", typeof (int)),
        new DataColumn("Col2", typeof (int)),
        new DataColumn("Col3", typeof (int))
    });


    for (var i = 0; i < 20; i++)
    {
        var row = datatable.NewRow();
        row[0] = i;
        row[1] = i * 10;
        row[2] = i * 100;
        datatable.Rows.Add(row);
    }

    var existingFile2 = new FileInfo(@"c:\temp\temp.xlsx");
    if (existingFile2.Exists)
        existingFile2.Delete();

    using (var package = new ExcelPackage(existingFile2))
    {
        //Add the data
        var ws = package.Workbook.Worksheets.Add("Sheet1");
        ws.Cells.LoadFromDataTable(datatable, true);

        //CustomHeight is set to false by default on all rows and giving such a large font 
        //will cause it to autosize to a bigger height by Excel when first opened
        ws.Row(1).Style.Font.Size = 20;

        //Setting the height manually will automatically set CustomHeight to true
        //preventing excel from automatically setting the height
        ws.Row(2).Height = 30;
        ws.Row(2).Style.Font.Size = 20;

        //row 1 height will be around 26 when opened in Excel (but cannot know that now), 
        //row 2 height will be 30 as set above, 
        //row 3 height will be the DefaultHeight (usually 15) of the worksheet since it can fit the default font
        Console.WriteLine("{{{0} : {1}}}", ws.Row(1).Height, ws.Row(1).CustomHeight);
        Console.WriteLine("{{{0} : {1}}}", ws.Row(2).Height, ws.Row(2).CustomHeight);
        Console.WriteLine("{{{0} : {1}}}", ws.Row(3).Height, ws.Row(3).CustomHeight);

        //Save the file
        package.Save();
    }
}

这是控制台日志输出:

{15 : False}
{30 : True}
{15 : False}

Excel Rendered Output


我尝试将CustomHeight = false设置为给我问题的行,但它没有起作用。这可能是因为文本不在第一列吗? - dario_ramos
@dario_ramos 这很有趣。如果将CustomHeight设置为true,Epplus不会计算高度,Excel会完成所有工作,但似乎它没有正确地完成。你能否发布更多的代码 - 单元测试可能有助于隔离问题。 - Ernie S
我已经将填充并为第4行设置样式的代码隔离出来,它可以正常工作...但是与其他部分存在一些奇怪的交互。我会逐个添加并查看是否发现冲突。 我敢打赌是我在其上方插入的图片引起的。 - dario_ramos
这个问题很有可能是由图片引起的。请参考此处所述的EditAs设置:https://dev59.com/Ml4c5IYBdhLWcg3weqW9。 - Ernie S

8

还有一个让我花费数小时才想明白的问题:如何在给定的列或行中启用单词换行。这是我如何做到的:

(水平和垂直对齐已设置为“居中”)

      // Increase the height of the header row by .5
      // (The height is already at 2 times the default value)
      wsDT.Row(1).Height *= 1.5;

      // Column index to Notes, Anniversary Month and Day
      //   add "1" since column index is relative to 0
      int colIdx = dt.Columns.IndexOf("Notes") + 1;

      // Set the column width 
      // This is a long text field - so width and word wrap are set
      wsDT.Column(colIdx).Width = 50;
      wsDT.Column(colIdx).Style.WrapText = true;

      // Set width of anniversary month column
      // Purpose here is to wrap header text 
      colIdx = dt.Columns.IndexOf("Lease Anniversary Month") + 1;
      wsDT.Column(colIdx).Width = 15;
      wsDT.Column(colIdx).Style.WrapText = true;

标题行显示自动换行和设置宽度 “备注”行的内容在适当时自动换行


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接