Convertir la gamme Excel par ADO.NET DataSet ou DataTable, etc.

J'ai une feuille de calcul Excel qui sera installée sur un lecteur de partage réseau. Il doit être consulté par mon application Winforms C # 3.0 (plusieurs users pourraient utiliser l'application et bash cette feuille de calcul en même time). Il existe beaucoup de données sur une feuille de calcul. Ces données sont divisées en zones que j'ai nommées en tant que gammes. Je dois pouvoir accéder à ces plages individuellement, renvoyer chaque gamme en tant que jeu de données, puis lier à une grid.

J'ai trouvé des exemples qui utilisent OLE et que ceux-ci fonctionnent. Cependant, j'ai vu des avertissements sur l'utilisation de cette méthode, plus au travail, nous utilisons Microsoft.Office.Interop.Excel comme norme jusqu'à présent. Je ne veux vraiment pas m'envoyer de ce fait, sauf si je le dois. Nos users utiliseront Office 2003 pour autant que je sache.

Je peux get la gamme dont j'ai besoin avec le code suivant:

MyDataRange = (Microsoft.Office.Interop.Excel.Range) MyWorkSheet.get_Range("MyExcelRange", Type.Missing); 

La manière OLE était agréable car elle prendrait ma première rangée et les transformerait en colonnes. Mes plages (12 total) sont pour la plupart différentes les unes des autres en nombre de colonnes. Je ne savais pas si cette information affecterait toute recommandation.

Existe-t-il un moyen d'utiliser Interop et de refind la gamme renvoyée dans un jeu de données?

Je ne connais pas une fonction embeddede, mais il ne devrait pas être difficile de l'écrire vous-même. Pseudocode:

 DataTable MakeTableFromRange(Range range) { table = new DataTable for every column in range { add new column to table } for every row in range { add new datarow to table for every column in range { table.cells[column, row].value = range[column, row].value } } return table } DataTable MakeTableFromRange (plage de scope) DataTable MakeTableFromRange(Range range) { table = new DataTable for every column in range { add new column to table } for every row in range { add new datarow to table for every column in range { table.cells[column, row].value = range[column, row].value } } return table } table = new DataTable DataTable MakeTableFromRange(Range range) { table = new DataTable for every column in range { add new column to table } for every row in range { add new datarow to table for every column in range { table.cells[column, row].value = range[column, row].value } } return table } pour chaque colonne dans la gamme DataTable MakeTableFromRange(Range range) { table = new DataTable for every column in range { add new column to table } for every row in range { add new datarow to table for every column in range { table.cells[column, row].value = range[column, row].value } } return table } append une nouvelle colonne au tableau DataTable MakeTableFromRange(Range range) { table = new DataTable for every column in range { add new column to table } for every row in range { add new datarow to table for every column in range { table.cells[column, row].value = range[column, row].value } } return table } pour chaque rangée dans la gamme DataTable MakeTableFromRange(Range range) { table = new DataTable for every column in range { add new column to table } for every row in range { add new datarow to table for every column in range { table.cells[column, row].value = range[column, row].value } } return table } Ajouter un nouveau Datarow à table DataTable MakeTableFromRange(Range range) { table = new DataTable for every column in range { add new column to table } for every row in range { add new datarow to table for every column in range { table.cells[column, row].value = range[column, row].value } } return table } pour chaque colonne dans la gamme DataTable MakeTableFromRange(Range range) { table = new DataTable for every column in range { add new column to table } for every row in range { add new datarow to table for every column in range { table.cells[column, row].value = range[column, row].value } } return table } table.cells [colonne, rangée] .value = scope [colonne, rangée] .value DataTable MakeTableFromRange(Range range) { table = new DataTable for every column in range { add new column to table } for every row in range { add new datarow to table for every column in range { table.cells[column, row].value = range[column, row].value } } return table } tableau de return DataTable MakeTableFromRange(Range range) { table = new DataTable for every column in range { add new column to table } for every row in range { add new datarow to table for every column in range { table.cells[column, row].value = range[column, row].value } } return table } 

