Asp.net:Excel文件上载未完成,但未给出错误消息
我在尝试上载Excel文件并将其存储在ASP.net应用程序的SQL server表中时遇到了一个相当奇怪的问题。 文件不是太大:大约2.5或3 Mb。 问题是,加载某些行后,上载会被“中断”,显然不会导致任何特定错误,因为加载过程通过显示我发送给客户端的成功消息来完成: “进程成功完成:已从中上载XXX行 文件” 问题是XXX行不是文件中的所有行。根据Excel文件的每列中有多少信息,该过程仅上载文件共25000行中的15500行。(如果Excel文件中的信息较少,它可以上传,比如说25000行中的20000行)。。。事实上,文件并没有被“完全”上传 我已经尝试增加web.config中的Asp.net:Excel文件上载未完成,但未给出错误消息,asp.net,file-upload,Asp.net,File Upload,我在尝试上载Excel文件并将其存储在ASP.net应用程序的SQL server表中时遇到了一个相当奇怪的问题。 文件不是太大:大约2.5或3 Mb。 问题是,加载某些行后,上载会被“中断”,显然不会导致任何特定错误,因为加载过程通过显示我发送给客户端的成功消息来完成: “进程成功完成:已从中上载XXX行 文件” 问题是XXX行不是文件中的所有行。根据Excel文件的每列中有多少信息,该过程仅上载文件共25000行中的15500行。(如果Excel文件中的信息较少,它可以上传,比如说25000
“httprequest maxRequestLength”
值,即使该文件不大于ASP.net默认的4Mb文件上载。
我使用的上载和读取文件的代码(vb.net)基本上如下所示:
Dim connection As DbConnection = Nothing
Dim Command As DbCommand = Nothing
Dim ConexionStringExcel As String
Dim dr As DbDataReader
Dim mensajeError as String = ""
Dim ncAdic as Integer = 0
Dim nReg as Integer = 0
'String connection for Excel 2007: for now, I'm not allowing other Excel versions
ConexionStringExcel = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source = " & sNombreArch & ";" & _
"Extended Properties=Excel 12.0 Xml;"
'sNombreArch is the full name of the uploaded file
connection = New OleDb.OleDbConnection(ConexionStringExcel)
Command = connection.CreateCommand()
Command.CommandText = "SELECT * FROM [" + nomHojaArch + "$]"
'---
'lblMensaje is a Label object in the aspx page:
'---
lblMensaje.Visible = False
Try
'Open the Excel file
connection.Open()
'Read file content
dr = Command.ExecuteReader()
While dr.Read
Try
'Two first columns of the file are mandatory in my case...
If Not IsDBNull(dr(0)) And Not IsDBNull(dr(1)) Then
'---
'dsTempCargue is a SqlDataSource object in the aspx page
'---
dsTempCargue.InsertParameters.Item("idReg").DefaultValue = dr(0)
dsTempCargue.InsertParameters.Item("nombre").DefaultValue = dr(1)
For ncAdic = 2 To 10
If Not IsDBNull(dr(ncAdic)) Then
dsTempCargue.InsertParameters.Item(ncAdic).DefaultValue = dr(ncAdic)
Else
dsTempCargue.InsertParameters.Item(ncAdic).DefaultValue = DBNull.ToString
End if
Next
dsTempCargue.Insert()
nReg = nReg + 1
Else
mensajeError = "Column A and B of the File cannot be empty"
Exit While
End If
Catch exRead As Exception
mensajeError = "Error reading the file or saving its content: " & exRead.Message
Exit While
End Try
End While
'If there was no error, show success message
If String.IsNullOrEmpty(mensajeError) Then
mensajeError = "The process finished successfuly. " & nReg.ToString() & " rows were uploaded from the file"
End IF
Catch ex As Exception
mensajeError = "Error uploading the file: " & ex.Message
End Try
lblMensaje.Text = mensajeError
lblMensaje.Visible = True
为什么你认为这个上传过程无法读取整个文件???。。。如有任何建议,将不胜感激
非常感谢
Diego对于存储行的表,不同的列是什么数据类型?可能其中一个单元格的内容与列数据类型不兼容。您有什么方法可以检查吗?尽管我真的不确定错误的原因是什么,但我最终还是通过将代码更改为使用我找到的第三方库,并提供了一些好的注释,这些注释可在此处获得: 我不太清楚最初的问题是什么,为什么使用这个库解决了这个问题,但它起了作用。(也许当我有时间的时候,我会试着找到更多关于这个问题的信息)
感谢Patrick的评论和大家的关注感谢Patrick的回答。。。但是,我已经检查了SQL表的数据类型和Excel文件中的信息,没有发现任何错误。我的SQL表(和文件)的第一列是一个int列,其余是strings(varchar)。。。我的应用程序。总是在阅读一定数量的信息:根据Excel文件中的信息量,我的流程上载的行数会减少或增加(请阅读原始问题)。。。我怀疑我必须配置某种“限制”参数(httprequest maxRequestLength除外)??。还有其他想法吗?当你多次上传同一个文件时,插入的行数是否相同?是的,没错:如果上传完全相同的文件,插入的行数总是相同的。。。有什么线索吗??。。。感谢第一行未插入的LOT,它与成功插入的最后一行有何不同?另外,excel文件中的信息较少是什么意思?这是否意味着更少的栏目?