将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时出错》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

万万没想到

万万没想到

万维钢(同人于野) / 电子工业出版社 / 2014-10-1 / 39.80元

本书精选了万维钢老师的文章和书评,以“用理工科思维理解世界”为导向。作者常用有趣的实验、数据来解读感性的事物,其理工科思维涉及行为经济学、认知心理学、社会学、统计学、物理等许多学科,以前沿的科学视角解读生活,为人们提供了认知的新方法。读完本书相当于精读了十几本经过筛选 、再创作及通俗化处理的巨著,不仅有趣还十分有营养。一起来看看 《万万没想到》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

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

html转js在线工具