GDI+ Error When Loading Excel File?!?

Hi Adrian,

I have a problem opening an Excel file.  The video shows the error:

http://screencast.com/t/5iYfUqrl

Is GDI+ really required to open a file?  Can a substitute font be used if a font is encountered which is not installed?

Thanks,

Steve

Steve,


Normally GDI+ (or GDI) is not needed to open a file. But in some cases it will be needed.

First some background: Of course FlexCel tries to minimize graphic calls, and it also is platform independent (It can run on a mac where it uses CoreGraphics instead of GDI+), but well, sadly Excel mixes a lot of UI code with non UI code, and sometimes we don't have other resource than to do similar. 

The classical example here is when you autofit, even when this is a common Excel thing, to do autofit right we need to mix UI code to calculate the text extent in the different fonts, with the given kerning, etc.

Now, in your example, what happens is that the "normal" font isn't found. And just to clarify, normal font isn't Arial, because when it is, FlexCel doesn't even bother crating an UI font, it already knows Arial's metrics. (Or calibri).

But if the "normal" font isn't arial/calibri, FlexCel needs to retrieve the font metrics, because this font is used in amny parts of the Excel specification. For starters, column width is measured in "how many "0" in the normal font can you fit in the column". A column width of "8" would mean you can write "00000000" in the cell, in normal font. (It isn't as simple as that, there are cell margins too, and double borders increase column widht, but in short, this is the idea).

Also the units used in xls and xlsx are different so we need to convert them (with a very complex reversed engineered algorithm that took me some weeks to figure out, since it isn't documented in the 6000+ pages of xlsx doc). While it is simple to do it approximately for the standard arial (Excel 2003) and calibri(Excel 2007), to exactly convert fro any normal font out there is complex. And if conversion isn't exact, by opening and saving a file many times you would end up shrinking or growing the columns.

For this conversion in particular is that we need to find how fat is a "0" in this normal font. (As you can see in the video, the method with the error is "GetFont0Width", it is trying to get the width of a "0" string in the normal font).

Now, some questions:
1)In the video, you press "break" when the exception shows. But, what happens if you press continue? 
It should continue and load the file. This exception should only happen at debug time, but it isn't a "real" exception, it is handled and the program should work just fine. Of course, FlexCel won't be using whatever font you had as "normal" to calculate column widths, and it will be replaced but whatever Windows considers to be a good substitute font for it, but unless it is a really fancy font, it shouldn't matter much. You might see some differences in column widths if you save the same file as xls and xlsx, because as said, FlexCel will be using a substitute font. Same if you do some autofitting. 

But the exception should be handled, just press "continue" instead of "break". And when running outside the debugger, you won't see any error, file will load ok.

2)Note that even when this isn't a serious error, it is an error (whatever font you have in normal style isn't in your machine), so while FlexCel will continue, it will also log an error to FlexCelTrace. 
If you want to be notified of non-fatal errors like this one, you can put a listener in FlexCelTrace. (Look for a demo: 
Demo\Delphi\Modules\10.API\X0.Handling Errors ). If you need more help in setting up a flexCleTrace listener let me know. But note that you don't need to put any listener, by default this error will be ignored because it isn't consider serious.

3)To know the "normal" font you have, you can  click in the ribbon, "Cell Styles", right click on normal and choose "Modify". It should display what you have as "normal" font. If it is a font you have, it might be because this was modified with some third party tool that wrote the "normal" style definition different from the "normal" cell definition. (They are actually different, and you could via FlexCel or other tool change them to have different values, but you shouldn't). Excel won't let you.

Hi Adrian,


It does indeed continue.  Thanks for the clarification!  When examining the font it appears to be "MS San Serif".  All good now,

Thanks again,

Steve