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.
Related
I'm creating two worksheets with openxml, I want to add a hyperlink from a cell in sheet 2 to a cell in sheet 1. I know that with Microsoft interop excel, you could use a match function to find the row number where a value is found. I was wondering how could the same task be done with openXML instead.
With Microsoft interop excel, I did something like this
var excelApp = new Application();
WorksheetFunction function = excelApp.WorksheetFunction;
var rowNumber = function.Match(currentItem.originID, sheet1.Range["B1","B" + rowCount], 0);
Where sheet1 is the sheet I'm searching through, and rowCount is how many rows there are in that sheet. This would find currentItem.originID in the range of column B and return the row number that the match was found
How could I do something similar to this but with openXML?
Is there a way to create an Excel workbook without any sheets in it?
This is the code that I use to create the workbook:
excelApp = new Microsoft.Office.Interop.Excel.Application();
excelBook = excelApp.Workbooks.Add();
I tried also adding
excelApp.SheetsInNewWorkbook = 0;
before the creation of the excelBook but the minimum value is 1 so the program crashes.
EDIT:
Tried also to delete the first sheet as soon as it is created but still doesn't work
Sheets excelSheetsToDelete = excelBook.Sheets[1];
excelSheetsToDelete.Delete();
I would like to be able to add my sheets with my names later without having to rename the first one.
You can't create an Excel without any sheet, Excel must contain at least one sheet. Try to delete single sheet in Excel application (desktop). You won't do that.
Like people have already said, Excel must contain at least 1 visible worksheet at all times. Since it seems like you just want to create a workbook, and manually specify your sheet name without having to rename the one created automatically, I wrote this quick VB Script that may work for you.
Dim objShell, objExcel, objWorksheet, strSheetName
strSheetName = InputBox("Enter a name for your new Worksheet:")
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("EXCEL.EXE")
WScript.Sleep 3000 'Wait for new excel file to open
Set objExcel = GetObject(, "Excel.Application")
Set objWorksheet = objExcel.ActiveWorkbook.Worksheets(1)
objWorksheet.Name = strSheetName
What this does is prompt you to enter a name for your new sheet. It then runs a new instance of Excel and sets the first worksheet of the newly created file as the name you specified.
If Excel includes 3 sheets when creating new workbooks, you can change this to 1 by going in any excel worbook and clicking - File > Options > General - and look for the "When the creating new workbooks" separator. There will be an option "Include this many sheets" which you can change to 1.
Also, to confirm what people have already said, if you try and change this value to 0, it will tell you "The entry must be greater than or equal to 1" meaning all workbooks must contain at least 1 visible worksheet. Hope this helped.
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.
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.
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.