Excel Worksheet Modifying Current Sheet C#
Is there anyway that you can modify an already opened worksheet in Excel. Their MSDN page gives you what is below. This adds a new workbook and worksheet everytime. I have the code opening and existing worksheet, and I just want it to replace the value in the cell and the close. Any help is greatly appreciated.
using Excel = Microsoft.Office.Interop.Excel; //Excel Reference
public virtual Object ActiveSheet { get; set; }
private void button15_Click(object sender, EventArgs e)//Generate Model and Part Numbers
{
Excel.Application oXL;
开发者_JAVA百科 Excel._Workbook oWB;
Excel._Worksheet oSheet;
//Start Excel and get Application object.
oXL = new Excel.Application();
oXL.Visible = true;
//Get a new workbook.
oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
oSheet.Cells[6, 4] = "0"; //Change Value in Cell in Excel Cell Location [y-axis, x-axis]
}
you can iterate over oXL.Workbooks
to access the currently open Workbooks... the currently active Workbook is available via oXl.ActiveWorkbook
.
EDIT - as per comment:
Either use oXL.Windows
to find the already open window and call Activate
on that before before accessing ActiveWorkbook
or just call oXL.ActiveWindow.Close()
or oXL.ActiveWindow.ActivatePrevious()
to get the already open window...
EDIT 2 - as per comment final part of solution:
use
oXL = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
Here is what I came up with I hope this will help everyone out. Thanks for everyone who helped
using Excel = Microsoft.Office.Interop.Excel; //Excel Reference
public virtual Object ActiveSheet { get; set; }
private void button15_Click(object sender, EventArgs e)//Generate Model and Part Numbers
{
//Gets ActiveSheet to Modify
Excel.Application oXL;
Excel.Workbook oWB;
Excel.Worksheet oSheet;
//Start Excel and get Active Workbook and Sheet to modify
oXL = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
oXL.Visible = true;
oWB = (Excel.Workbook)oXL.ActiveWorkbook;
oSheet = (Excel.Worksheet)oWB.ActiveSheet;
//Cell Input
oSheet.Cells[6, 4] = "0"; //Change Value in Cell in Excel Cell Location [y-axis, x-axis]
}
If you open like below then you will create just a new instance. oXL = new Excel.Application();
c# will create a second excel process. With this way you can not access to any Excel application, which one opened by the user manually.
If you want to open active worksheet which one is opened not by your application then please check here: Get instance of Excel application with C# by Handle
all the rest is same:
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
if you want to have access to worksheet, which you open via c# instance then:(.net framework 4)
using Excel;
Excel.Application oXl = new Excel.Application();
Workbook oWb = oxl.workbooks.add();
Worksheet oWs = oWs.Worksheets(1); //As default excel will open 3 worksheet and active worksheet will be first one.
Edit: I didnt check the code block can be typing error.
Might be simpler to abandon the Interop Classes and use OLEDB to run an update query on the sheet. I can provide examples if your interested.
精彩评论