How to set list to check data in column / columns of excel file using OpenXml in C #?

I need to create a drop down list using a specific list as a source in the column / columns of an excel file using openXml.

I use the following code for this purpose,

     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();

And it throws an error when opening excel. The log is as follows:

      <?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>

Also, how can I assign a comma separated list of values ​​to DataValidations and achieve the desired result, since we do it manually for the excel column?

+4
source share
2 answers

The problem may be that there is already a "DataValidatoins" node. This works for me:

        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);
        }
+7

2 , , .

DataValidation excel #, Excel.

WorkSheet, . "workheet.Append(...)", .

, ! ( , excel, , )

  • SheetDimension
  • SheetViews
  • SheetFormatProperties
  • SheetData​​li >
  • DataValidations
  • PageMargins
  • PageSetup

,

            //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 - , ...

0

Source: https://habr.com/ru/post/1534632/


All Articles