Copy Excel DropDown from WorkBook to a new Workbook - c#

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.

Related

Can ClosedXML create a formatted table from merged cells?

Based on documentation examples from: https://github.com/ClosedXML/ClosedXML/wiki/Using-Tables
In a C# application, I use ClosedXML to create a workbook with new content. Part of the content is a range of cells that I would like to style as a table. However, the first "column" of the table actually spans multiple cell columns. Without applying any table formatting, the result is like the following simplified snippet.
In my C# application, after I have populated the cells of that table, I grab the range of those cells and create an IXLTable from them.
IXLWorkbook book = ...;
IXLWorksheet sheet = ...;
//------------------------------
// Populate cells with milestone data.
...
//------------------------------
// Create a table.
IXLRange range = sheet.Range( "A1:D3");
IXLTable table = range.CreateTable();
//------------------------------
// Done.
book.SaveAs( "foo.xlsx" );
That works, insofar as the C# application compiles, executes, and generates a workbook file.
However, when I open that workbook in Microsoft Excel, Excel decides that there are errors and prompts me whether to repair the workbook. Excel repairs the workbook by just eliminating any <table> that was in it.
In contrast, if the first "column" of the table just spans one cell column, then the created table has no errors and survives to be loaded in Excel. The result is like the following simplified snippet.
Therefore, the root problem is that the table contains merged cells.
Is there any way to accomplish what I want, with one of the table "columns" consisting of merged cells that span multiple cell columns?
IXLRange range = sheet.Range( "A1:D3");
range.Range(startRow, startColumn, startRow, endColumn).Merge();

Create Excel workbook without any sheet

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.

In ClosedXML, how to sort a worksheet by a column?

In C# using asp/MVC the app generates an Excel .xlsx file based on data thats filled in to the specific columns, works great.
But the goal is to provide additional worksheets that use the same columns but sort on specific colums, such as Column "J"
var wb = new XLWorkbook();
var ws = wb.Worksheets.Add("Proj Info");
var ws2 = wb.Worksheets.Add("Sort By Dates");
The worksheet ws has values filled in by variables or formulas, the data is correct, but cannot make it sort on a column
ws.AutoFilter.Column("J"); //no, nothing changes
ws.Column("J").Sort(); -> this shifts all the columns up but does not sort
ws.Column("J").Sort(XLSortOrder.Ascending); ->same, doesnt sort only shifts
Update: ws.Sort(9); worked in sorting, but the problem is that Column 10 has a Formula, and I need to sort on that Column.
ws.Cell("J" + c).FormulaR1C1 = "=C$2-F" + c;
With this? it WILL NOT SORT. The ws.Sort(10); works when the cell contains a final value, but when its got the Formula? Is there any workaround to force the Excel page to sort after its implemented the formula's in each cell?
You are not sorting the table, but the values in column J.
Try this:
ws.Sort("Column10");

Setting Active worksheet using Gembox

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.

Google Spreadsheet - Write to Blank Worksheet

I'm creating a new worksheet in a Google Spreadsheet using the C# library. After creating the worksheet, I want to write to it. I don't have a LocalName, since it's a new sheet with no header. Has anybody done this before?
newRow.Elements.Add(new ListEntry.Custom
{
Value = value,
LocalName = local
});
For new worksheet, I would suggest using the Cell based feed to add the first row:
https://developers.google.com/google-apps/spreadsheets/#changing_contents_of_a_cell
Simply use the indexes A1 to A{N} to add the header and continue using the list based feed as you used to.

Categories