【问题标题】:Import Excel to Access with c#使用 c# 将 Excel 导入到 Access
【发布时间】:2019-06-07 07:17:35
【问题描述】:

我已导入 Excel 文件并使用数据网格视图显示数据,但我还想保存到我的 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());
     }
}

【问题讨论】:

    标签: c# excel ms-access import


    【解决方案1】:

    像这样尝试并反馈。

    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();
    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-19
      • 1970-01-01
      • 2021-02-25
      • 2011-11-16
      • 2010-12-04
      相关资源
      最近更新 更多