简体   繁体   中英

Copy Excel DropDown from WorkBook to a new Workbook

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM