申请导入Excel数据表

时间:2013-06-18 16:42:49

标签: c# windows-applications

我想导入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;
    }

}
}

但代码提供提供商无法找到或未正确安装

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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());
            }
        }
    }