15

I'm working on a web application (ASP.NET) with a MySQL database. When I try to upload an Excel file (.xlsx) via this application I have no issues. The issue begins when the file is more than 24904 records.

At that point I get the following error:

This table contains cells that are outside the range of cells defined in this spreadsheet.

and it writes the first 24904 records.

I tried to split the load in multiples batches and it did not work.

Any thoughts?

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
Bugs
  • 4,491
  • 9
  • 32
  • 41

6 Answers6

1

I Don't Use OLDB or excel since its create many issue while reading excel

I use ExcelDataReader https://github.com/ExcelDataReader/ExcelDataReader

Try This

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;
    }
Abhishek Kumar
  • 768
  • 5
  • 9
0

I suspect you've hit an Excel limit (like the number of table mappings you can add)

Try starting your loop count at a higher number and see if it's the count of how many times you do this rather than the data that causes the problem. Maybe something like:

Dim x As Integer = 40002
Dim y As Integer = 60001
Derek Tomes
  • 3,989
  • 3
  • 27
  • 41
0

you can reset your range......by adding counter variable....increment it for specific range...and reset it...

firefly
  • 419
  • 1
  • 8
  • 18
0

You also can use LOAD DATA in a file query to load excel file to the database. The syntax for the query is:

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,...]
bummi
  • 27,123
  • 14
  • 62
  • 101
0

Remove range use Sheet1 ...and retest Dim oda As New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM [Sheet1$]", connExcel)

sams
  • 21
  • 8
0

The Excel limit is bigger than that. You have cells containing either hidden characters or spaces that fall outside of the specified range (as the error message states) check the columns to the right of your 'last' column on the error rows. I think you'll find they contain something.

An easy way to test, make a fresh new file with 50 000 records, filled with new data. I think you'll find it works.

CarlB
  • 1