简体   繁体   中英

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].

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 .

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.

I am working on a Windows application using VS2010 C#.

Firstly, make a SheetSelectionForm. Then put these codes on your form, and call PopulateSheetsOfExcelFile(excelFilePath) method. This Form will show you to names of Excel sheets and you can select which sheet you want to read from Excel.

As you say, these tables have different structure, so you need to make different DataTables for each sheet of Excel.

There is another way to read whole Excel with DataAdapter. With my method, you create custom DataTable, and fill that with giving excel column/row index.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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