-1

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>support@XXXX.com</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?

Oleg Sh
  • 8,496
  • 17
  • 89
  • 159
  • 2
    Is that actually an Excel file or is it just an HTML file that you renamed? – jmcilhinney Apr 10 '23 at 03:07
  • @jmcilhinney this is actually an Excel file, even from official government US website, so, I even could not imagine that it can be just renamed HTML file.... – Oleg Sh Apr 10 '23 at 21:54
  • 2
    It looks awfully like an HTML file. Try creating an XLS file yourself and then open both in a text editor to see the difference. If the file is faulty then you should contact the source. Are you sure that you didn't try to download data from a web site and choose the wrong format? – jmcilhinney Apr 11 '23 at 01:32
  • @jmcilhinney no way to select format, proposed excel file with ".xls" extension and inside this HTML :) I'm not expert to excel formats, there are 100500 formats, also excel opens it, therefore I decided that there is one from these excel formats :) In any case, I parse it as simple html, it's ok for now. Thank you anyway ! – Oleg Sh Apr 11 '23 at 01:54

1 Answers1

0

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.

kasprdev
  • 651
  • 1
  • 4
  • 14