Trying to figure out why I am unable to read a MemoryStream
using ExcelDataReader
. If I hard code the path to the CSV file and pass the stream as a FileStream
it works. My problem is that I am getting the file from Blazor using <InputFile OnChange="LoadFile" accept=".csv,.xlsx,.xls" />
which only provides me an IBrowserFile
, which gives me a Stream, not a FileStream
. When I attempt to pass this in the following code (which works on this file if I pass a FileStream
) I get an "Invalid Signature" exception.
The code:
using (var memoryStream = new MemoryStream())
{
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
// Copy the file data to the memory stream
await browserFile.OpenReadStream().CopyToAsync(memoryStream);
memoryStream.Position = 0; // Reset stream position to the beginning
var config = new ExcelReaderConfiguration
{
FallbackEncoding = System.Text.Encoding.UTF8 // Default encoding
};
// Exception happens here, CreateReader...
using var reader = ExcelReaderFactory.CreateReader(memoryStream, config);
var result = reader.AsDataSet(new ExcelDataSetConfiguration()
{
ConfigureDataTable = _ => new ExcelDataTableConfiguration()
{
UseHeaderRow = true // Use the first row as column names
}
});
}
The body of the exception is:
ExcelDataReader.Exceptions.HeaderException
HResult=0x80131500
Message=Invalid file signature.
Source=ExcelDataReader
StackTrace:
at ExcelDataReader.ExcelReaderFactory.CreateReader(Stream fileStream, ExcelReaderConfiguration configuration)
at CollectXScore.Web.Utilities.ExcelCSVFileReader.<LoadFile>d__1.MoveNext() in mycode.cs:line 44
This exception was originally thrown at this call stack:
[External Code]
When make the call this way it works for the same file:
FileStream source = File.Open("C:\\myfilepath\\" + browserFile.Name, FileMode.Open);
using var reader = ExcelReaderFactory.CreateReader(source);
What am I doing wrong?
Trying to figure out why I am unable to read a MemoryStream
using ExcelDataReader
. If I hard code the path to the CSV file and pass the stream as a FileStream
it works. My problem is that I am getting the file from Blazor using <InputFile OnChange="LoadFile" accept=".csv,.xlsx,.xls" />
which only provides me an IBrowserFile
, which gives me a Stream, not a FileStream
. When I attempt to pass this in the following code (which works on this file if I pass a FileStream
) I get an "Invalid Signature" exception.
The code:
using (var memoryStream = new MemoryStream())
{
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
// Copy the file data to the memory stream
await browserFile.OpenReadStream().CopyToAsync(memoryStream);
memoryStream.Position = 0; // Reset stream position to the beginning
var config = new ExcelReaderConfiguration
{
FallbackEncoding = System.Text.Encoding.UTF8 // Default encoding
};
// Exception happens here, CreateReader...
using var reader = ExcelReaderFactory.CreateReader(memoryStream, config);
var result = reader.AsDataSet(new ExcelDataSetConfiguration()
{
ConfigureDataTable = _ => new ExcelDataTableConfiguration()
{
UseHeaderRow = true // Use the first row as column names
}
});
}
The body of the exception is:
ExcelDataReader.Exceptions.HeaderException
HResult=0x80131500
Message=Invalid file signature.
Source=ExcelDataReader
StackTrace:
at ExcelDataReader.ExcelReaderFactory.CreateReader(Stream fileStream, ExcelReaderConfiguration configuration)
at CollectXScore.Web.Utilities.ExcelCSVFileReader.<LoadFile>d__1.MoveNext() in mycode.cs:line 44
This exception was originally thrown at this call stack:
[External Code]
When make the call this way it works for the same file:
FileStream source = File.Open("C:\\myfilepath\\" + browserFile.Name, FileMode.Open);
using var reader = ExcelReaderFactory.CreateReader(source);
What am I doing wrong?
Looks like this was the IDE being stupid, and potentially the reference to UTF-8. This code did work for the CSV file now:
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
using var source = new MemoryStream();
await file.OpenReadStream().CopyToAsync(source);
source.Position = 0;
using var reader = ExcelReaderFactory.CreateCsvReader(source);
var result = reader.AsDataSet(new ExcelDataSetConfiguration()
{
ConfigureDataTable = _ => new ExcelDataTableConfiguration()
{
UseHeaderRow = true // Use the first row as column names
}
});
Where file is the the IBrowserFile
reference.
FileStream
- can you share the working code? Are you sure the file is really encoded inEncoding.UTF8
or could there be a BOM specifying some other encoding? Also, is there any chance the file is larger than the defaultlong maxAllowedSize = 512000
? – dbc Commented Jan 2 at 19:33ExcelReaderFactory.CreateReader(Stream fileStream, ExcelReaderConfiguration configuration = null)
, it seems to be xls or xlsx files. For CSV files you need to useCreateCsvReader()
. – dbc Commented Jan 2 at 19:45