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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2012282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:39:06  来源:igfitidea点击:

Open Excel File on a specific worksheet

c#excel

提问by Erez

I have an Excel file with 5 worksheets and I want with c# code to open it and when it is opened I want the sheet number 3 to be activated.

我有一个包含 5 个工作表的 Excel 文件,我想用 c# 代码打开它,当它打开时,我想激活第 3 个工作表。

How can I do that?

我怎样才能做到这一点?

采纳答案by Tony The Lion

Like this:

像这样:

 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");

Hope this helps

希望这可以帮助

MDSN info here

MDSN信息在这里

回答by Rubens Farias

What about something like this: (untested)

这样的事情怎么样:(未经测试)

//using Excel = Microsoft.Office.Interop.Excel;

Excel.ApplicationClass app = new Excel.ApplicationClass();
Excel.Workbook workbook = app.Workbooks.Open("YourFile.xls", 
    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
    Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Sheets["Number 3"];
worksheet.Activate();

回答by user261875

If wanting to present visual feedback to the User, these two statements will set the activated sheet and select the range accordingly:

如果想向用户呈现视觉反馈,这两个语句将设置激活的工作表并相应地选择范围:

Consider including the following statement immediately prior to initializing the Excel.Range...

考虑在初始化 Excel.Range 之前立即包含以下语句...

// Set Active sheet in Excel

// 在 Excel 中设置活动工作表

excelWorksheet.Activate()

excelWorksheet.Activate()

Also consider the following statement immediately after initializing the Excel.Range...

还要在初始化 Excel.Range 后立即考虑以下语句...

// Set Active range in Excel

// 在 Excel 中设置活动范围

excelCell.Activate()

excelCell.Activate()

回答by Makatun

public static Workbook openExternalWorkBook(String fileName)
    {
        Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();            
        excel.Visible = false;
        return excel.Workbooks.Open(fileName, false);
    }