在特定工作表上打开Excel文件

Ere*_*rez 11 c# excel

我有一个带有5个工作表的Excel文件,我希望用c#代码打开它,当它打开时,我希望激活工作表编号3.

我怎样才能做到这一点?

Ton*_*ion 26

像这样:

 using Excel; 

 Excel.Application excelApp = new Excel.ApplicationClass();

  // if you want to make excel visible to user, set this property to true, false by default
  excelApp.Visible = true;

 // open an existing workbook
 string workbookPath = "c:/SomeWorkBook.xls";
    Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath,
        0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
        true, false, 0, true, false, false);



// get all sheets in workbook
   Excel.Sheets excelSheets = excelWorkbook.Worksheets;

  // get some sheet
 string currentSheet = "Sheet1";
    Excel.Worksheet excelWorksheet = 
        (Excel.Worksheet)excelSheets.get_Item(currentSheet);

 // access cell within sheet
  Excel.Range excelCell = 
        (Excel.Range)excelWorksheet.get_Range("A1", "A1");
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助

MDSN信息在这里