• 将数据从excel中导入到access


    c# 代码

            private void button1_Click(object sender, EventArgs e)
            {
                // Create Access datatable
                if (File.Exists(@"d:\book.mdb"))
                {
                    File.Delete(@"d:\book.mdb");
                }
    
                Microsoft.Office.Interop.Access.Application _accessData;
                _accessData = new Microsoft.Office.Interop.Access.Application();
                _accessData.Visible = false;
                _accessData.NewCurrentDatabase(@"d:\book.mdb", Microsoft.Office.Interop.Access.AcNewDatabaseFormat.acNewDatabaseFormatAccess2000);
                _accessData.CloseCurrentDatabase();
                _accessData.Quit(Microsoft.Office.Interop.Access.AcQuitOption.acQuitSaveAll);
                _accessData = null;
    
                string _filename = @"d:\Book.xls";
                string _conn;
                _conn = "Provider=Microsoft.Jet.OLEDB.4.0;" + @"Data Source=" + _filename + "; Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
    
                OleDbConnection _connection = new OleDbConnection(_conn);
    
                OleDbCommand _command = new OleDbCommand();
                _command.Connection = _connection;
    
                try
                {
                    _command.CommandText = @"SELECT * INTO [MS Access;Database=d:\book.mdb].[Sheet1] FROM [Sheet1$]";
                    _connection.Open();
                    _command.ExecuteNonQuery();
                    _connection.Close();
                    MessageBox.Show("The import is complete!");
                }
    
                catch (Exception exc)
                {
                    MessageBox.Show("Import Failed, correct Column name in the sheet!" + "\nError message:\n" + exc.Message);
                }
    
            }
    View Code

    VB.NET 代码 

    Imports System.IO
    Imports Microsoft.Office.Interop
    Imports System.Data.OleDb
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            ' delete the file with the same and create a new access file
            If File.Exists("d:\book.mdb") Then
                File.Delete("d:\book.mdb")
            End If
    
            Dim _accessData As Access.Application
            _accessData = New Access.Application()
            _accessData.Visible = False
            _accessData.NewCurrentDatabase("d:\book.mdb", Access.AcNewDatabaseFormat.acNewDatabaseFormatAccess2000, , , )
    
            _accessData.CloseCurrentDatabase()
            _accessData.Quit(Microsoft.Office.Interop.Access.AcQuitOption.acQuitSaveAll)
            _accessData = Nothing
    
            ' initialize the connect string
            Dim _filename As String = "d:\Book.xls"
            Dim _conn As String
            _conn = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & _filename & ";" & "Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
    
            Dim _connection As OleDbConnection = New OleDbConnection(_conn)
    
            'Use OledbCommand object to select all the data from sheet1 and execute an ExecuteNonQuery to import data into Book.mdb.
            Dim _command As OleDbCommand = New OleDbCommand()
            _command.Connection = _connection
    
            Try
                _command.CommandText = "SELECT * INTO [MS Access;Database=d:\Book.mdb].[Sheet1] FROM [Sheet1$]"
                _connection.Open()
                _command.ExecuteNonQuery()
                _connection.Close()
                MessageBox.Show("The import is complete!")
            Catch e1 As Exception
                MessageBox.Show("Import Failed, correct Column name in the sheet!" & Environment.NewLine & "Error Message:" & Environment.NewLine & e1.Message)
            End Try
    
        End Sub
    
    End Class
    View Code

    相关错误解决办法:
    http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/2b7d388d-907d-4d0b-b46f-cacc9290c1b3

  • 相关阅读:
    线程安全-一个VC下多个网络请求
    [从头学数学] 第172节 直线与方程
    ASP.NET MVC 视图(一)
    Pixhawk之姿态解算篇(1)_入门篇(DCM Nomalize)
    Android基础新手教程——3.7 AnsyncTask异步任务
    IC卡、ID卡、M1卡、射频卡的区别是什么【转】
    .gitignore文件配置:keil工程文件类型【转】
    RK平台images打包细则【转】
    使用/dev/uinput的简要介绍(含demo程序)【转】
    Linux--struct file结构体【转】
  • 原文地址:https://www.cnblogs.com/guaiguai9/p/1745414.html
Copyright © 2020-2023  润新知