Skip to content
This repository has been archived by the owner on Mar 9, 2020. It is now read-only.

AutoFitColumns on long data #445

Open
Errors4l opened this issue Apr 5, 2019 · 2 comments
Open

AutoFitColumns on long data #445

Errors4l opened this issue Apr 5, 2019 · 2 comments
Assignees

Comments

@Errors4l
Copy link

Errors4l commented Apr 5, 2019

Write a detailed description of your issue.
When trying to call AutoFitColumns on a range that includes data exceeding ~32000 characters, Graphics.MeasureStrings throws a generic GDI+ exception. This applies even when supplying the method with a max length.

System.Runtime.InteropServices.ExternalException
  HResult=0x80004005
  Message=A generic error occurred in GDI+.
  Source=System.Drawing.Common
  StackTrace:
   at System.Drawing.Graphics.MeasureString(String text, Font font, SizeF layoutArea, StringFormat stringFormat)
   at OfficeOpenXml.ExcelRangeBase.AutoFitColumns(Double MinimumWidth, Double MaximumWidth)

var size = g.MeasureString(t, f, 10000, StringFormat.GenericDefault);

Attach a test to reproduce your issue, if it is a bug or unexpected behaviour. This is very important.
Apologies for incomplete code. I can't share the code I'm working with. This seems to be all that's necessary to reproduce the bug.

ws.Cells[1, 1].Value = new string('a', 50000);
ws.Cells[1, 1].AutoFitColumns();

Related

JanKallman pushed a commit that referenced this issue Apr 10, 2019
@JanKallman
Copy link
Owner

Thanks, added a fix that truncates the string to 32000 chars before using it as a parameter to MeasureString.

@JanKallman JanKallman self-assigned this Apr 10, 2019
@justin-oj-orange
Copy link

@JanKallman Has this fully made it into the repo yet, we are using v4.5.3.1. If not (not implying anything) is there an easy work around, apart from truncating every cell value we supply of course

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants