如何在c#中使用OpenXml为excel文件的列/列设置数据验证列表?

时间:2014-04-02 10:17:08

标签: list c#-4.0 openxml validation

我需要使用openXml在excel文件的列/列中使用特定列表作为源创建一个下拉列表。

我出于此目的使用以下代码,

     SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open("C:\\Users\\Harun.TV\\Desktop\\OpenXml\\1.xlsx",true);

        WorkbookPart workbookpart = spreadSheetDocument.WorkbookPart;
        Workbook workbook=workbookpart.Workbook;


        WorksheetPart worksheetPart=workbookpart.WorksheetParts.First();            


        DataValidations dataValidations1 = new DataValidations();
        DataValidation dataValidation2 = new DataValidation() { Formula1 = new Formula1("'mySheet'!$A$1:$A$4"), Type = DataValidationValues.List, AllowBlank = true, ShowInputMessage = true, ShowErrorMessage = true, SequenceOfReferences = new ListValue<StringValue>() { InnerText = "A4:B4" } };
        Formula1 formula12 = new Formula1();
        formula12.Text = "$A$1:$A$3";
        dataValidations1.Append(dataValidation2);
        worksheetPart.Worksheet.Append(dataValidations1);

         workbookpart.Workbook.Save();


        spreadSheetDocument.Close();

打开excel时抛出错误。日志如下,

      <?xml version="1.0" encoding="UTF-8" standalone="true"?>
     -<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">     <logFileName>error055840_01.xml</logFileName>
      <summary>Errors were detected in file  'C:\Users\Harun.TV\Desktop\OpenXml\6.xlsx'</summary>
      -<removedParts summary="Following is a  list of removed parts:">     
       <removedPart>Replaced Part: /xl/worksheets/sheet3.xml part with XML error. Load error. Line 1, column 467.</removedPart></removedParts></recoveryLog>

另外,我如何将一个逗号分隔值列表分配给DataValidations并获得所需的结果,因为我们手动为excel列操作?

2 个答案:

答案 0 :(得分:7)

问题可能是已经存在&#34; DataValidatoins&#34;节点。 这对我有用:

        DataValidation dataValidation = new DataValidation
        {
            Type = DataValidationValues.List,
            AllowBlank = true,
            SequenceOfReferences = new ListValue<StringValue>() { InnerText = "B1" },
            Formula1 = new Formula1("'SheetName'!$A$1:$A$3")
        };

        DataValidations dvs = worksheet.GetFirstChild<DataValidations>(); //worksheet type => Worksheet
        if (dvs != null)
        {
            dvs.Count = dvs.Count + 1;
            dvs.Append(dataValidation);
        }
        else
        {
            DataValidations newDVs = new DataValidations();
            newDVs.Append(dataValidation);
            newDVs.Count = 1;
            worksheet.Append(newDVs);
        }

答案 1 :(得分:1)

我知道2年前的这个发布日期,但是我遇到了完全相同的问题,我找到了解决方案。

当我尝试使用C#在我的excel文件中创建DataValidation时,在Excel中打开它时遇到了同样的问题。

如果检查WorkSheet对象,您会在里面找到几个Child。 当你做&#34; workheet.Append(...)&#34;你只需要添加另一个孩子。

就我而言,我只是想知道孩子的顺序很重要! (事实上​​,我并没有创建所有的excel文件,我使用现有的文件修改了一些东西)

这里,孩子的顺序

  1. SheetDimension
  2. SheetViews
  3. SheetFormatProperties
  4. SheetData
  5. DataValidations
  6. PageMargins
  7. PAGESETUP
  8. 在那里,我如何重新排序工作表孩子

                //workseet property is a Worksheet object
                var pageMargins = worksheet.GetFirstChild<PageMargins>();
                pageMargins.Remove();
    
                var pageSetup = worksheet.GetFirstChild<PageSetup>();
                pageSetup.Remove();
    
                DataValidations newDataValidations = new DataValidations();
    
                DataValidation dataValidation = new DataValidation()
                {
                    Type = DataValidationValues.List,
                    AllowBlank = true,
                    SequenceOfReferences = new ListValue<StringValue>() { InnerText = "B2:B4" },
                    Formula1 = new Formula1("\"test,great test\""),
                    ShowErrorMessage = true,
                    ShowInputMessage = true,
                };
    
                newDataValidations.Append(dataValidation);
                newDataValidations.Count = 1;
                worksheet.Append(newDataValidations);
                worksheet.Append(pageMargins);
                worksheet.Append(pageSetup);
    

    所以要小心,如果将DataValidation添加到你做的最后一件事......