NPOI 有时仅从电子表格中读取 10 行
问题:我遇到了故障排除的难题,希望得到关于检查哪些内容以解决我正在处理的网站遇到的问题的建议。当使用 NPOI (C#) 从电子表格中读取数据时,有时(并非总是)行读取会在十行后停止。
很抱歉这篇文章很长,但不确定什么有用/没用。在这里发帖的主要原因是我不知道向伟大的谷歌机器提出正确的问题。
我有一个 Intranet 站点,我正在其中读取 XLSX 文件并将其内容推送到 Oracle 表中。从主题行可以看出,我正在使用 NPOI。在大多数情况下,它只是工作,但只是有时......
在Oracle中,我有一个临时表,它被截断并且应该填充电子表格中的数据。
在我的应用程序 (ASPX) 中,用户将电子表格上传到服务器(这可以正常工作),然后应用程序调用一个 WebMethod 来截断 Oracle 临时表中的数据(这可以正常工作),然后调用另一个 WebMethod 来读取数据从电子表格中获取数据并加载临时表(这有点管用)。
我需要帮助的就是这个“有点作品”的作品。
该电子表格有 170 个数据行。当我在 VS 中运行该应用程序时,它大多数时候会读取/写入所有 170 条记录,但有时它只读取 10 条记录。当我从网络服务器运行该应用程序时,第一次失败(无法捕获特定错误),第二次及随后的时间,它仅从电子表格中读取十条记录并成功加载所有十条记录。我检查了上传到服务器的文件,确实有170条数据记录。
无论进程读取 10 条记录还是 170 条记录,都不会出现错误消息,也不会说明为什么在 10 条记录后就停止读取。 (我会在这里提到,今天的文件有 170 个,但明天可能有 180 个或 162 个,所以它不是固定的)。
因此,我已经描述了它应该做什么以及它实际在做什么。我认为现在是代码片段的时候了。
/* snowSource below is the path/filename assembled separately */
/* SnowExcelFormat below is a class that basically maps row data with a specific data class */
IWorkbook workbook;
try
{
using (FileStream file = new FileStream(snowSource, FileMode.Open, FileAccess.Read, FileShare.Read))
{
workbook = WorkbookFactory.Create(file);
}
var importer = new Mapper(workbook);
var items = importer.Take<SnowExcelFormat>(0);
/* at this point, item should have 170 rows but sometimes it contains only 10 with no indication why */
/* I don't see anything in the workbook or importer objects that sheds any light on what's happening. */
同样,从 VS 运行时,大多数情况下这都可以正常工作。这告诉我这是可行的代码。在 Web 服务器上运行此程序时,我第一次尝试该过程时失败,但随后它运行,但只获取前 10 条记录,忽略其余记录。此外,读取的所有数据(10 或 170)都成功插入到临时表中,这告诉我 Oracle 对数据、其格式和此过程完全没问题。我需要的只是弄清楚为什么我的代码没有从 Excel 读取所有数据。
我已经多次验证本地 DLL 和网络服务器 DLL 是相同的。我正在阅读同一个 Excel 文件。
我在这里遇到了严重的障碍,并且对于如何在代码失败时排除故障的想法已经用尽了。我不知道是否存在限制 FileStream 对象可用内存的因素,导致它过早停止读取文件 - 并且没有遇到任何看起来像资源限制器的东西。我不知道是否存在限制 importer.Take 方法拉取的行数的因素。任何建议将不胜感激。
PROBLEM: I've hit a troubleshooting wall and hoping for suggestions on what to check to get past an issue I'm having with an internet site I'm working on. When reading data from a spreadsheet using NPOI (C#), sometimes (not all the time) the row reading stops after just ten rows.
Sorry for the very long post but not sure what is/isn't useful. Primary reason for posting here is that I don't know the right question to ask the Great Google Machine.
I have an intranet site where I'm reading an XLSX file and pushing its contents into an Oracle table. As you can tell by the subject line, I'm using NPOI. For the most part, it's just working, but only sometimes...
In Oracle, I have a staging table, which is truncated and is supposed to be filled with data from the spreadsheet.
In my app (ASPX), users upload their spreadsheet to the server (this just works), then the app calls a WebMethod that truncates data from the Oracle staging table (this just works), then another WebMethod is called that is supposed to read data from the spreadsheet and load the staging table (this, kinda works).
It's this "kinda works" piece is what I need help with.
The spreadsheet has 170 data rows. When I run the app in VS, it reads/writes all 170 records most of the time but sometimes it reads just 10 records. When I run the app from the web server, the first time it fails (haven't been able to catch a specific error), the second and subsequent times, it reads just ten records from the spreadsheet and successfully loads all ten. I've checked the file uploaded to the server and it does have 170 data records.
Whether the process reads 10 records or 170 records, there are no error messages and no indication why it stopped reading after just ten. (I'll mention here that the file today has 170 but tomorrow could have 180 or 162, so it's not fixed).
So, I've described what it's supposed to do and what it's actually doing. I think it's time for code snippet.
/* snowSource below is the path/filename assembled separately */
/* SnowExcelFormat below is a class that basically maps row data with a specific data class */
IWorkbook workbook;
try
{
using (FileStream file = new FileStream(snowSource, FileMode.Open, FileAccess.Read, FileShare.Read))
{
workbook = WorkbookFactory.Create(file);
}
var importer = new Mapper(workbook);
var items = importer.Take<SnowExcelFormat>(0);
/* at this point, item should have 170 rows but sometimes it contains only 10 with no indication why */
/* I don't see anything in the workbook or importer objects that sheds any light on what's happening. */
Again, this works perfectly fine most of the time when running from VS. That tells me this is workable code. When running this on the web server, it fails the first time I try the process but subsequently it runs but only picking up that first 10 records, ignoring the rest. Also, all the data that's read (10 or 170) is successfully inserted into the staging table, which tells me that Oracle is perfectly okay with the data, its format, and this process. All I need is to figure out why my code doesn't read all the data from Excel.
I have verified numerous times that the local DLL and webserver DLL are the same. And I'm reading the same Excel file.
I'm hitting a serious wall here and have run out of ideas on how to troubleshoot where the code is failing, when it fails. I don't know if there's something limiting memory available to the FileStream object causing it to stop reading the file prematurely - and didn't run across anything that looked like a resource limiter. I don't know if there's something limiting the number of rows pulled by the importer.Take method. Any suggestions would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在某些文件上遇到了同样的问题,在分析了问题后,这对我有用。
Importer.take&lt; SnowExcelforMat&gt;(0)
有三个参数,其中一个是maxerrorrows
。它的默认值为10。面对10个以上错误时,您的解析停止,然后函数停止读数。
您要做的是设置
maxerRorrows
而不是取10个默认值。I faced the same issue on some files and after analyzing the problem, this is what worked for me.
importer.Take<SnowExcelFormat>(0)
have three parameters and one of them ismaxErrorRows
. Its default value is 10.Your parsing stop when facing more than 10 errors then the function stop reading.
What you have to do is to set
maxErrorRows
instead of taking default value that is 10.