导入Excel以使用C#进行访问

时间:2019-06-07 07:17:35

标签: c# excel ms-access import

我已经导入了Excel文件,并使用datagrid视图显示了数据,但我也想保存到MS Access数据库中。

可以使用OleDbCommandselect * into吗?

下面是我从Excel文件导入的代码;对于导入,我根据此处的另一篇文章创建了它:

private void BtnImport1_Click(object sender, EventArgs e)
{
     try
     {
          OpenFileDialog openfile1 = new OpenFileDialog();
          openfile1.Filter = "Excel Files | *.xlsx; *.xls; *.xlsm";
          openfile1.Title = "Seleccione el archivo de Excel";

          if (openfile1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
          {
               if (openfile1.FileName.Equals("") == false)
               {
                    this.tBox1.Text = openfile1.FileName;
                    Ruta = openfile1.FileName;
               }
          }

          string constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Ruta + ";Extended Properties = \"Excel 12.0; HDR=YES;\" ; ";
          OleDbConnection con = new OleDbConnection(constr);
          OleDbDataAdapter MyDataAdapter = new OleDbDataAdapter("Select * From [Hoja 1$]", con);
          DataTable dt1Excel = new DataTable();
          MyDataAdapter.Fill(dt1Excel);
          dataGridView1.DataSource = dt1Excel;
     }
     catch (Exception ex)
     {
          MessageBox.Show(ex.ToString());
     }
}

1 个答案:

答案 0 :(得分:0)

像这样尝试并提供反馈。

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;

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

        private void button1_Click(object sender, EventArgs e)
        {
            //here is a sample code which reads an Excel file Sheet1 which has 2 columns
            //And inserts the same into an access table
            //Change the file names, sheet name, table names and column names as per your requirements
            //File Names, replae with your file names
            string fileNameExcel = @"C:\your_path_here\Book1.xls";
            string fileNameAccess = @"C:\your_path_here\Database1.mdb";

            //Connection string for Excel
            string connectionStringExcel =
                string.Format("Data Source= {0};Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;", fileNameExcel);
            //Connection string for Access

            string ConnectionStringAccess =
                string.Format("Data Source= {0}; Provider=Microsoft.Jet.OLEDB.4.0; Persist security Info = false", fileNameAccess);

            //Connection object for Excel
            OleDbConnection connExcel = new OleDbConnection(connectionStringExcel);
            //Connection object for Access
            OleDbConnection connAccess = new OleDbConnection(ConnectionStringAccess);
            //Command object for Excel
            OleDbCommand cmdExcel = connExcel.CreateCommand();
            cmdExcel.CommandType = CommandType.Text;
            cmdExcel.CommandText = "SELECT * FROM [Sheet1$]";

            //Command object for Access

            OleDbCommand cmdAccess = connAccess.CreateCommand();
            cmdAccess.CommandType = CommandType.Text;
            cmdAccess.CommandText = "INSERT INTO Table1 (Column1, Column2) VALUES(@column1, @column2)";
            //Add parameter to Access command object
            OleDbParameter param1 = new OleDbParameter("@column1", OleDbType.VarChar);
            cmdAccess.Parameters.Add(param1);
            OleDbParameter param2 = new OleDbParameter("@column2", OleDbType.VarChar);
            cmdAccess.Parameters.Add(param2);
            //Open connections
            connExcel.Open();
            connAccess.Open();
            //Read Excel
            OleDbDataReader drExcel = cmdExcel.ExecuteReader();

            while (drExcel.Read())
            {
                //Assign values to access command parameters
                param1.Value = drExcel[0].ToString();
                param2.Value = drExcel[1].ToString();
                //Insert values in access
                cmdAccess.ExecuteNonQuery();
            }

            //close connections
            connAccess.Close();
            connExcel.Close();

        }
    }
}