简体   繁体   English

使用Excel Data Reader的意外结果

[英]Unexpected results using Excel Data Reader

I'm reading an XLSX (Microsoft Excel XML file) using the Excel Data Reader from http://exceldatareader.codeplex.com/ and I getting some unexpected results. 我正在从http://exceldatareader.codeplex.com/使用Excel数据读取器读取XLSX(Microsoft Excel XML文件),但得到了一些意外的结果。

The following code outputs data from multiple tabs 以下代码从多个选项卡输出数据

var reader = Excel.ExcelReaderFactory.CreateOpenXmlReader(uploadFile.InputStream);
while (reader.Read())
{
    System.Diagnostics.Debug.WriteLine(reader.FieldCount );
    for (int i = 0; i < reader.FieldCount; i++)
    {
        System.Diagnostics.Debug.Write(reader[i] + "*");
    }
    System.Diagnostics.Debug.WriteLine("\n~\n");
}

On a single line, I can get data from 3 or more tabs. 我可以单行从3个或更多选项卡中获取数据。

I would expect this to loop through and show all of the contents of the first tab and only the first tab. 我希望这会循环并显示第一个选项卡的所有内容,并且仅显示第一个选项卡。

What am I missing? 我想念什么?

Update: It appears that the above code does work fine if there is only 1 tab in the excel file. 更新:如果excel文件中只有1个选项卡,则上述代码似乎可以正常工作。 This may just be a bug with this library. 这可能只是该库的错误。 Has anyone else used this library to parse excel files with multiple tabs? 有没有其他人使用此库来解析具有多个选项卡的excel文件?

Thanks 谢谢

OK, so my reply is extremely late with reference to this question, but if its any help try encapsulating your code in a reader.NextResult() block. 好的,所以我的答复对于这个问题来说非常晚了,但是如果有帮助的话,可以尝试将代码封装在reader.NextResult()块中。 This works the same way as when you parse through multiple DataTable objects within a DataSet. 这与解析数据集中的多个DataTable对象时的工作方式相同。

Additionally, this approach has a very small memory footprint as opposed to the reader.AsDataSet() method, which hogs a lot of memory even for workbooks as small as 20MBs 此外,与reader.AsDataSet()方法相比,此方法的内存占用量非常小,即使对于最小20MB的工作簿也要占用大量内存

eg 例如

var reader = Excel.ExcelReaderFactory.CreateOpenXmlReader(uploadFile.InputStream);
do
{
    while (reader.Read())
    {
        System.Diagnostics.Debug.WriteLine(reader.FieldCount );
        for (int i = 0; i < reader.FieldCount; i++)
        {
            System.Diagnostics.Debug.Write(reader[i] + "*");
        }
        System.Diagnostics.Debug.WriteLine("\n~\n");
    }
}while(reader.NextResult());

Which is why I am using NPOI . 这就是为什么我使用NPOI的原因 I have tried several other Excel readers, this one actually worked for me. 我已经尝试了其他一些Excel读取器,而这个实际上对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM