Comment lire une feuille spécifique dans un file Excel à l'aide de Microsoft.Office.Interop.Excel

Je veux lire une feuille spécifique dans un file Excel en utilisant "Microsoft.Office.Interop.Excel" . Mon code va comme

private static Microsoft.Office.Interop.Excel.ApplicationClass appExcel; private static Workbook newWorkbook = null; private static _Worksheet objsheet = null; static void excel_init(Ssortingng path) { appExcel = new Microsoft.Office.Interop.Excel.ApplicationClass(); if (System.IO.File.Exists(path)) { newWorkbook = appExcel.Workbooks.Open(path, true, true); objsheet = (_Worksheet)appExcel.ActiveWorkbook.ActiveSheet; } else { MessageBox.Show("Unable to open file!"); } } 

Comment modifier cela pour lire une fiche spécifique?

Vous devriez essayer de cette façon:

 static void Main(ssortingng[] args) { try { OleDbConnection oledbConn; ssortingng path = System.IO.Path.GetFullPath(@"D:\FileName.XLS"); oledbConn = new OleDbConnection(@"provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + path + "';Extended Properties=Excel 8.0;"); oledbConn.Open(); OleDbCommand cmd = new OleDbCommand(); ; OleDbDataAdapter oleda = new OleDbDataAdapter(); DataSet ds = new DataSet(); cmd.Connection = oledbConn; cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT * FROM [sheet2]"; oleda = new OleDbDataAdapter(cmd); oleda.Fill(ds, "dsSlno"); oledbConn.Close(); } catch (Exception) { throw; } } 

comme ci-dessous

 var sheet = (_Worksheet)appExcel.ActiveWorkbook.Sheets["Sheet2"]; sheet.Select(Type.Missing);