Reputation: 2565
I was wondering how do you set the text length of a cell in an Excel worksheet using ClosedXML. I have the suspicion that XLTextLengthCriteria may be helpful. I read the docs of ClosedXML but I did not find a concrete answer. Any help will be appreaciated.
Upvotes: 1
Views: 2273
Reputation: 518
check this solution
notice that ErrorStyle and ErrorTitle are optional
worksheet.Range("A1", "A1").SetDataValidation().TextLength.EqualOrGreaterThan(5);
worksheet.Range("A1", "A1").SetDataValidation().ErrorStyle = ClosedXML.Excel.XLErrorStyle.Stop;
//XLErrorStyle.Stop will prevent adding data,
//XLErrorStyle.Information will show hint,
//XLErrorStyle.Warning will let user choose to continue or not
worksheet.Range("A1", "A1").SetDataValidation().ErrorTitle = "Text Length should be greater than 4 charachters";
Upvotes: 0
Reputation: 17550
For validation you can set the text length like this:
worksheet.Cell(1,1).SetDataValidation().TextLength.EqualOrLessThan(10);
For a complete column use this:
worksheet.Column(1).AsRange().SetDataValidation().TextLength.EqualOrLessThan(10);
Upvotes: 1