15

我正在开发一个带有MySQL数据库的 Web 应用程序 ( ASP.NET )。当我尝试通过此应用程序上传 Excel 文件 ( .xlsx ) 时,我没有任何问题。当文件超过 24904 条记录时,问题就开始了。

那时我收到以下错误:

此表包含超出此电子表格中定义的单元格范围的单元格。

它写入前 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
4

6 回答 6

1

我不使用 OLDB 或 excel,因为它在阅读 excel 时会产生很多问题

我使用 ExcelDataReader https://github.com/ExcelDataReader/ExcelDataReader

尝试这个

安装包 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;
    }
于 2016-08-22T13:54:49.087 回答
0

我怀疑您已达到 Excel 限制(例如您可以添加的表映射数量)

尝试以更高的数字开始循环计数,看看它是否是您执行此操作的次数而不是导致问题的数据。也许是这样的:

Dim x As Integer = 40002
Dim y As Integer = 60001
于 2013-01-17T21:06:33.503 回答
0

你可以重置你的范围......通过添加计数器变量......为特定范围增加它......并重置它......

于 2014-03-14T10:36:27.947 回答
0

您还可以在文件查询中使用 LOAD DATA 将 excel 文件加载到数据库中。查询的语法是:

LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'
    [REPLACE | IGNORE]
    INTO TABLE tbl_name
    [CHARACTER SET charset_name]
    [{FIELDS | COLUMNS}
        [TERMINATED BY 'string']
        [[OPTIONALLY] ENCLOSED BY 'char']
        [ESCAPED BY 'char']
    ]
    [LINES
        [STARTING BY 'string']
        [TERMINATED BY 'string']
    ]
    [IGNORE number LINES]
    [(col_name_or_user_var,...)]
    [SET col_name = expr,...]
于 2015-07-02T09:24:58.970 回答
0

Excel 的限制比这更大。您的单元格包含超出指定范围的隐藏字符或空格(如错误消息所述),请检查错误行上“最后一个”列右侧的列。我想你会发现它们包含一些东西。

一种简单的测试方法,创建一个包含 50 000 条记录的全新文件,其中填充了新数据。我想你会发现它有效。

于 2016-05-13T11:21:44.740 回答
0

删除范围使用 Sheet1 ...并重新测试 Dim oda As New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM [Sheet1$]", connExcel)

于 2015-11-28T05:17:29.580 回答