In VSTO, in developing an excel add-in, i've an excel area name, is it possible to get the worksheet it belong to?
Is it also possible to get the worksheet that a cell belong to?
If you have a range object just use .parent to get the worksheet. Then you can get the name of the worksheet or whatever else.
If you only have a range name as a string then you need to loop through the names in the workbook and worksheets looking for that name. Then use the refersTo property to get the address. Parse for the worksheet name.
Related
I have found two ways those were being used by people if we want to read or write to a cell in an excel.
Declaration:
Excel.Application ExcelApp = new Excel.Application();
Excel.Workbook srcWorkBook = ExcelApp.Workbooks.Open(#"C:\test.xls");
Excel.Worksheet srcWorkSheet = srcWorkBook.Worksheets[1];
Excel.Range srcRange = srcWorkSheet.UsedRange;
Usage 1:
srcWorkSheet.Cells[1, 1].value2 = "foo bar";
Usage 2:
srcRange.Cells[2, 2].Value2 = "foo bar";
Which one is the best way to use ? or it's all fine in .NET ?
The two ways are very different.
srcWorkSheet.Cells[1, 1].value2 = "foo bar";
The 1. usage refers to the A1 cell of the first worksheet.
MSDN Worksheets.Cells property Excel
srcRange.Cells[2, 2].Value2 = "foo bar";
The 2. usage takes the cell on the 2. row, 2. column of the UsedRange. If the UsedRange in Excel is B2:D5, it would put value at C3:
MSDN Worksheets.Range Property Excel
Having a variable named Range as the Range class is really not a great idea. The same goes for WorkSheet and WorkBook.
Either option will work, but they get messy as soon as you have to modify the layout of the worksheet.
If it's an option, I like to add named ranges. This works particularly well if you're using a template where you can modify the "starting" state of the workbook.
Let's say you have a series of columns, and one of them contains the "foo" value. You can add a name like "fooColumn" to the entire column or the top cell in the column.
Then you can get the column number using
worksheet.Range("fooColumn").Column
That protects you from having to manually edit column or row numbers all over the place if you move elements around on your worksheet.
And to second what others have said, use EPPlus instead of Interop. You can still use named ranges. But EPPlus is good and Interop can bring all sorts of pain when COM objects aren't released. Interop automates an application that manipulates the file. EPPlus just works with the file.
iterating through all sheets in workbook using openxml(C#) but want to skip a specific sheet based on its name.Please suggest how it can be done.
The sheet names are stored in the WorkbookPart in a Sheets element
which has children of element Sheet which corresponds to each
worksheet in the Excel file.
See the answer given in this question.
Make your own loop and skip the sheet according to its name.
I'm currently working on a spreadsheet reader that needs to read the second worksheet in the ExcelFile. I've looked only but can't find anywhere that references how to set the activeworksheet.
Currently my active worksheet is set like this:
ExcelWorksheet worksheet = excelFile.Worksheets.ActiveWorksheet;
When debugging, i noticed that it's reading the first worksheet out of the two, when i need it to be reading the second file.
How would i be able to set the active worksheet to an index of 1 rather than an index of 0.
Thanks
UPDATE:
I fixed this by querying using Linq, that went through the ExcelFile Worksheets and setting the worksheet index. Example code below:
ExcelWorksheet worksheet = excelFile.Worksheets.Where(x => x.Index = 1).SingleOrDefault();
First note that active worksheet is the one selected when file is opened with some Excel application (like MS Excel), see ActiveWorksheet property's help page.
You can set any sheet to be an active one and usually (by default) it's the first one in the workbook, so that is why you're accessing the first sheet with it. But in order to access any sheet that you want you can retrieve it from the Worksheets collection through indexer, like the following:
ExcelWorksheet firstSheet = excelFile.Worksheets[0];
ExcelWorksheet secondSheet = excelFile.Worksheets[1];
Or with the sheet's name, like the following:
ExcelWorksheet firstSheet = excelFile.Worksheets["Sheet1"];
ExcelWorksheet secondSheet = excelFile.Worksheets["Sheet2"];
See Worksheets properties on help page.
I'm trying to create a WinForms application that interacts with Excel using the Excel Object Library v.15.
I'm using this to get the Excel Application object
(Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
And I know I can get the Range for the named table I want by using
_application.Range["MyTableName"];
Now, the problem I'm facing is that if I have two workbooks opened at the same time and each one of them has a named table with the same name, I don't know which Range will be returned.
The Excel application is unique, so I cannot simply try to get the process based on the window title or something like that.
I can get the Workbook object based on its title:
_application.Workbooks.Cast<Workbook>().FirstOrDefault(w.Name.Equals(title))
However I cannot access the Ranges of a particular Workbook.
I know I could iterate through the Sheets of the Workbook element trying to find the table but I was wondering if there was another "cleaner" way.
I would appreciate any guideline on this.
Thanks,
Will
The simplest/most direct way I can think of would be to iterate through the Names collection, check the parent, and move on.
E.g:
For Each NamedRange as Range in MyApplication.Names
Dim Check as Object = TryCast(NamedRange.Parent, Workbook)
If Check Is Nothing Then
Check = TryCast(NamedRange.Parent, Worksheet)
Check = TryCast(Check.Parent, Workbook)
End If
If Not Check Is Nothing AndAlso Check.Name = "MyWorkbook" Then
'Do something
End If
Next
Again a Excel question.
My client send me a Excel File that contains a Drop Down List (Combobox), and i need copy that Combobox or build a new with the information inside.
For excel i find that dropdownlist was named as Data Validation (i already got it, but can do nothing with him).
I Have Gembox dll and NativeExcel dll and didnt find any solution.
With GemBox i already get this:
ExcelFile ef = ExcelFile.Load("Modelo_AA.xlsx");
ExcelWorksheet ws = ef.Worksheets[0];
ExcelFile efnovo = new ExcelFile();
ExcelWorksheet wsnovo = efnovo.Worksheets.Add("Hello");
DataValidationCollection dvc = ws.DataValidations;
DataValidation dv = dvc[0];
bool dd = dv.InCellDropdown; //here i get true
Thanks
Andrew
Edit: I suposed the datavalitation is associated with dropdownlist!
Edit2: Main problem is copy a drop down list from a WorkBook to other!
First I would like to say that yes in your case DataValidation is associated with dropdownlist because it is a List type (see dv.Type property).
Also to copy this list DataValidation into another excel file it will depend on DataValidation source, as mentioned in the comments by the shahkalpesh they can be hard-coded or they can come from a cell range. You can check the source of DataValidation by getting a dv.Formula1 object.
For example if it is hard-coded then the Formula1 will be an array of those list items and you can just add that DataValidation into another ExcelFile:
wsnovo.DataValidations.Add(dv);
But if that DataValidation has a cell range in Formula1 then you will have to copy that cell range in a new ExcelFile as well or you can try reading the values of cells in that cell range and replace a Formula1 with a hard-coded list of items.