Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当我试图用文件流打开excel文件时,程序返回偏移量异常_C#_.net - Fatal编程技术网

C# 当我试图用文件流打开excel文件时,程序返回偏移量异常

C# 当我试图用文件流打开excel文件时,程序返回偏移量异常,c#,.net,C#,.net,我是一名C#.NET新手,我使用NPOI读取.xls文件,.xls文件大小为86KB,我使用此方法读取文件: public static void ReadXlsFile(string filePath) { using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { var workbook

我是一名C#.NET新手,我使用NPOI读取
.xls
文件,
.xls
文件大小为86KB,我使用此方法读取文件:

        public static void ReadXlsFile(string filePath)
        {
            using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                var workbook = new HSSFWorkbook(fs);
                var sheet = workbook.GetSheetAt(0);
                int rowIndex = 10;
                while (sheet.GetRow(rowIndex) != null)
                {
                    var row = sheet.GetRow(rowIndex);
                    Console.WriteLine("Row: {0} - Date: {1} - Class: {2} - Periods: {3} - Room: {4} - Time: {5}", rowIndex, row.GetCell(0).StringCellValue, row.GetCell(3).StringCellValue, 
                        row.GetCell(6).StringCellValue, row.GetCell(7).StringCellValue, row.GetCell(8).StringCellValue);
                    rowIndex++;
                }

            }
        }
程序返回此异常:
偏移量和长度超出了数组的界限,或者计数大于从索引到源集合末尾的元素数。

你能解释一下并给我一个解决办法吗?谢谢

更新:更清晰的异常消息:

Unhandled Exception: System.ArgumentException: Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.
   at System.IO.FileStream.Read(Byte[] array, Int32 offset, Int32 count)
   at NPOI.Util.IOUtils.ReadFully(Stream stream, Byte[] b, Int32 off, Int32 len)
   at NPOI.POIFS.FileSystem.NPOIFSFileSystem..ctor(Stream stream)
   at NPOI.HSSF.UserModel.HSSFWorkbook..ctor(Stream s, Boolean preserveNodes)
   at NPOI.HSSF.UserModel.HSSFWorkbook..ctor(Stream s)
   at DataCrawler.Utils.AnalyzeXlsFile(String filePath) in D:\C#\actvnDataGetter\DataCrawler\Utils.cs:line 46
   at Console.Program.<Main>d__0.MoveNext() in D:\C#\actvnDataGetter\Console\Program.cs:line 17
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at Console.Program.<Main>(String[] args)

未处理的异常:System.ArgumentException:偏移量和长度超出了数组的界限,或者计数大于从索引到源集合结尾的元素数。
在System.IO.FileStream.Read(字节[]数组,Int32偏移量,Int32计数)
在NPOI.Util.IOUtils.ReadFully处(流,字节[]b,Int32 off,Int32 len)
在NPOI.POIFS.FileSystem.NPOIFSFileSystem..ctor(流)
在NPOI.HSSF.UserModel.HSSFWorkbook..ctor(流,布尔节点)
在NPOI.HSSF.UserModel.HSSFWorkbook..ctor(流s)
在D:\C\actvndagetter\DataCrawler\Utils.cs中的DataCrawler.Utils.AnalyzeXlsFile(字符串文件路径):第46行
在d:\C\actvndagetter\Console\Program.cs中的Console.Program.d_u0.MoveNext()处:第17行
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Runtime.CompilerServices.TaskAwaiter.GetResult()中
在Console.Program中。(字符串[]args)

我找到了问题的解决方案,只需降级NPOI版本,我使用了2.5.2,它使我无法读取
.xls
文件,所以我降级到了2.3.0版本,它成功了

你们有第11排的床单吗?工作表中提到的列?@viveknuna当然,即使我尝试将
rowIndex
更改为0,它也不会更改任何内容,并且在
using
block行上返回的异常您是否阅读了正确的工作表?@viveknuna我认为我在这一步没有错,我尝试更改工作表索引值您正在对第一个工作表中的while循环进行排序,从第11行到第一列为非空的行,因此您必须确保所有行都具有所需的列