2
OpenFileDialog ofImport = new OpenFileDialog();
ofImport.Title = "Select file";
ofImport.InitialDirectory = @"c:\";
ofImport.FileName = txtFileName.Text;
ofImport.Filter = "Excel Sheet(*.xlsx)|*.xlsx|All Files(*.*)|*.*";
ofImport.FilterIndex = 1;
ofImport.RestoreDirectory = true;

if (ofImport.ShowDialog() == DialogResult.OK)
{

     string path = System.IO.Path.GetFullPath(ofImport.FileName);
     string query = "SELECT * FROM Customer.xlsx";
     OleDbConnection conn = new OleDbConnection();
     conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+ofImport.FileName+";Extended Properties=" + "\"Excel 12.0 Xml;HDR=YES;IMEX=1\"";
     OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn);

     //DataSet dataSet = new DataSet();
     adapter.Fill(dsSource);
     dataGridView1.DataSource = dsSource;

}
else
{
     ofImport.Dispose();
}   

我想将 Excel 数据检索到DataGridViewusing dataset. dsSource是使用的数据集。

我得到的错误在线adapter.Fill(dsSource);

Microsoft Access 数据库引擎找不到对象“xlsx”。确保对象存在并且正确拼写其名称和路径名。如果“xlsx”不是本地对象,请检查您的网络连接或联系服务器管理员。

我可以选择文件,但它没有填写数据集。

该怎么办?

4

3 回答 3

4

在以下行中,您从文件中选择:

string query = "SELECT * FROM " + ofImport.FileName;

但是,您需要从sheet中进行选择,因此应该是这样的:

string query = "SELECT * FROM [Sheet1$]"; // Note the '$' sign!!

You need to find out the sheet name of the Excel file so you can select from it (the sheet name is displayed in the tab for the sheet - just append a $ sign to it). The file name is only used in the connection string so the database engine knows which file to open.

Think of the following analogy with usual SQL database access in .NET:

file name = database name
sheet name = table name

EDIT
To make things clearer: In the following picture, the sheet name is circled red. In your code, write the sheet name in the select statement, followed by a dollar sign.

enter image description here

于 2012-12-06T09:02:08.623 回答
3

this my code to read excel to datagridview without database configuration.

        DataTable dt = new DataTable("dataTable");
        DataSet dsSource = new DataSet("dataSet");
        dt.Reset();

        Excel.Workbook ExWorkbook;
        Excel.Worksheet ExWorksheet;
        Excel.Range ExRange;
        Excel.Application ExObj = new Excel.Application();

        openFileDialog1.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";
        DialogResult result = openFileDialog1.ShowDialog();

        if (result == DialogResult.OK) // Test result.
        {
            ExWorkbook = ExObj.Workbooks.Open(openFileDialog1.FileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            ExWorksheet = (Excel.Worksheet)ExWorkbook.Sheets.get_Item(1);
            ExRange = ExWorksheet.UsedRange;

            for (int Cnum = 1; Cnum <= ExRange.Columns.Count; Cnum++)
            {
                dt.Columns.Add(new DataColumn((ExRange.Cells[1, Cnum] as Excel.Range).Value2.ToString()));
            }
            dt.AcceptChanges();

            string[] columnNames = new String[dt.Columns.Count];
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                columnNames[0] = dt.Columns[i].ColumnName;
            }
            //string[] columnNames = (from dc in dt.Columns.Cast<DataColumn>() select dc.ColumnName).ToArray();

            for (int Rnum = 2; Rnum <= ExRange.Rows.Count; Rnum++)
            {
                DataRow dr = dt.NewRow();
                for (int Cnum = 1; Cnum <= ExRange.Columns.Count; Cnum++)
                {
                    if ((ExRange.Cells[Rnum, Cnum] as Excel.Range).Value2 != null)
                    {
                        dr[Cnum - 1] = (ExRange.Cells[Rnum, Cnum] as Excel.Range).Value2.ToString();
                    }
                }
                dt.Rows.Add(dr);
                dt.AcceptChanges();
            }
            ExWorkbook.Close(true, Missing.Value, Missing.Value);
            ExObj.Quit();

            dataGridView1.DataSource = dt;  
于 2014-01-30T02:44:28.730 回答
1
    DialogResult dialogResult = MessageBox.Show("Sure", "Some Title", MessageBoxButtons.YesNo);
    if (dialogResult == DialogResult.Yes)
    {
        dt = dsSource.Tables[Index];
        dt.Reset();
        Excel.Workbook workbook;
        Excel.Worksheet NwSheet;
        Excel.Range ShtRange;
        Microsoft.Office.Interop.Excel.Application ExcelObj = new Microsoft.Office.Interop.Excel.Application();
        OpenFileDialog filedlgExcel = new OpenFileDialog();
        filedlgExcel.Title = "Select file";
        filedlgExcel.InitialDirectory = @"c:\";
        //filedlgExcel.FileName = textBox1.Text;
        filedlgExcel.Filter = "Excel Sheet(*.xlsx)|*.xlsx|All Files(*.*)|*.*";
        filedlgExcel.FilterIndex = 1;
        filedlgExcel.RestoreDirectory = true;
        if (filedlgExcel.ShowDialog() == DialogResult.OK)
        {

            workbook = ExcelObj.Workbooks.Open(filedlgExcel.FileName, Missing.Value, Missing.Value,
                 Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                 Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            NwSheet = (Excel.Worksheet)workbook.Sheets.get_Item(1);
            ShtRange = NwSheet.UsedRange;
            for (int Cnum = 1; Cnum <= ShtRange.Columns.Count; Cnum++)
            {
                dt.Columns.Add(new DataColumn((ShtRange.Cells[1, Cnum] as Excel.Range).Value2.ToString()));
            }
            dt.AcceptChanges();
            string[] columnNames = new String[dt.Columns.Count];
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                columnNames[0] = dt.Columns[i].ColumnName;
            }
            //string[] columnNames = (from dc in dt.Columns.Cast<DataColumn>() select dc.ColumnName).ToArray();


            for (int Rnum = 2; Rnum <= ShtRange.Rows.Count; Rnum++)
            {
                DataRow dr = dt.NewRow();
                for (int Cnum = 1; Cnum <= ShtRange.Columns.Count; Cnum++)
                {
                    if ((ShtRange.Cells[Rnum, Cnum] as Excel.Range).Value2 != null)
                    {
                        dr[Cnum - 1] = (ShtRange.Cells[Rnum, Cnum] as Excel.Range).Value2.ToString();
                    }
                }
                dt.Rows.Add(dr);
                dt.AcceptChanges();
            }
            workbook.Close(true, Missing.Value, Missing.Value);
            ExcelObj.Quit();

            dataGridView1.DataSource = dt;  
于 2012-12-11T08:57:35.180 回答