將Excel檔案從ASP.NET應用程式上傳到MySQL時出錯
我正在使用SQL/">MySQL資料庫的Web應用程式(ASP.NET).當我嘗試通過此應用程式上傳Excel檔案(.xlsx)時,我沒有任何問題.當檔案超過24904條記錄時,該問題開始.
那時我得到以下錯誤:
This table contains cells that are outside the range of cells defined in this spreadsheet.
並寫入了第24904條記錄.
我試圖分批載入多個批次,它沒有工作.
有什麼想法嗎?
Dim connExcel As New System.Data.OleDb.OleDbConnection(conStr) Dim cmdExcel As New System.Data.OleDb.OleDbCommand() Dim dt As New DataTable() Dim dataset As New DataSet Dim x As Integer = 2 Dim y As Integer = 20001 Dim range As String = "A" + x.ToString + ":" + "I" + y.ToString cmdExcel.Connection = connExcel If erro = 0 Then Try For i As Integer = 0 To 50 connExcel.Open() dataset.Reset() dataset.Clear() Dim oda As New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM [Sheet1$" & range.ToString & "]", connExcel) oda.TableMappings.Add("Table", "ExcelTest") oda.Fill(dataset) connExcel.Close() If dataset.Tables(0).Rows.Count > 0 Then SendToDB(dataset) Else i = 50 End If x = x + 20000 y = y + 20000 range = "A" + x.ToString + ":" + "I" + y.ToString Next Label7.Visible = True Label7.Text = "The information has been written successfully from 0 to " + y.ToString Catch ex As Exception Label9.Visible = True Label9.Text = "Database Error 2:" + ex.Message connExcel.Close() End Try End If
我不會使用OLDB或excel,因為它在閱讀excel時建立了許多問題
我使用ExcelDataReaderofollow,noindex" target="_blank">https://github.com/ExcelDataReader/ExcelDataReader
嘗試這個
Install-Package ExcelDataReader
public static DataSet GetExcelDataSet(string filePath) { FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read); IExcelDataReader excelReader; if (Path.GetExtension(filePath) == ".xls") { //Choose one of either 1 or 2 //1. Reading from a binary Excel file ('97-2003 format; *.xls) excelReader = ExcelReaderFactory.CreateBinaryReader(stream); } else { //2. Reading from a OpenXml Excel file (2007 format; *.xlsx) excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); } //Choose one of either 3, 4, or 5 //3. DataSet - The result of each spreadsheet will be created in the result.Tables ////////DataSet result = excelReader.AsDataSet(); //4. DataSet - Create column names from first row excelReader.IsFirstRowAsColumnNames = true; DataSet result = excelReader.AsDataSet(); //5. Data Reader methods //while (excelReader.Read()) //{ ////excelReader.GetInt32(0); //} //6. Free resources (IExcelDataReader is IDisposable) excelReader.Close(); return result; }
http://stackoverflow.com/questions/13352739/error-uploading-excel-file-from-asp-net-application-to-mysql