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.
Related
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.
The support for working with defined names in SpreadsheetGear isn't as good as for other components of Excel.
Can this be done? If so, how?
When I copy in a sheet from another workbook, the scope is limited to that worksheet, and I'd like to be able to apply it to the whole workbook.
This is how you define a name with workbook scope
SpreadsheetGear.IWorkbook workbook = workbookView.ActiveWorkbook;
SpreadsheetGear.INames definedNames = workbook.Names;
definedNames.Add(name, refTo, SpreadsheetGear.ReferenceStyle.A1);
definedNames[name].Comment = "SomeComment";
definedNames[name].Visible = true;
"When I copy in a sheet from another workbook, the scope is limited to that worksheet"
If I understand you correclty, you cannot do what you want and it is logically impossible. The defined names always should have 'workbook scope', that is, it is workbook.Names that holds the defined names information. Now based on this fact, if you copy a sheet from workbookA to workbookB, the sheet holds nothing about the defined names of that workbook (workbookA.Names), thus it can never hold their references.
I hope this helps.
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.
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();
Im using ExcelQueryFactory to retrieve the column names of a worksheet using C# 4.0.I'm able to see the list of column names but why do i get an additional set of column names like F12,F100,F20 etc . It happens with any reader i used to read my excel file.
It's not an C# issue, that is an Excel issue.
if your columns do not have column names, Excel will provide names in the format F###, where the number is the number of the column, and F is short for Field (I guess).
Now, if you have any columns in the file that at anytime contained data, or are referenced in formulas, or Excel just thinks they are important, you'll get them in the column list.
Just filter out any columns in that format, of course with the caveat that real columns with real names like F7 will get the short end of the stick there.