2016-11-11 73 views
2

在Excel中,我可以將驗證規則添加到一系列單元格,並將接受的輸入限制爲顯示在下拉列表中的值列表。這是使用數據驗證工具來完成,如下圖所示:使用OpenXML將下拉驗證添加到整個Excel列中

enter image description here

我有一個生成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; 

的問題是,我不能保證我的用戶已經安裝了Microsoft Office。因此,我想改爲使用DocumentFormat.OpenXML

是否可以使用OpenXML添加相同類型的下拉驗證?

我已經看到一些使用DataValidation的帖子,但還沒有能夠找到如何讓它工作,如果這將解決我的問題。

回答

1

隨着多一點挖,我能弄清楚如何使用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); 
} 
+1

這不適用於引用整個列的常規方法嗎?例如 - 答:A而不是A1:A1048576 – RogerN

+0

@RogerN是的,這是行不通的。謝謝!更新我的解決方案 –

2

你很對,你需要使用DataValidation類,但你也需要DataValidations類。

WorkSheet可以具有零個或一個DataValidations這反過來又可以包含一個或多個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); 
}