简体   繁体   English

使用C#从包含多个表的Excel工作表中读取数据

[英]Reading data from Excel sheet containg multiple tables using C#

Let's say I have an Excel file contains one sheet, and in this sheet there is 2 tables one for employee with fields [empID,empName,title] and another table for department with fields [deptId,deptName]. 假设我有一个包含一个工作表的Excel文件,在此工作表中有2个表,一个表用于员工字段[empID,empName,title],另一表用于部门字段[deptId,deptName]。

I want to know how can I read data from that Excel file and load both tables in datatables, one for emplyee and the other for department . 我想知道我怎么可以从Excel文件中读取数据,并在数据表,一个用于加载两个表emplyee和其他的department

I have searched and find that I can query through sheet by "select * from [sheet$]" after making an oleDbconnection with Excel but in my case the sheet contains two tables different in structure. 我进行了搜索,发现可以在与Excel进行oleDbconnection之后通过“ select * from [sheet $]选择”来查询工作表,但就我而言,工作表包含两个结构不同的表。

I am working on a Windows application using VS2010 C#. 我正在使用VS2010 C#开发Windows应用程序。

Firstly, make a SheetSelectionForm. 首先,制作一个SheetSelectionForm。 Then put these codes on your form, and call PopulateSheetsOfExcelFile(excelFilePath) method. 然后将这些代码放在窗体上,并调用PopulateSheetsOfExcelFile(excelFilePath)方法。 This Form will show you to names of Excel sheets and you can select which sheet you want to read from Excel. 该表格将向您显示Excel工作表的名称,您可以选择要从Excel中读取的工作表。

As you say, these tables have different structure, so you need to make different DataTables for each sheet of Excel. 如您所说,这些表具有不同的结构,因此您需要为每张Excel表格创建不同的数据表。

There is another way to read whole Excel with DataAdapter. 还有另一种使用DataAdapter读取整个Excel的方法。 With my method, you create custom DataTable, and fill that with giving excel column/row index. 使用我的方法,您可以创建自定义DataTable,并使用excel列/行索引进行填充。

using System.Data.OleDb;


private void SelectItem() 
        { 
            ExcelSheetName = excelSheetsListBox.SelectedItem != null ? 
                excelSheetsListBox.SelectedItem.ToString() : string.Empty; 
            Close(); 
        }

private void PopulateSheetsOfExcelFile(string excelFilePath) 
        { 
            try 
            { 
                String connString = string.Empty;

                try 
                { 
                                      connString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;\"", excelFilePath); 
                    using (OleDbConnection objConn = new OleDbConnection(connString)) 
                    { 
                        objConn.Open(); 
                        using (DataTable dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null)) 
                        { 
                            if (dt == null) 
                                return;

                            excelSheetsListBox.Items.Clear();

                            for (int i = 0; i < dt.Rows.Count; i++) 
                            { 
                                DataRow row = dt.Rows[i]; 
                                excelSheetsListBox.Items.Add(row["TABLE_NAME"].ToString()); 
                            } 
                        } 
                    } 
                } 
                catch (Exception exA1) 
                { 
                                        connString = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"", excelFilePath); 
                    using (OleDbConnection objConn = new OleDbConnection(connString)) 
                    { 
                        objConn.Open(); 
                        using (DataTable dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null)) 
                        { 
                            if (dt == null) 
                                return;

                            excelSheetsListBox.Items.Clear();

                            for (int i = 0; i < dt.Rows.Count; i++) 
                            { 
                                DataRow row = dt.Rows[i]; 
                                excelSheetsListBox.Items.Add(row["TABLE_NAME"].ToString()); 
                            } 
                        } 
                    } 
                } 
            } 
            catch (Exception ex) 
            { 
               MessageBox.Show(“HATA”);

                ExcelSheetName = string.Empty; 
                Close(); 
            } 
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM