OpenXML:创建工作簿,然后将验证器放入-vs。打开工作簿,然后将验证器放入

时间:2018-08-08 05:06:21

标签: c# openxml openxml-sdk

以下方法创建一个新的工作簿,并插入两个验证器下拉列表:

public static void ForValidator() {
 using (SpreadsheetDocument myDoc = SpreadsheetDocument.Create("validator output.xlsx", SpreadsheetDocumentType.Workbook)) {
  WorkbookPart workbookpart = myDoc.AddWorkbookPart();
  workbookpart.Workbook = new Workbook();
  WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
  SheetData sheetData_ = new SheetData();
  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"
  });
  DataValidations dataValidations = new DataValidations();
  DataValidation dataValidation = new DataValidation() {
   Type = DataValidationValues.List, AllowBlank = true, SequenceOfReferences = new ListValue<StringValue>() { InnerText = "F2:F3" }
  };
  Formula1 formula = new Formula1();
  formula.Text = "\"Selection 1,Selection 2,Selection 3\"";
  dataValidation.Append(formula);
  dataValidations.Append(dataValidation);
  worksheetPart.Worksheet.AppendChild(dataValidations);
 }
}

是否可以将同一例程修改为只打开现有的“验证器output.xlsx”文件并执行相同的操作?

一个多星期以来,我尝试了所有我想尝试的事情,但没有提出任何建议。谢谢您的帮助。

0 个答案:

没有答案