使用OpenXML将下拉validation添加到整个Excel列
在Excel中,我可以将validation规则添加到一系列单元格,并将接受的input限制为显示在下拉列表中的值列表。 这是使用数据validation工具完成的,如下图所示:
我有一些生成excel工作表的C#代码,我想将这种validation添加到其中一列。
使用Microsoft.Office.Interop.Excel
,我可以将这种下拉validation添加到整个列:
string flatList = "FirstChoice,SecondChoice,ThirdChoice"; //select the entire first row as the range Microsoft.Office.Interop.Excel.Range range = worksheet.get_Range("A1").EntireColumn; //remove any previously existing validation range.Validation.Delete(); //add new validation range.Validation.Add( Microsoft.Office.Interop.Excel.XlDVType.xlValidateList, Microsoft.Office.Interop.Excel.XlDVAlertStyle.xlValidAlertInformation, Microsoft.Office.Interop.Excel.XlFormatConditionOperator.xlBetween, flatList, Type.Missing); range.Validation.IgnoreBlank = true; range.Validation.InCellDropdown = true;
问题是,我不能保证我的用户安装了Microsoft Office。 因此,我想改为使用DocumentFormat.OpenXML
。
是否可以使用OpenXML添加相同types的下拉validation?
我已经看到一些使用DataValidation
post,但还没有能够如何得到这个工作,如果这将解决我的问题。
你是非常正确的,你需要使用DataValidation
类,但你也需要DataValidations
类。
一个工作WorkSheet
可以有零个或一个DataValidations
,而DataValidations
又可以包含一个或多个DataValidation
。
以下代码将创build您正在查找的数据validation:
using (SpreadsheetDocument myDoc = SpreadsheetDocument.Create(filename, SpreadsheetDocumentType.Workbook)) { /*** GENERAL SETUP ***/ WorkbookPart workbookpart = myDoc.AddWorkbookPart(); workbookpart.Workbook = new Workbook(); // Add a WorksheetPart to the WorkbookPart. WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>(); SheetData sheetData = new SheetData(); // Add a WorkbookPart to the document. worksheetPart.Worksheet = new Worksheet(sheetData); Sheets sheets = myDoc.WorkbookPart.Workbook.AppendChild(new Sheets()); sheets.AppendChild(new Sheet() { Id = myDoc.WorkbookPart.GetIdOfPart(myDoc.WorkbookPart.WorksheetParts.First()), SheetId = 1, Name = "Sheet1" }); /*** DATA VALIDATION CODE ***/ DataValidations dataValidations = new DataValidations(); DataValidation dataValidation = new DataValidation() { Type = DataValidationValues.List, AllowBlank = true, SequenceOfReferences = new ListValue<StringValue>() { InnerText = "A1:A1048576" } }; Formula1 formula = new Formula1(); formula.Text = "\"FirstChoice,SecondChoice,ThirdChoice\""; dataValidation.Append(formula); dataValidations.Append(dataValidation); worksheetPart.Worksheet.AppendChild(dataValidations); }
有了更多的挖掘,我能弄清楚如何使用DataValidation
添加下拉validation到我的Excel表中的整个列使用DocumentFormat.OpenXml
:
string flatList = "FirstChoice,SecondChoice,ThirdChoice"; DataValidation dataValidation = new DataValidation { Type = DataValidationValues.List, AllowBlank = true, //Use A:A or A1:A1048576 to select the entire column A //1048576 (2^20) is the max row number since Excel 2007. SequenceOfReferences = new ListValue<StringValue>() { InnerText = "A:A" }, //Set the formula to the list of dropdown values. Escape the double quotes. Formula1 = new Formula1("\"" + flatList + "\"") }; //Check if there are any other DataValidations already in the worksheet DataValidations dvs = worksheet.GetFirstChild<DataValidations>(); if (dvs != null) { //If you already have existing validation for column A, you may need to Remove() //or Replace() the current validation to get the new validation to show. //Add the new DataValidation to the list of DataValidations dvs.Count = dvs.Count + 1; dvs.Append(dataValidation); } else { DataValidations newDVs = new DataValidations(); newDVs.Append(dataValidation); newDVs.Count = 1; //Append the validation to the DocumentFormat.OpenXml.SpreadSheet.Worksheet variable worksheet.Append(newDVs); }
- 写入Excel单元格时,从HRESULT:0x800A03EC发生Excelexception
- C#Excel VSTO – 可以移动一个数据透视表?
- Excel / Quickbooks数据到C#
- Microsoft Open SDK 2.0使用c#生成excel文件
- Web API操作会返回FileContentResult,如果保存为.csv,将以乱码打开,而如果为.txt,则为ok。 为什么?
- 是否有一个Excel图表对象到System.Windows.Forms.DataVisualization.Charting转换器?
- 用C#在Excel中查找上次使用的行
- 我想在一个文件中读取多个Excel表而不使用C#中的OLEDB
- 导出DataTable到Excel删除换行符