[英]Open an excel worksheet
How to open a specific workbook of an excel sheet? 如何打开Excel工作表的特定工作簿? Currently I am using 目前我正在使用
System.Diagnostics.Process.Start(path);
but it just opens that excel. 但它只是打开了那个优势。 I would like to focus in to a specific worksheet. 我想关注一个特定的工作表。
How to do it? 怎么做? Is it possible using 是否可以使用
Microsoft.Office.Interop.Excel;
Add reference to Microsoft.Office.Interop.Excel in your project and you can use next code as base 在项目中添加对Microsoft.Office.Interop.Excel的引用,然后可以使用下一个代码作为基础
using Excel=Microsoft.Office.Interop.Excel;
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
//Start Excel and get Application object.
oXL = new Excel.Application();
oXL.Visible = false;
oXL.DisplayAlerts = false; //prevents message from popping up
try
{
//Get a new workbook.
oWB = (Excel._Workbook)(oXL.Workbooks.Open(filename,
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));
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
int nCounter = 1;
oSheet.Copy(oSheet, Type.Missing);
Excel._Worksheet oSheetGame = (Excel._Worksheet)oWB.Worksheets["MyTemplate"];
oSheetGame.Name = "MyNewWorksheetName";
//do something with worksheet
((Excel._Worksheet)oWB.Sheets["MyTemplate"]).Delete(); // delete template
((Excel._Worksheet)oWB.Worksheets["MyNewWorksheetName"]).Activate();
}
catch (Exception e)
{
//throw e;
throw;
}
finally
{
//Make sure Excel is visible and give the user control
//of Microsoft Excel's lifetime.
oXL.Visible = true;
oXL.UserControl = true;
}
oXL.Save(Type.Missing);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.