加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 09:39:30 所属栏目:asp.Net 来源:网络整理
导读:我正在研究一个Web应用程序,我想知道是否有可能在上传后使用 Asp.net(VB.net)将Excel文件的数据字段信息插入到我的SqlServer数据库中? 谢谢 解决方法 在VB.NET中尝试此代码: Protected Sub btnExport(ByVal sender As Object,ByVal e As System.EventArgs)
我正在研究一个Web应用程序,我想知道是否有可能在上传后使用 Asp.net(VB.net)将Excel文件的数据字段信息插入到我的SqlServer数据库中?

谢谢

解决方法

在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("~directoryfilename.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("~directoryfilename.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位应用程序.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读