0

我想导入一个 excel 文件并遇到提供程序问题

代码是

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.IO;
using System.Data.SqlClient;
using System.Windows.Documents;
using System.Windows.Controls;
using ADOX;
namespace Import
{
public partial class Form1 : Form
    {
    public Form1()
    {
        InitializeComponent();
    }

    public static string SelectedTable = string.Empty;

    private void button1_Click_1(object sender, EventArgs e)
    {
        OpenFileDialog fdlg = new OpenFileDialog();
        fdlg.Title = "Select file";
        fdlg.InitialDirectory = @"c:\";
        fdlg.FileName = txtFileName.Text;
        fdlg.Filter = "Excel Sheet(*.xls)|*.xls|All Files(*.*)|*.*";
        fdlg.FilterIndex = 1;
        fdlg.RestoreDirectory = true;
        if (fdlg.ShowDialog() == DialogResult.OK)
        {
            txtFileName.Text = fdlg.FileName;
            Import();
            Application.DoEvents();
        }
    }

    private void Import()
    {
        if (txtFileName.Text.Trim() != string.Empty)
        {
            try
            {
                string[] strTables = GetTableExcel(txtFileName.Text);

                frmSelectTables objSelectTable = new frmSelectTables(strTables);
                objSelectTable.ShowDialog(this);
                objSelectTable.Dispose();
                if ((SelectedTable != string.Empty) && (SelectedTable != null))
                {
                    DataTable dt = GetDataTableExcel(txtFileName.Text, SelectedTable);
                    dataGridView1.DataSource = dt.DefaultView;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
    }

    public static DataTable GetDataTableExcel(string strFileName, string Table)
    {
        System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source = " + strFileName + "; Extended Properties = \"Excel 12.0;HDR=Yes;IMEX=1\";");
        conn.Open();
        string strQuery = "SELECT * FROM [" + Table + "]";
        System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(strQuery, conn);
        System.Data.DataSet ds = new System.Data.DataSet();
        adapter.Fill(ds);
        return ds.Tables[0];
    }

    public static string[] GetTableExcel(string strFileName)
    {
        string[] strTables = new string[100];
        Catalog oCatlog = new Catalog();
        ADOX.Table oTable = new ADOX.Table();
        ADODB.Connection oConn = new ADODB.Connection();
        oConn.Open("Provider=Microsoft.ACE.OLEDB.12.0; Data Source = " + strFileName + "; Extended Properties = \"Excel 12.0;HDR=Yes;IMEX=1\";", "", "", 0);
        oCatlog.ActiveConnection = oConn;
        if (oCatlog.Tables.Count > 0)
        {
            int item = 0;
            foreach (ADOX.Table tab in oCatlog.Tables)
            {
                if (tab.Type == "TABLE")
                {
                    strTables[item] = tab.Name;
                    item++;
                }
            }
        }
        return strTables;
    }

}
}

但是代码给出了Provider 找不到,或者没有正确安装

并改进代码中的 oledb 连接版本和 Excel 版本,但这并不能帮助我运行代码。当我尝试在应用程序中浏览 excel 文件时,我得到了那个错误或异常

4

2 回答 2

0

你的操作系统是64位的吗?您是否安装了 64 位的 Microsoft Excel?您是否安装了 64 位系统的驱动程序,该驱动程序位于: http ://www.microsoft.com/downloads/details.aspx?FamilyID=C06B8369-60DD-4B64-A44B-84B371EDE16D&displaylang=en

于 2013-06-18T16:56:36.590 回答
0
public void Form1_Load(object sender, EventArgs e)
    {
        OpenFileDialog fdlg = new OpenFileDialog();

        fdlg.Title = "Select file";
        fdlg.InitialDirectory = @"d:\";
        var txtFileName = fdlg.FileName;
        fdlg.Filter = "Excel Sheet(*.xlsx)|*.xlsx|Excel Sheet(*.xls)|*.xls|All Files(*.*)|*.*";
        fdlg.FilterIndex = 1;
        fdlg.RestoreDirectory = true;
        if (fdlg.ShowDialog() == DialogResult.OK)
        {
            txtFileName = fdlg.FileName;
            Import(txtFileName);
            System.Windows.Forms.Application.DoEvents();
        }
    }

    private void Import(string txtFileName)
    {
        if (txtFileName != string.Empty)
        {
            try
            {
                String name = "Sheet1";    // default Sheet1 
                String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                           txtFileName +
                            ";Extended Properties='Excel 12.0 XML;HDR=YES;';";

                OleDbConnection con = new OleDbConnection(constr);
                OleDbCommand oconn = new OleDbCommand("Select * From [" + name + "$]", con);
                con.Open();

                OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
                System.Data.DataTable data = new System.Data.DataTable();

                sda.Fill(data);
                dataGridView1.DataSource = data;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
    }
于 2014-07-05T09:15:28.023 回答