Amadeus Sanchez
Amadeus Sanchez

Reputation: 2565

Set Text Length of Cell using ClosedXML

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

Answers (2)

khaled saleh
khaled saleh

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

Raidri
Raidri

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

Related Questions