ExcelDataReader, can't read "html-format" file

103 views Asked by At

I have the following code:

        MemoryStream ms = new MemoryStream();
        using (var fileStream = File.OpenRead("G:\\tmp\\abc.xls"))
        {
            fileStream.CopyTo(ms);
        }
        var reader = ExcelReaderFactory.CreateReader(ms);

where file abc.xls has the following format:

<div>
    <table cellspacing="0" rules="all" border="1" style="border-collapse:collapse;">
        <tr>
            <th scope="col">Device Name</th><th scope="col">Model Number</th><th scope="col">Software Version</th><th scope="col">ELD Identifier</th><th scope="col">Company Name</th><th scope="col">Phone</th><th scope="col">Email</th><th scope="col">Website</th><th scope="col">Mailing Address</th><th scope="col">City, State, and Zip</th><th scope="col">Data Transfer Options</th>
        </tr><tr>
            <td>XXX ELD - iOS 2.0</td><td>20I</td><td>Version 3 and higher</td><td>20XX03</td><td>J. J. XX&amp; Associates, Inc.</td><td>XXX-XXX-XXXX</td><td>[email protected]</td><td>www.XXX.com/elogs</td><td>3003 XXXLane</td><td>Neenah, WI 54957</td><td>Telematic (Web Service, Email)</td>
        </tr><tr>
        ......
        </tr>
</table>

but I got an error:

ExcelDataReader.Exceptions.HeaderException: Invalid file signature. [2023-04-10T02:43:50.481Z] at ExcelDataReader.ExcelReaderFactory.CreateReader(Stream fileStream, ExcelReaderConfiguration configuration)

what is wrong in my case and how to fix?

1

There are 1 answers

1
kasprdev On

You are putting HTML content in a .XLS file, which is not compatible. Changing the extension on a file doesn't automatically format it to that file type. Using a correctly-formatted .XLS file with your code should work just fine.