我找不到使用EPPlus Excel库自动调整行高的方法.当我使用Excel Interop时,我能做到sheet.Rows.AutoFit().我正在使用ILSpy查看界面,但到目前为止我没有找到任何有用的东西.是否有一些解决方法,或者我错过了什么?
更新:第4行给我带来了问题:

我试过做sheet.Row(4).CustomHeight = false,但它不起作用...我也尝试在加载单元格内容之前和之后设置属性,它仍然是相同的.它确实将行高调整为不同的值,但它不是一个好的值.也许EPPlus有测量弦高的问题?我知道当我弄乱一些传统的GDI +代码时我确实遇到了很多问题,我需要测量字符串...
实际上,如果查看CustomHeight行对象的属性,默认情况下会看到它设置为false.这意味着Excel将(应该)在打开时自动设置行的高度.如果你想停止它,你可以将其设置为false或手动设置行高度,这将自动将其设置为false.
唯一的缺点是,如果您依赖自动调整行,那么您无法知道在EPPlus中构建时的高度,因为Excel将在第一次打开文件时决定.有点像你不知道如果你使用该AutoFitColumn功能,列宽将是什么.
这证明了属性的逻辑:
[TestMethod]
public void Row_Height_Test()
{
//http://stackoverflow.com/questions/31496172/autofit-rows-in-epplus
//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();
}
}
Run Code Online (Sandbox Code Playgroud)
这是控制台日志输出:
{15 : False}
{30 : True}
{15 : False}
Run Code Online (Sandbox Code Playgroud)

小智 5
我花了好几个小时才弄清楚的另一个问题是:如何在给定的列或行中启用自动换行。这是我的方法:
(水平和垂直对齐方式已设置为“居中”)
// 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;
Run Code Online (Sandbox Code Playgroud)
标题行显示换行文本和设置的宽度 “注释”行的内容在适当时具有换行文本
| 归档时间: |
|
| 查看次数: |
15243 次 |
| 最近记录: |