使用EPPLus库,我试图以这种方式在xlsx单元格中自动调整非常长的字符串:
worksheet.Cells.AutoFitColumns();
行:1000
专栏:10
对于2列,我期望每个单元格中有大约6000个字符。 在这种情况下,AutoFitColumns方法生成以下异常:
消息:GDI +中发生了一般性错误。
StackTrace :at System.Drawing.Graphics.MeasureString(String text,Font font,SizeF layoutArea,StringFormat stringFormat)at System.Drawing.Graphics.MeasureString(String text,Font font,Int32 width,StringFormat格式)at OfficeOpenXml.ExcelRangeBase.AutoFitColumns(Double MinimumWidth, Double MaximumWidth)at OfficeOpenXml.ExcelRangeBase.AutoFitColumns()...
修改
这是我的完整代码:
using (ExcelPackage package = new ExcelPackage(newFile))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet1");
// Setup the first row with headers
for (int j = 0; j < fields.Length; j++)
{
worksheet.Cells[1, j + 1].Value = fields[j];
}
Color colFromHex = ColorTranslator.FromHtml("#B4B4B4");
worksheet.Row(1).Style.Fill.PatternType = ExcelFillStyle.Solid;
worksheet.Row(1).Style.Fill.BackgroundColor.SetColor(colFromHex);
// Insert new rows
for (int i = 0; i < elements.Count; i++)
{
// this will return List<object>
var properties = elements[i].GetPatentProperties();
for (int j = 0; j < properties.Count; j++)
{
worksheet.Cells[i + 2, j + 1].Value = properties[j];
}
}
// This will throw an exception for big number of elements: ~ 1000 element
worksheet.Cells.AutoFitColumns();
worksheet.Cells.Style.WrapText = true;
//worksheet.Cells[autosize].Style.HorizontalAlignment = ExcelHorizontalAlignment.CenterContinuous;
package.Save();
}
请注意,对于相同的元素列表,如果我删除worksheet.Cells.AutoFitColumns();
,我将毫无例外地获取我的xlsx文件。
是否有可能克服这个问题?
答案 0 :(得分:0)
您是否尝试过设置最小和最大音量?
ws.Cells.AutoFitColumns(10, 60);