使用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位应用程序. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
推荐文章
站长推荐
- asp.net-mvc – 我应该把我的控制器业务逻辑放在
- redis 介绍和常用命令
- asp.net – IIdentity,IPrincipal,OWIN,Identity
- asp.net-mvc – MVC帮助 – 使用@URL的图像src?
- 你使用ASP.NET MVC Action方法做了什么聪明的事情
- asp.net – 如何从Date对象减去一个月?
- DevExpress v17.2新版亮点—ASP.NET篇(二)
- asp.net – 表单身份验证可以忽略returnUrl
- asp.net – 是否有可能过滤SignalR中的接收器?
- asp.net-mvc – 使用mvc 4 razor引擎显示3个文本
热点阅读