简体   繁体   中英

C# Excel Drop Down List

I am trying to create a drop list, haven't had any luck finding similar sample code or documentation to follow so I'm flying solo

When I run this sample code I created

static void Main(string[] args)
    {

        using (FileStream filename = new FileStream(@"INSERTYOUROWNPATH.xlsx", FileMode.Create))
        {

            var workbook = new XSSFWorkbook();

            XSSFSheet xsheet = (XSSFSheet)workbook.CreateSheet("Validation");

            CT_DataValidation valid = new CT_DataValidation();
            valid.showDropDown = true;
            valid.allowBlank = true;

            var constraint = new XSSFDataValidationConstraint(new string[] { "0064", "0061" }); //to be used in list
            var range = new CellRangeAddressList(11, 14, 13, 13);    //apply to row 12:14 starting at column N finishing at column N
            var addingconstraint = new XSSFDataValidation(constraint, range, valid);

            addingconstraint.EmptyCellAllowed = true;
            addingconstraint.SuppressDropDownArrow = false;
            xsheet.AddValidationData(addingconstraint);

            workbook.Write(filename);

        }
    }

I get the following errors when I open the spreadsheet:

"We found a problem with some content in "Validations.xlsx" do you want use to try to recover as much as we can? If you trust the source of this workbook, click Yes"

Then I get the following repair message:

"Repaired Part: /xl/worksheets/sheet1.xml part with XML error. Load error. Line 1 column 684"

Could I please get some insight in to where this is failing?

your issue seems to be very similar to this problem about data validation and data constraint.

XSSFDataValidation dataValidation = null;
XSSFDataValidationConstraint dvConstraint = null;
XSSFDataValidationHelper validationHelper = null;

int DVRowLimit = (Int16.MaxValue);
XSSFCellStyle numberCellStyle = (XSSFCellStyle)workbook.CreateCellStyle();
XSSFDataFormat numberDataFormat = (XSSFDataFormat)workbook.CreateDataFormat();
numberCellStyle.SetDataFormat(numberDataFormat.GetFormat("#,###,###"));


CellRangeAddressList cellRangeFieldsType1 = new CellRangeAddressList(1, DVRowLimit, headerCount, headerCount);
dvConstraint = (XSSFDataValidationConstraint)validationHelper.CreateintConstraint(OperatorType.BETWEEN, "0", Int64.MaxValue.ToString());
dataValidation = (XSSFDataValidation)validationHelper.CreateValidation(dvConstraint, cellRangeFieldsType1);
dataValidation.ShowErrorBox = true;
dataValidation.SuppressDropDownArrow = true;
dataValidation.ErrorStyle = 0;
dataValidation.CreateErrorBox("InvalidValue", "Number Should be a integer.");
dataValidation.ShowErrorBox = true;
dataValidation.CreatePromptBox("Number Data Validation", "Enter Number.");
dataValidation.ShowPromptBox = true;
sheet.AddValidationData(dataValidation);
sheet.SetDefaultColumnStyle(column, numberCellStyle);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM