Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Wed Aug 26, 2020 1:14 pm

Hello,

I have an Excel File which I think is valid - because opening it with MS Excel triggers no errors and displays all the data as expected. Unfortunately the file can't be opened using workbook.LoadFromStream method. There is a workaround - just hitting the "save" button in MS Excel once and trying againg to load it to Spire works. However, in our automated data processing system such a manual workaround is not feasible.

The file is loaded using the code below:

Code: Select all
            // 1. try "auto-detect"
            try
            {
                workbook.LoadFromStream(inputStream);
                return;
            }
            catch (Exception) { }

            // 2. fallback: try all available Versions
            foreach(ExcelVersion excelVersion in Enum.GetValues(typeof(ExcelVersion)))
            {
                try
                {
                    workbook.LoadFromStream(inputStream, excelVersion);
                    return;
                }
                catch (Exception) { }
            }
            throw new Exception("Excel format is not supported");


Can you tell what the problem is? Is there a bug in Spire or is the original file just broken or uses an unsupported version?

See attached:
test.xlsx - the original file causing the problem
test-opened-saved.xslx - the file after hitting save button once in MS Excel, no problem here

KR,
Gerhard
Attachments
test.zip
(162.3 KiB) Downloaded 158 times

GerhardFinee
 
Posts: 26
Joined: Thu Nov 07, 2019 7:57 am

Thu Aug 27, 2020 1:53 am

Hello,

Thanks for your inquiry.
I tested your scenario and did reproduce your issue. This issue has been logged in our bug tracking system with the ticket SPIREXLS-2694 for further investigation. We will keep you informed if there is any progress. Sorry for inconvenience caused.

Sincerely,
Elena
E-iceblue support team
User avatar

Elena.Zhang
 
Posts: 279
Joined: Thu Jul 23, 2020 1:18 am

Wed Sep 02, 2020 10:15 am

Hello,

Thank you for your patience.
Glad to inform that we just released Spire.XLS Pack(Hotfix) Version:10.9.0 which fixes the issue SPIREXLS-2694, please download it from the following links to test.
Website link: https://www.e-iceblue.com/Download/down ... t-now.html
Nuget link: https://www.nuget.org/packages/Spire.XLS/

Sincerely,
Elena
E-iceblue support team
User avatar

Elena.Zhang
 
Posts: 279
Joined: Thu Jul 23, 2020 1:18 am

Fri Sep 04, 2020 11:18 am

Hi Elena,

wow, that was really fast - thank you for your rapid response, the issue is solved for that particular file type.
However, I have another file type that still is not supported, looks like kind of an old version of XML spreadsheet. Is it possible to get support for the attached format as well?
test.zip
(8.74 KiB) Downloaded 155 times


KR,
Gerhard

GerhardFinee
 
Posts: 26
Joined: Thu Nov 07, 2019 7:57 am

Mon Sep 07, 2020 3:40 am

Hello,

Thanks for your feedback and sorry for the later reply as weekend.
Regarding the new file you provided, I tested it and got the error“Zip exception.Can't locate end of central directory record. Possible wrong file format or archive is corrupt” when loading the file. However, when I open your Excel file directly in Microsoft Excel, the following error message will appear. Our Spire.XLS is based on Microsoft Excel, this issue should be caused by errors in the data in your document itself. Hope you can understand.
16571230707894.png
16571230707894.png (9.53 KiB) Viewed 1678 times

Sincerely,
Elena
E-iceblue support team
User avatar

Elena.Zhang
 
Posts: 279
Joined: Thu Jul 23, 2020 1:18 am

Mon Sep 07, 2020 7:26 am

Hello,

thank you for your response. I am getting the same error message when trying to open it using MS Excel. The first sentence of the message refers to the problem: "The file format and extension of 'test.xls' don't match". You can open the file without error message when you change the file extension to ".xml". The data is neither corrupted nor unsafe, it's possible to open the file and view the contents when hitting "yes" on the message. We have a few more examples of this file format. It's "SpreadsheetML", an XML based Office 2003 format, which apparently does not find wide use - see https://en.wikipedia.org/wiki/SpreadsheetML. You can see it in the header of the file, if you open it with a plain text editor:

Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="http://www.w3.org/TR/REC-html40">


The data contents look something like this:
Code: Select all
   <Row>
    <Cell><Data ss:Type="String">DUMMY</Data></Cell>
    <Cell><Data ss:Type="String">DUMMY</Data></Cell>
    <Cell><Data ss:Type="String">DUMMY</Data></Cell>
    <Cell><Data ss:Type="String">DUMMY</Data></Cell>
    <Cell><Data ss:Type="String">DUMMY</Data></Cell>
    <Cell><Data ss:Type="String">DUMMY</Data></Cell>
    <Cell><Data ss:Type="String">DUMMY</Data></Cell>
    <Cell><Data ss:Type="String">DUMMY</Data></Cell>
    <Cell ss:StyleID="s_currency2RPT"><Data ss:Type="Number">0E0</Data></Cell>
    <Cell ss:StyleID="s_currency2RPT"><Data ss:Type="Number">0E0</Data></Cell>
    <Cell ss:StyleID="s_currency2RPT"><Data ss:Type="Number">0E0</Data></Cell>
    <Cell ss:StyleID="s_currency4RPT"><Data ss:Type="Number">0E0</Data></Cell>
    <Cell ss:StyleID="s_currency2RPT"><Data ss:Type="Number">0E0</Data></Cell>
    <Cell ss:StyleID="s_currency2RPT"><Data ss:Type="Number">0E0</Data></Cell>
    <Cell ss:StyleID="s_currency2RPT"><Data ss:Type="Number">0E0</Data></Cell>
    <Cell ss:StyleID="s_currency2RPT"><Data ss:Type="Number">0E0</Data></Cell>
    <Cell ss:StyleID="s_currency2RPT"><Data ss:Type="Number">0E0</Data></Cell>
    <Cell ss:StyleID="s_currency2RPT"><Data ss:Type="Number">0E0</Data></Cell>
    <Cell ss:StyleID="s_currency6RPT"><Data ss:Type="Number">0E0</Data></Cell>
    <Cell ss:StyleID="s_percent2"><Data ss:Type="Number">0E0</Data></Cell>
    <Cell ss:StyleID="s_currency2RPT"><Data ss:Type="Number">0E0</Data></Cell>
    <Cell ss:StyleID="s_currency4RPT"><Data ss:Type="Number">0E0</Data></Cell>
    <Cell><Data ss:Type="String">DUMMY</Data></Cell>
    <Cell><Data ss:Type="String">DUMMY</Data></Cell>
   </Row>


