使用OpenXML将验证器放入现有的,已格式化的工作表的单元格中

时间:2018-08-03 02:18:06

标签: c# openxml openxml-sdk

如何将验证器下拉对象放入已由OpenXML进程打开的格式化工作表的单元格中?

我确实有一个例程,必须先打开一个文档并在添加验证器之前创建一个新工作表,但是到目前为止,我尚无法将其集成到已经打开的工作表中。在这里:

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 = "F11" }
 };
 Formula1 formula = new Formula1();
 formula.Text = "\"Selection 1,Selection 2,Selection 3\"";

 dataValidation.Append(formula);
 dataValidations.Append(dataValidation);

 worksheetPart.Worksheet.AppendChild(dataValidations);
}

我希望它可以像以下一样工作,一种填充剂可以在已经打开的文档上运行,甚至不需要活动工作表的名称:

static Row getRow(uint rowIndex, WorksheetPart worksheetPart) {
 Worksheet worksheet = worksheetPart.Worksheet;
 SheetData sheetData = worksheet.GetFirstChild<SheetData>();
 Row row;
 if (sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).Count() != 0) {
 row = sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).First();
 }
 else {
  row = new Row() { RowIndex = rowIndex };
  sheetData.Append(row);
 }
 return row;
}

static void AddbackgroundFormat(SpreadsheetDocument document, Cell c, string colorCode) {
 Fills fs = AddFill(document.WorkbookPart.WorkbookStylesPart.Stylesheet.Fills, colorCode);
 AddCellFormat(document.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats, document.WorkbookPart.WorkbookStylesPart.Stylesheet.Fills);
 c.StyleIndex = (UInt32)(document.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.Elements<CellFormat>().Count() - 1);
}

static Fills AddFill(Fills fills1, string colorCode) {
 Fill fill1 = new Fill();

 PatternFill patternFill1 = new PatternFill() { PatternType = PatternValues.Solid };
 ForegroundColor foregroundColor1 = new ForegroundColor() { Rgb = colorCode };

 patternFill1.Append(foregroundColor1);

 fill1.Append(patternFill1);
 fills1.Append(fill1);
 return fills1;
}

static void AddCellFormat(CellFormats cf, Fills fs) {
 CellFormat cellFormat2 = new CellFormat() { NumberFormatId = 0, FontId = 0, FillId = (UInt32)(fs.Elements<Fill>().Count() - 1), BorderId = 0, FormatId = 0, ApplyFill = true };
 cf.Append(cellFormat2);
}

Row row1 = getRow(11, worksheetPart);
foreach (Cell c2 in row1.Elements<Cell>()) {
 if (c2.CellReference.Value == "F11") {
  AddbackgroundFormat(document, c2, "ff0000");
 }
}

感谢您的帮助。

更新: 我的最新尝试,没有任何效果。它不会崩溃,但也不会创建验证器:

    DataValidations dataValidations1 = new DataValidations(){ Count = (UInt32Value)1U };
    DataValidation dataValidation1 = new DataValidation(){ Type = DataValidationValues.List, AllowBlank = true, ShowInputMessage = true, ShowErrorMessage = true, SequenceOfReferences = new ListValue<StringValue>() { InnerText = "F11" } };
    Formula1 formula11 = new Formula1();
    formula11.Text = "\"item 1,item 2,item 3\"";
    dataValidation1.Append(formula11);
    dataValidations1.Append(dataValidation1);
    document.WorkbookPart.WorkbookStylesPart.Stylesheet.Append(dataValidations1);

0 个答案:

没有答案