Je ne sais pas quel type de données vous avez. Mais pour une donnée Excel telle que présentée dans ce lien http://www.freeimagehosting.net/image.php?f8d4ef4173.png , vous pouvez utiliser le code suivant pour charger dans datatables table.

  private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; } private void Form1_Load (object sender, EventArgs e)  private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; } DataTable sheetTable = loadSingleSheet (@ "C: \ excelFile.xls", "Sheet1 $");  private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; } dataGridView1.DataSource = sheetTable;  private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; } capture (Exception Ex)  private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; } MessageBox.Show (Ex.Message, "");  private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; } private OleDbConnection returnConnection (ssortingng fileName)  private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; } returnne le nouveau OleDbConnection ("Provider = Microsoft.Jet.OLEDB.4.0; Data Source =" + fileName + "; Jet OLEDB: Type de moteur = 5; properties étendues = \" Excel 8.0; \ "");  private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; } Private DataTable loadSingleSheet (ssortingng fileName, ssortingng sheetName)  private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; } DataTable sheetData = new DataTable ();  private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; } en utilisant (OleDbConnection conn = this.returnConnection (nom de file))  private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; } conn.Open ();  private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; } // récupère datatables à l'aide d'un adaptateur de données  private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; } OleDbDataAdapter sheetAdapter = nouvel OleDbDataAdapter ("select * from [" + sheetName + "]", conn);  private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; } SheetAdapter.Fill (sheetData);  private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; } feuille de returnDonnées;  private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; } 

Il vaut la peine d'examiner NPOI lorsqu'il s'agit de lire / écrire des files Excel 2003 XLS. NPOI est un économiseur de vie.

Je pense que vous devrez itérer votre gamme et créer DataRows pour mettre votre DataTable.

Cette question sur StackOverflow fournit plus de ressources:

Créer un file Excel (.XLS et .XLSX) à partir de C #

Cette méthode ne fonctionne pas bien lorsque la même colonne de la feuille de calcul excel contient du text et des numbers. Par exemple, si Range("A3")=Hello and Range("A7")=5 il lit seulement Hello et la valeur pour Range("A7") est DBNULL

 private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; private void Form1_Load (object sender, EventArgs e) private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; DataTable sheetTable = loadSingleSheet (@ "C: \ excelFile.xls", "Sheet1 $"); private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; dataGridView1.DataSource = sheetTable; private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; capture (Exception Ex) private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; MessageBox.Show (Ex.Message, ""); private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; private OleDbConnection returnConnection (ssortingng fileName) private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; returnne le nouveau OleDbConnection ("Provider = Microsoft.Jet.OLEDB.4.0; Data Source =" + fileName + "; Jet OLEDB: Type de moteur = 5; properties étendues = \" Excel 8.0; \ ""); private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; Private DataTable loadSingleSheet (ssortingng fileName, ssortingng sheetName) private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; DataTable sheetData = new DataTable (); private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; en utilisant (OleDbConnection conn = this.returnConnection (nom de file)) private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; conn.Open (); private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; // récupère datatables à l'aide d'un adaptateur de données private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; OleDbDataAdapter sheetAdapter = nouvel OleDbDataAdapter ("select * from [" + sheetName + "]", conn); private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData; SheetAdapter.Fill (sheetData); private void Form1_Load(object sender, EventArgs e) { try { DataTable sheetTable = loadSingleSheet(@"C:\excelFile.xls", "Sheet1$"); dataGridView1.DataSource = sheetTable; } catch (Exception Ex) { MessageBox.Show(Ex.Message, ""); } } private OleDbConnection returnConnection(ssortingng fileName) { return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\""); } private DataTable loadSingleSheet(ssortingng fileName, ssortingng sheetName) { DataTable sheetData = new DataTable(); using (OleDbConnection conn = this.returnConnection(fileName)) { conn.Open(); // resortingeve the data using data adapter OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "]", conn); sheetAdapter.Fill(sheetData); } return sheetData;