Is there a chance you could support "SpreadsheetML" files?

KR,
Gerhard

GerhardFinee
 
Posts: 26
Joined: Thu Nov 07, 2019 7:57 am

Mon Sep 07, 2020 9:47 am

Hello,

Thank you for your prompt reply.
After further analysis, we found that your document is actually an xml format document, not an xls format document. You can try to manually change the suffix of your document to .xml (test.xls -> test.xml), and open it in Microsoft Excel, then you will find that there will be no more error messages. In the meantime, I tried to use the LoadFromXml to load your file and found that it works fine. You can also try it on your side.

Code: Select all
 workbook.LoadFromXml("test.xls");


Sincerely,
Elena
E-iceblue support team
User avatar

Elena.Zhang
 
Posts: 279
Joined: Thu Jul 23, 2020 1:18 am

Mon Sep 07, 2020 11:53 am

Hello,

thank you for your input. Unfortunately I can't use LoadFromXml(filename) overload because I need to work with streams - but LoadFromXml(stream) works - under certain curcumstances. So in principle, my problem is solved.

Just to give you a hint for improving your solution: as I stated in my initial post, I am using two-step algorithm for opening ANY given Excel file:

Code: Select all
            // 1. try "auto-detect"
            try
            {
                workbook.LoadFromStream(inputStream);
                return;
            }
            catch (Exception) { }

            // 2. fallback: try all available Versions
            foreach(ExcelVersion excelVersion in Enum.GetValues(typeof(ExcelVersion)))
            {
                try
                {
                    workbook.LoadFromStream(inputStream, excelVersion);
                    return;
                }
                catch (Exception) { }
            }
            throw new Exception("Excel format is not supported");


As XML-based spreadsheets need to be opened using LoadFromXml(stream) obviously, I tried to add a third step at the end of my algorithm:

Code: Select all
            // 1. try "auto-detect"
            try
            {
                workbook.LoadFromStream(inputStream);
                return;
            }
            catch (Exception) { }

            // 2. fallback: try all available Versions
            foreach (ExcelVersion excelVersion in Enum.GetValues(typeof(ExcelVersion)))
            {
                try
                {
                    workbook.LoadFromStream(inputStream, excelVersion);
                    return;
                }
                catch (Exception) { }
            }

            // 3. fallback: try load as XML
            try
            {
                workbook.LoadFromXml(inputStream);
                return;
            }
            catch (Exception) { }

            throw new Exception("Excel format is not supported");


For some reason this doesn't work, LoadFromXml method fails with a NullReferenceException somewhere inside. It turns out that reordering the fallback steps solves my problem - trying LoadFromXml upfront enables me to open XML files PLUS it does not break opening all other Excel files. I can't see where the problem is, but I guess there's an issue with the position of the stream or the state of the workbook object after a failed attempt to call workbook.LoadFromStream(stream).

What works for me now is the following:
Code: Select all
            // 1. try load as XML
            try
            {
                workbook.LoadFromXml(inputStream);
                return;
            }
            catch (Exception) { }

            // 2. fallback: try "auto-detect"
            try
            {
                workbook.LoadFromStream(inputStream);
                return;
            }
            catch (Exception) { }

            // 3. fallback: try all available Versions
            foreach (ExcelVersion excelVersion in Enum.GetValues(typeof(ExcelVersion)))
            {
                try
                {
                    workbook.LoadFromStream(inputStream, excelVersion);
                    return;
                }
                catch (Exception) { }
            }

            throw new Exception("Excel format is not supported");


For my current problem no further action on your side is required, as I have the workaround in place with the "correct" order of opening attempts. However, in a perfect world all these steps should not be necessary and workbook.LoadFromStream(stream) would simply try all possible file formats INCLUDING XML based spreadsheets.

KR,
Gerhard

GerhardFinee
 
Posts: 26
Joined: Thu Nov 07, 2019 7:57 am

Tue Sep 08, 2020 8:57 am

Hello,

Thanks for your feedback and suggestion.
Sorry that currently our LoadfromStream method only supports loading files in the following formats. We are very sorry for the inconvenience caused.
164309217693124.png
164309217693124.png (10.28 KiB) Viewed 1647 times


Sincerely,
Elena
E-iceblue support team
User avatar

Elena.Zhang
 
Posts: 279
Joined: Thu Jul 23, 2020 1:18 am

Return to Spire.XLS