Tot*_*Zam 3 c# validation excel openxml office-interop
在 Excel 中,我能够向一系列单元格添加验证规则,并将接受的输入限制为下拉列表中显示的值列表。这是使用数据验证工具完成的,如下图所示:
我有一些生成 Excel 工作表的 C# 代码,我想将这种相同类型的验证添加到其中一列。
使用Microsoft.Office.Interop.Excel
,我可以将这种下拉验证添加到整个列中:
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;
Run Code Online (Sandbox Code Playgroud)
问题是,我不能保证我的用户安装了 Microsoft Office。因此,我想改为使用DocumentFormat.OpenXML
.
是否可以使用 OpenXML 添加这种相同类型的下拉验证?
我已经看到一些帖子使用DataValidation
,但一直无法弄清楚如何让它工作,以及如果这能解决我的问题。
您需要使用该类,DataValidation
但您也需要该类,这是完全正确的DataValidations
。
AWorkSheet
可以有零个或一个,DataValidations
而 A 又可以包含一个或多个DataValidation
'。
以下代码将创建您正在寻找的数据验证:
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);
}
Run Code Online (Sandbox Code Playgroud)
通过更多的挖掘,我能够弄清楚如何使用以下方法DataValidation
向我的 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);
}
Run Code Online (Sandbox Code Playgroud)