blah blah blah is here! blah blah » Close

up0down
link

I just tried using Office Interop

Excel = Microsoft.Office.Interop.Excel;


I'm looking out for a way with which I can make a particular worksheet (as Active worksheet) in the Excel workbook.

Something like this:

if (checkBox1.checked)
// Excel.worksheet.active = "Sheet 1";
else
// Excel.worksheet.active = "Sheet 2";


The code
Excel.Application xlApp;
Excel.Workbook xlWorkBook ;
Excel.Worksheet xlWorkSheet ;
object misValue = System.Reflection.Missing.Value;
Excel.Range chartRange;

xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);

xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Name.Equals("Mark Sheet");
does not changes the name of the WorkSheet also.... I'm still trying to fist change the name of the workSheet then make a particular sheet active also.......

last answered one year ago

2 answers

up0down
link

This line:

xlWorkSheet.Name.Equals("Mark Sheet");

should be:
xlWorkSheet.Name = "Mark Sheet";

The Equals method is only used for comparisons.

To make, say, the second worksheet active:
Excel.Worksheet xlWorkSheet2 = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
xlWorkSheet2.Activate();

up0down
link

Error
Ambiguity between method 'Microsoft.Office.Interop.Excel._Worksheet.Activate()' and non-method ''Microsoft.Office.Interop.Excel.DocEvents_Events.Avctivate' Using method group

I'm just trying to open an existing file and save file with the required sheet as an active sheet. I did something like this:

if (openFile.ShowDialog() == DialogResult.OK)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
WelcomeForm welcome = (WelcomeForm)Application.OpenForms["WelcomeForm"];
RadioButton rb1 = (RadioButton)welcome.Controls["radiobutton1"];
if (rb1.Checked)
{
if (xlWorkSheet.Name != "SI Units")
{
Excel.Worksheet xlWorkSheet2 = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
xlWorkSheet2.Activate();
xlWorkBook.Save();
xlApp.Quit();
}
}
else
{
if (xlWorkSheet.Name != "FPS Units")
{
Excel.Worksheet xlWorkSheet2 = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
xlWorkSheet2.Activate();
xlWorkBook.Save();
xlApp.Quit();
}
}
}

But the file is not overwritten,
An empty file 'Book2.xls' is getting saved in 'My Documents' folder.

vulpes
17279

It's a compiler warning rather than an error - if you ignore it, it should work fine (it did when I tried it just now). If you want to get rid of the warning then change xlWorkSheet2.Activate(); to: ((Excel._Worksheet)xlWorkSheet2).Activate();

Feedback