将Excel文件从ASP.NET应用程序上传到MySQL时出错

栏目: ASP.NET · 发布时间: 6年前

内容简介:http://stackoverflow.com/questions/13352739/error-uploading-excel-file-from-asp-net-application-to-mysql

我正在使用 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时创建了许多问题

我使用ExcelDataReader 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


以上所述就是小编给大家介绍的《将Excel文件从ASP.NET应用程序上传到MySQL时出错》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Lean Analytics

Lean Analytics

Alistair Croll、Benjamin Yoskovitz / O'Reilly Media / 2013-3-18 / USD 29.99

If you're involved with a startup, analytics help you find your way to the right product and market before the money runs out. But with a flood of information available, where do you start? This book ......一起来看看 《Lean Analytics》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具