从Excel表中读取数据包含多个表c#

问候

可以说我有一个excel文件包含一张表,在这张表中有两个表用于雇员与字段[empID,empName,标题] ,,,另一个表部门与字段[deptId,deptName],我wana知道如何从Excel文件中读取数据,并加载数据表中的一个emplyee和其他部门,我已经search,并发现我可以通过“select * from [sheet $] oleDbconnection与Excel中,但在我的情况下,工作表中包含两个表结构不同。

日Thnx

我正在研究windows应用程序,, vs2010 c#

制作SheetSelectionForm将这些代码放在窗体上,然后调用PopulateSheetsOfExcelFile(excelFilePath); 方法。 本表格将向您显示Excell表格的名称,您可以select要从Excel中读取的表格。 正如你所说,这些表格有不同的结构,所以你需要制作不同的DataTables的Excel表格。 有一篇关于如何读取http://www.serkanhekimoglu.com/?p=105的 excel文件的文章, 请查看。 检查代码块。 你可以问我什么 还有另一种方式来读取DataAdapter的整个Excel。 用我的方法,你创build自定义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(); } }