asp.net - 使用 ASP.net (Vb.net) 将上传的 Excel 文档中的数据插入数据库

标签 asp.net sql-server vb.net file-upload

我正在开发一个 web 应用程序,我想知道是否有可能在上传后将 Excel 文件的数据字段信息插入到我的 SqlServer 数据库中,使用 Asp.net(VB.net) ??

谢谢

最佳答案

在 VB.NET 中试试这个代码:

Protected Sub btnExport(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim TheFile As FileInfo = New FileInfo(MapPath(".") & "\" & "filename.xls")

    ' Connection String to Excel Workbook 2010 (xlsx)
    ' Dim excelConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("~\directory\filename.xlsx") + ";Extended Properties=""Excel 12.0 Xml;HDR=YES;"""

    ' Connection String to Excel Workbook 2003 (xls)
    Dim excelConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~\directory\filename.xls") + ";Extended Properties=""Excel 8.0;HDR=YES;"""

    ' Create Connection to Excel Workbook
    Using connection As New OleDbConnection(excelConnectionString)
        Dim command As New OleDbCommand("Select * FROM [sheetname$] ", connection)

        connection.Open()

        ' Create DbDataReader to Data Worksheet
        Using dr As DbDataReader = command.ExecuteReader()

            ' SQL Server Connection String
            Const sqlConnectionString As String = "Data Source=server; Initial Catalog=database; Persist Security Info=True;User ID=userid;Password=password"

            ' Bulk Copy to SQL Server
            Using bulkCopy As New SqlBulkCopy(sqlConnectionString)
                bulkCopy.DestinationTableName = "SqlServerTableName"
                bulkCopy.WriteToServer(dr)
            End Using
        End Using
    End Using
End Sub

附加提示:尝试将 IIS 设置为运行 32 位应用程序。

关于asp.net - 使用 ASP.net (Vb.net) 将上传的 Excel 文档中的数据插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18732063/

相关文章:

sql-server - 在计算列中使用 master.sys.fn_varbintohexsubstring

sql-server - 如何在链接服务器(SQL Server 2012)上查找具有特定表的数据库?

sql-server - 在 Microsoft SQL Server 上使用正则表达式模式最多提取三个不同长度的字符串

vb.net - 如何在 VB.Net 中添加阿拉伯语输入?

html - 表格间距问题

c# - 以设计器模式打开 .NET 表单 - 获取 "The path is not of a legal form"

c# - 使用 asp.net 图表控件的线和点

c# - 处理以下代码的更好方法

asp.net - Javascript 中 window.sessionStorage 的替代品?

asp.net - 如何在.net core框架中使用TransactionScope?我找不到它