阅读Excel以进行访问 [英] Reading Excel to Access

查看:62
本文介绍了阅读Excel以进行访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试开发一个可以将Excel工作表转换为Access数据库的应用程序.我是软件开发的新手.我无法解决以下错误,如果有人可以帮助我找出解决方案,将不胜感激.在此先感谢

产生的错误如下:

1. Acess._Application.Quit(Access.AcQuitOption)的最佳重载方法匹配具有一些无效的参数

2.参数"1":无法从"Microsoft.Office.Interop.Access.AcQuitOption"转换为"Access.AcQuitOption"

3.方法"FillAccessDatabase"的重载不接受"1"自变量

这是我使用的代码:

Hi, im trying to develop an application which can convert Excel worksheet to an Access db. im new for software development. Im unable to fix the below errors and would be grateful if somebody can help me to figure out the solution. thanks in advance

errors that were generated given below:

1. The best overloaded method match for Acess._Application.Quit(Access.AcQuitOption)'' has some invalid arguments

2. Argument ''1'': cannot convert from ''Microsoft.Office.Interop.Access.AcQuitOption'' to ''Access.AcQuitOption''

3. No overload for method ''FillAccessDatabase'' takes ''1'' arguments

here is the code that i use:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        public static void CheckUpdateDBFile(string filename)
        {
            
            if (File.Exists(@"C:\Book.mdb"))
            {

                File.Delete(@"C:\Book.mdb");

            }

            Access.Application _accessData;

            _accessData = new Access.ApplicationClass();

            _accessData.Visible = false;

            _accessData.NewCurrentDatabase(@"C:\Book.mdb");

            _accessData.CloseCurrentDatabase();

            _accessData.Quit(Microsoft.Office.Interop.Access.AcQuitOption.acQuitSaveAll);

            _accessData = null;

            OleDbConnection _connection = MakeExcelConnection(filename);

            FillAccessDatabase(_connection);

        }

        private static OleDbConnection MakeExcelConnection(string fileName)
        {

            string _filename = @"C:\NMS_List_Export.xls";

            string _conn;

            _conn = "Provider=Microsoft.Jet.OLEDB.12.0;" + @"Data Source=" + _fileName + ";" +

            "Extended Properties=Excel 12.0;";
            OleDbConnection _connection = new OleDbConnection(_conn);

            return _connection;
        }

        public static void FillAccessDatabase()
        {
            OleDbCommand _command = new OleDbCommand();

            _command.Connection = _connection;

            try
            {

                _command.CommandText = @"SELECT * INTO [MS Access;Database=C:\Book.mdb].[NMS_List_Export] FROM [NMS_List_Export$]";

                _connection.Open();

                _command.ExecuteNonQuery();

                _connection.Close();

                MessageBox.Show("The import is complete!");

            }

            catch (Exception)
            {

                MessageBox.Show("Import Failed, correct Column name in the sheet!");
            }
        }

    }
}

推荐答案

"; _connection.Open(); _command.ExecuteNonQuery(); _connection.Close(); MessageBox.Show(" ); } 捕获(异常) { MessageBox.Show(" ); } } } }
"; _connection.Open(); _command.ExecuteNonQuery(); _connection.Close(); MessageBox.Show("The import is complete!"); } catch (Exception) { MessageBox.Show("Import Failed, correct Column name in the sheet!"); } } } }


我也在您的代码中发现了一些问题:
-在MakeExcelConnection函数中,您有一个名为"_fileName"的变量,其参数为"fileName",而该函数内部的私有变量为"_filename".

解决您遇到的错误的方法:
#1和#2尝试将Platform目标更改为x86

#3.您应该将
I''ve found some issues in your code also:
- In function MakeExcelConnection, you have a variable named "_fileName" in which your argument is "fileName" and the private variable inside that function is "_filename".

Solution to the errors you encountered:
#1 and #2 try to change the Platform target to x86

#3. You should modify your
public static void FillAccessDatabase()

修改为

public static void FillAccessDatabase(OleDbConnection _connection)


这篇关于阅读Excel以进行访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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