将excel工作簿导入vb.net中的sql server [英] Importing excel workbook into sql server in vb.net

查看:91
本文介绍了将excel工作簿导入vb.net中的sql server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的先生/女士,



请帮助我.....



我们有包含五个工作表的excel工作簿,我们必须将所有工作表中的数据导入vb.net中的MS-SqlServer 2008R2(.Net framework- 3.5)





请给我一个想法



如何将数据从excel工作簿导入sqlserver 2008R2。

Dear Sir/Madam,

kindly help me.....

we have a excel workbook which contain five worksheets and we have to import data from all worksheets into MS-SqlServer 2008R2 in vb.net (.Net framework- 3.5)


kindly give me idea

How to import data from excel workbook into sqlserver 2008R2.

推荐答案

请查看我对该问题的评论,并了解此所需的字段。



这是一篇很好的ADO.NET入门CodeProject文章:

为初学者使用ADO.NET [ ^ ]。



如需了解更多详情,请从以下开始:

http: //en.wikipedia.org/wiki/ADO.NET [ ^ ],

http://msdn2.microsoft.com/en-us/library/aa286484.aspx [ ^ ]。



使用Office Excel Interop:

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel%28v=office.11​​%29.aspx [ ^ ]。



基本上,从添加引用窗口的.NET选项卡中添加对项目的引用,就像你一样将在您的GAC中进行汇编。选择适当版本的Office Excel Interop程序集。使用它。



-SA
Please see my comment to the question and learn the fields required for this.

This is a good introductory CodeProject article for ADO.NET:
Using ADO.NET for beginners[^].

For further detail, please start with:
http://en.wikipedia.org/wiki/ADO.NET[^],
http://msdn2.microsoft.com/en-us/library/aa286484.aspx[^].

Use Office Excel Interop:
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel%28v=office.11%29.aspx[^].

Basically, add a reference to the project from the tab ".NET" of the "Add reference" window, as you will have the assembly in your GAC. Choose "Office Excel Interop" assembly of appropriate version. Use it.

—SA


试试这个代码。其中Test.Xlsx是excel表和Excel是数据库表





Try This code.Where Test.Xlsx Is excel sheet and Excel is DataBase Table


Imports System.Data.SqlClient
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

         Dim MyConnection As System.Data.OleDb.OleDbConnection
        Dim DtSet As System.Data.DataSet
        Dim MyCommand As System.Data.OleDb.OleDbDataAdapter

        Dim fBrowse As New OpenFileDialog
        With fBrowse
            .Filter = "Excel files(*.xlsx)|*.xlsx|All files (*.*)|*.*"
            .FilterIndex = 1
            .Title = "Import data from Excel file"
        End With
        If fBrowse.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim fname As String
            fname = fBrowse.FileName
            MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source='" & fname & " '; " & "Extended Properties=Excel 8.0;")
            MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1


,MyConnection)
MyCommand.TableMappings.Add( 测试
DtSet = System.Data.DataSet
MyCommand.Fill(DtSet)
MyConnection.Close()
For 每个 Drr As DataRow DtSet.Tables( 0 )。行
Execute_Local( 插入Excel(名称,名称,薪水)值('& Drr( 0 )。ToString& ','& Drr( 1 )。ToString& ','& Drr( 2 )。ToString& ')
下一步
MsgBox( 已成功保存

< span class =code-keyword>结束 如果

结束 Sub
", MyConnection) MyCommand.TableMappings.Add("Table", "Test") DtSet = New System.Data.DataSet MyCommand.Fill(DtSet) MyConnection.Close() For Each Drr As DataRow In DtSet.Tables(0).Rows Execute_Local("INSERT INTO Excel(Name, Designation, Salary) VALUES ('" & Drr(0).ToString & "','" & Drr(1).ToString & "','" & Drr(2).ToString & "')") Next MsgBox("Successfully Saved") End If End Sub


这篇关于将excel工作簿导入vb.net中的sql server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