调用ExcelReaderFactory.CreateOpenXmlReader(stream)二次执行抛出空值异常咨询
System.ArgumentNullException on repeated calls to ExcelReaderFactory.CreateOpenXmlReader Hey Richard, let's figure out why you're hitting that ArgumentNullException on the second and subsequent calls to ExcelReaderFactory.CreateOpenXmlReader(stream)—first run works, then it breaks. This is almost always related to how you're handling the underlying stream, so let's break down the most likely fixes:
Common Cause: Stream is exhausted or closed after first use
When you use ExcelDataReader to read a stream, it typically advances the stream's position to the end during processing. Additionally, if you wrap the reader in a using block (which you should!), the reader might automatically close the stream when it's disposed (depending on the library's implementation). By the time you try to reuse the same stream for the second call, it's either at the end of its data, closed, or even nulled out somewhere in your code.
Fixes to Try
1. Create a fresh stream for each call
If you're reading from a file, don't reuse the same FileStream instance. Instead, open a new stream every time you need to create a reader. This is the simplest and most reliable approach for most scenarios:
// ❌ Bad: Reusing the same stream leads to exhaustion/closure // var stream = new FileStream("your-file.xlsx", FileMode.Open); // ✅ Good: Create a new stream for each read operation using (var stream = new FileStream("your-file.xlsx", FileMode.Open)) { using var reader = ExcelReaderFactory.CreateOpenXmlReader(stream); // Process your data here } // Second call uses a brand new stream using (var stream = new FileStream("your-file.xlsx", FileMode.Open)) { using var reader2 = ExcelReaderFactory.CreateOpenXmlReader(stream); // Process your second batch of data here }
2. Reset the stream position and prevent closure (for reusable streams like MemoryStream)
If you need to reuse a stream (e.g., an in-memory MemoryStream), you'll need to reset its position to the start before each read, and ensure the reader doesn't close the stream when disposed. Use the overload of CreateOpenXmlReader that accepts a leaveOpen parameter (available in newer ExcelDataReader versions):
// Example with a MemoryStream var ms = new MemoryStream(); // Assume you've already populated the MemoryStream with Excel data here // First read: reset position and leave stream open ms.Position = 0; using var reader = ExcelReaderFactory.CreateOpenXmlReader(ms, leaveOpen: true); // Process first batch... // Second read: reset position again before creating a new reader ms.Position = 0; using var reader2 = ExcelReaderFactory.CreateOpenXmlReader(ms, leaveOpen: true); // Process second batch... // Don't forget to manually close the stream when you're done with it entirely ms.Close();
3. Check for accidental stream disposal elsewhere
Double-check your code to make sure no other part of your application is closing or nulling the stream between calls. For example, if you pass the stream to another method that wraps it in a using block without specifying leaveOpen: true, that method might close the stream out from under you.
Quick Debug Tip
Take a look at the full stack trace of the ArgumentNullException—it should tell you exactly which parameter is null. If it's the stream parameter itself, that confirms the stream is being disposed or nulled. If it's an internal parameter, you might need to ensure your ExcelDataReader library is up to date, but the stream handling fixes above still cover 90% of cases.
内容的提问来源于stack exchange,提问作者Richard Watts

