简体   繁体   English

将Excel DropDown从WorkBook复制到新工作簿

[英]Copy Excel DropDown from WorkBook to a new Workbook

Again a Excel question. 再次是一个Excel问题。 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. 我的客户向我发送了一个包含下拉列表(组合框)的Excel文件,我需要复制该组合框或使用内部信息构建新的组合框。 For excel i find that dropdownlist was named as Data Validation (i already got it, but can do nothing with him). 对于excel,我发现dropdownlist被命名为Data Validation(我已经知道了,但是对他无能为力)。

I Have Gembox dll and NativeExcel dll and didnt find any solution. 我有Gembox dll和NativeExcel dll,没有找到任何解决方案。

With GemBox i already get this: 有了GemBox,我已经得到了:

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! 编辑:我认为datavalitation与dropdownlist相关联!

Edit2: Main problem is copy a drop down list from a WorkBook to other! Edit2:主要问题是将下拉列表从WorkBook复制到其他!

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). 首先,我想说的是,在您的情况下,DataValidation与dropdownlist相关联,因为它是一种List类型(请参阅dv.Type属性)。

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. 另外,要将此列表DataValidation复制到另一个Excel文件中,它也将取决于DataValidation源,如shahkalpesh的注释中所述,它们可以是硬编码的,也可以来自单元格区域。 You can check the source of DataValidation by getting a dv.Formula1 object. 您可以通过获取dv.Formula1对象来检查DataValidation的来源。 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: 例如,如果它是硬编码的,那么Formula1将是这些列表项的数组,您可以将该DataValidation添加到另一个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. 但是,如果该DataValidation在Formula1中具有一个单元格范围,则您也必须在新的ExcelFile中复制该单元格范围,或者您可以尝试读取该单元格范围中的单元格值,并用硬编码的项目列表替换Formula1 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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