This topic is related to: What object type are multiple selected sheets in Excel?
To give an overview of the problem, I run some code that changes my selection in Excel, and I want to return the selection to what it originally was (I call this "originalSelection"). In most instances, you can just invoke the Select() method on the originalSelection.
var originalSelection = ExcelApp.Selection;
originalSelection.GetType().InvokeMember("Select", System.Reflection.BindingFlags.InvokeMethod, null, originalSelection , null);
With multiple sheets selected, the selection type is always of a range (it's what Excel defaults to). However, if you have multiple sheets selected, you can run into errors when trying to invoke Select again. You need to do some dancing around to get things to work.
If multiple sheets are selected, but not all the sheets, you can do the follow:
selectedSheets.Select();
activeSheet.Activate();
originalSelection.Select(); //this was casted to an Excel.Range
However, if all the sheets are selected, the activeSheet.Activate() line deselects all the other selected sheets. This also happens if you try it natively using the UI.
I was wondering if there is a way to pragmatically mimic shift-selecting sheets one by one through the code? Closest stuff I've found is stuff with range groupings, but nothing for sheets.
I tried to keep my overview brief, but if you need more clarification on what I'm doing, just ask.
So I figured out a way programatically select sheets.
You can create a string array of names, and use the ordering of the array to get a collection of the sheets. Select this collection, and you should have all the specified sheets selected.
String[] sheetsToBeSelected = {"Sheet3","Sheet1","Sheet2"}; //Note, Sheet3 is the first item in this array
excel.Workbook workbook = ExcelApp.ActiveWorkbook; //get your Excel application however you want
excel.Sheets worksheets = workbook.Worksheets; //get all the sheets in this workbook
//This gets a collection of the sheets specified and ordered by the array of names passed in.
//Just call select on this collection, and the first sheet in the collection becomes the active sheet!
((excel.Sheets)worksheets.get_Item(sheetsToBeSelected)).Select();
Related
I have a C# code that connects to Excel database via OpenFileDialog func. Excel file has too many worksheets and I would like to get all their names in one array and add them to combobox.
For example, this one is the first Excel document.
Any suggestions?
You could try the following:
Accord.IO.ExcelReader reader = new Accord.IO.ExcelReader("yourFile.xlsx");
string[] worksheets = reader.GetWorksheetList();
make sure to include
using Accord.IO;
which you can get Accord from NuGet by searching Accord and Accord.IO.
Then just fill in the items of the Combo box
foreach (string worksheet in worksheets)
{
myComboBox.Items.Add(worksheet);
}
I'm trying to return the active sheet from the workbook.SheetActivate event as a Microsoft.Office.Tools.Excel.Worksheet object but I'm having some real issues!
I'm creating worksheets at runtime by copying one worksheet, and then renaming it. The worksheet I'm copying is a template which houses the look and feel for each generated excel sheet.
I need to get the Microsoft.Office.Tools.Excel.Worksheet object because I then need to add a button control to the stated excel sheet.
This is how I've tried to accomplish this:
void Application_SheetActivate(object Sh)
{
Excel._Worksheet test = Sh as Excel._Worksheet;
Worksheet worksheet = Globals.Factory.GetVstoObject(test);
MessageBox.Show(worksheet.Name.ToString());
}
This works for the Sheet1, but the template sheet and anything else that gets started up gets this error:
System.NullReferenceException. Object not sent to an instance of an object. After running the debugger I can see that the first excel sheet is assigned correctly, but any other ones aren't... I know what the error states and what it means.
What am I doing incorrectly here? When a sheet tab gets clicked, all I want to be able to do is return a Microsoft.Office.Tools.Excel.Worksheet type, but boy I've never had so many issues!
I have a
Excel.Workbook book = application.Workbooks.Open(txtPathExcel.Text);
Excel.Worksheet mainSheet = (Excel.Worksheet)book.Sheets[1];
How can i add a new row in mainSheet? I can't find any NetOffice functions for it.
All rows are already exist in the sheet, you just need to select the required.
The Rows property of the Application class returns a Range object that represents all the rows on the active worksheet. For example:
Worksheets("Sheet1").Rows(3)
The range object refers to the third row on the sheet.
Also you may find the Insert method helpful. It inserts a cell or a range of cells into the worksheet or macro sheet and shifts other cells away to make space.
I am trying to get names of the selected sheets in Excel. I have 4-5 worksheets in my excel file. User is supposed to select two of them and then my application scans specific columns and compare values. However I could not find a way in C# to get names of the worksheets when user selects more than one sheet. User can also delete these selected worksheets via the application. Any ideas?
Using VSTO:
var sheets = Application.ActiveWindow.SelectedSheets;
var names = new List<string>();
foreach (Excel.Worksheet sh in sheets)
{
names.Add(sh.Name);
}
One way is to use Spreadsheetgear: Link to Spreadsheetgear website
This is the easiest solution to manipulating Excel workbooks I tryed. But quite expensive.
If you're after a free solution, have a look at Excel Package Plus: http://epplus.codeplex.com/
It should have all the features that you require.
I have an Excel Workbook that has several worksheets in it. The tables in the worksheets have been assigned names using the Name Manager in Excel. Is there a way to get a list of these table names, instead of the worksheet names?
For instance, where workbook is defined as a Microsoft.Office.Interop.Excel.Workbook:
var names = (workbook.Names as System.Collections.IEnumerable).Cast<Name>();
returns a list of defined name ranges in the workbook. Additionally:
var worksheets = workbook.Worksheets.Cast<Worksheet>();
returns a list of worksheets in the workbook, whose names are the same as the tab names of the worksheets.
Neither of these list the tables by their assigned names. They only list the tables by their worksheet name. I hope I've made the question clear enough, but I can provide an example if need be.
For historical reasons, Tables in Excel are called Lists in the OM.
What you are looking for are ListObjects on a Worksheet. A ListObject has a Range object attached to it, which you can use to figure out the name. DisplayName may return the name as well - I am not sure.