我有一张包含许多图像的表,并希望调整行高 在这篇文章中(尚未回答)Epplus row height issue有人遇到了相同的性能问题。
要解决此问题,请考虑全局设置行高并禁用每行的自定义Rowheight。
但是例如Row(4).CustomHeight = false;
不会使用DefaultHeigth ...它只会调整高度以适应文本。
问题: 有没有办法在EPPlus甚至OpenXML中实现我想要的东西(教导一行从默认高度,插入文本后取高度),而不是性能问题?
我写了一个用于复制的NUnit测试(在安装了excel的Windows 10上测试过)。我真正的应用程序所有图像都不同,这只是为了测试。
[Test]
public void SettingRowHeight() {
ExcelPackage pkg = new ExcelPackage();
ExcelWorksheet sheet = pkg.Workbook.Worksheets.Add("Test");
Stopwatch sw = new Stopwatch();
sw.Start();
Bitmap bmp = new Bitmap(100,100);
int targetcolindex = 21;
// adjust for scaling the problem 100 (10sec)is OK
// 500 will slow drastically(~19min)
int rocount = 500;
//Inserting Test Data
for (int i = 1; i < rocount; i++)
{
for (int k = 1; k < 20; k++)
{
sheet.Cells[i, k].Value = "test" + i;
}
var picture = sheet.Drawings.AddPicture("pic" + i.ToString() + targetcolindex.ToString(), bmp);
picture.From.Column = targetcolindex - 1;
picture.From.Row = i - 1;
picture.From.ColumnOff = 2 * 9525;
picture.From.RowOff = 2 * 9525;
}
Console.WriteLine($"Inserting {rocount} Cells: {sw.ElapsedMilliseconds} ms");
sw.Stop();
sw.Start();
for (int i = 1; i < rocount; i++)
{
sheet.Row(i).Height = 50;
}
Console.WriteLine($"Set Height {rocount} Cells: {sw.ElapsedMilliseconds} ms");
sw.Stop();
sw.Restart();
sheet.Row(1).Height = 30;
Console.WriteLine($"Set Height Row 1: {sw.ElapsedMilliseconds} ms");
sw.Restart();
sheet.Row(2).Height = 30;
Console.WriteLine($"Set Height Row 2: {sw.ElapsedMilliseconds} ms");
sw.Restart();
sheet.Row(3).Height = 30;
Console.WriteLine($"Set Height Row 3: {sw.ElapsedMilliseconds} ms");
sw.Restart();
sheet.Row(rocount).Height = 30;
Console.WriteLine($"Set Height Row {rocount}: {sw.ElapsedMilliseconds} ms");
// trying to set the rowHeight globally so that above code can be skipped
sheet.DefaultRowHeight = 50;
// just for checking if something has changed
sheet.Row(4).Height = 70;
// this should remove the height and set it to Default value? if not: is there a way to do that?
sheet.Row(4).CustomHeight = false;
// this doesnt work either
sheet.Row(6).Height = sheet.DefaultRowHeight;
sheet.Row(6).CustomHeight = false;
// for looking over the result visually
var filename = Path.GetTempFileName()+".xlsx";
pkg.SaveAs(new System.IO.FileInfo(filename));
System.Diagnostics.Process.Start(filename);
}
[Test]
public void SettingRowHeight() {
ExcelPackage pkg = new ExcelPackage();
ExcelWorksheet sheet = pkg.Workbook.Worksheets.Add("Test");
Stopwatch sw = new Stopwatch();
sw.Start();
Bitmap bmp = new Bitmap(100,100);
int targetcolindex = 21;
// adjust for scaling the problem 100 (10sec)is OK
// 500 will slow drastically(~19min)
int rocount = 500;
//Inserting Test Data
for (int i = 1; i < rocount; i++)
{
for (int k = 1; k < 20; k++)
{
sheet.Cells[i, k].Value = "test" + i;
}
var picture = sheet.Drawings.AddPicture("pic" + i.ToString() + targetcolindex.ToString(), bmp);
picture.From.Column = targetcolindex - 1;
picture.From.Row = i - 1;
picture.From.ColumnOff = 2 * 9525;
picture.From.RowOff = 2 * 9525;
}
Console.WriteLine($"Inserting {rocount} Cells: {sw.ElapsedMilliseconds} ms");
sw.Stop();
sw.Start();
for (int i = 1; i < rocount; i++)
{
sheet.Row(i).Height = 50;
}
Console.WriteLine($"Set Height {rocount} Cells: {sw.ElapsedMilliseconds} ms");
sw.Stop();
sw.Restart();
sheet.Row(1).Height = 30;
Console.WriteLine($"Set Height Row 1: {sw.ElapsedMilliseconds} ms");
sw.Restart();
sheet.Row(2).Height = 30;
Console.WriteLine($"Set Height Row 2: {sw.ElapsedMilliseconds} ms");
sw.Restart();
sheet.Row(3).Height = 30;
Console.WriteLine($"Set Height Row 3: {sw.ElapsedMilliseconds} ms");
sw.Restart();
sheet.Row(rocount).Height = 30;
Console.WriteLine($"Set Height Row {rocount}: {sw.ElapsedMilliseconds} ms");
// trying to set the rowHeight globally so that above code can be skipped
sheet.DefaultRowHeight = 50;
// just for checking if something has changed
sheet.Row(4).Height = 70;
// this should remove the height and set it to Default value? if not: is there a way to do that?
sheet.Row(4).CustomHeight = false;
// this doesnt work either
sheet.Row(6).Height = sheet.DefaultRowHeight;
sheet.Row(6).CustomHeight = false;
// for looking over the result visually
var filename = Path.GetTempFileName()+".xlsx";
pkg.SaveAs(new System.IO.FileInfo(filename));
System.Diagnostics.Process.Start(filename);
}
编辑: 我当前的解决方法:在插入任何图像之前设置rowheigth。 即使删除所有图像,设置高度,重新添加图像也会从文档中的特定图像数开始变得更快。