apache-poi
NPOI : XSSF (.xslx)에 대한 데이터 유효성 검사 접근법은 C #
수색…
소개
데이터 유효성 검사를 통해 사용자는 드롭 다운 목록을 만들고 셀의 값을 이러한 항목으로 제한 할 수 있습니다. 제한으로 인해 Excel은 프로그래밍 방식으로 256 자 이상을 바인딩 할 수 없습니다. 256 자 이상을 바인드하려면 설명 된 접근 방식을 따를 수 있습니다.
모든 목록 항목의 총 문자 수가 256보다 작은 경우
모든 설정 파일에서 모든 항목을 읽거나 인라인으로 입력 할 수 있습니다.
구성 파일에 저장되었는지 고려
// Read all list items from config file
string[] countryDV = ConfigurationManager.AppSettings["countryDV"].Split(',').Select(s => s.Trim().ToUpper()).ToArray();
int DVRowLimit = (Int16.MaxValue);
CellRangeAddressList countryDVAddList = new CellRangeAddressList(1, DVRowLimit, 0, 0);
dvConstraint = (XSSFDataValidationConstraint)validationHelper.CreateExplicitListConstraint(countryDV);
// In case of Inline list values
// use this approach: dvConstraint = (XSSFDataValidationConstraint)validationHelper.CreateExplicitListConstraint(new string[] { "USA", "CANADA"});
dataValidation = (XSSFDataValidation)validationHelper.CreateValidation(dvConstraint, countryDVAddList);
dataValidation.ShowErrorBox = true;
dataValidation.SuppressDropDownArrow = true;
dataValidation.ErrorStyle = 0;
dataValidation.CreateErrorBox("InvalidValue", "Select Valid country.");
dataValidation.ShowErrorBox = true;
dataValidation.CreatePromptBox("country Data Validation", "Enter country.");
dataValidation.ShowPromptBox = true;
sheet.AddValidationData(dataValidation);
모든 목록 항목의 총 문자 합계가 256을 초과하면
이전 예제와 같이 모든 항목이 직접 바인드 된 경우 Excel 파일에서 전체 문자 수가 256 미만인 항목 목록에 대한 데이터 유효성 검사를 지원하므로이 경우 접근 방식은 이전 예제와 다릅니다. 그러나 많은 상황에서 목록 항목은 256자를 초과 할 수 있으며이 경우 직접 바인딩이 작동하지 않습니다.
그러나 Excel 파일은 동일한 Excel 파일의 다른 열에서 참조되는 경우 목록 항목의 256 자 이상을 지원합니다. 따라서 한 번 해결 방법으로 데이터베이스 또는 설정 파일에서 모든 값을 읽고 현재보기에서 멀리 떨어진 열로 숨기고 데이터 유효성 검사를 통해이 숨겨진 열을 수식을 통해 읽으면 목록 항목을 만들 수 있습니다. 아래 코드는 설정 파일을 통해 데이터 값을 읽음으로써이 방법을 보여줍니다.
// Read all list items from config file
string[] countryDV = ConfigurationManager.AppSettings["countryDV"].Split(',').Select(s => s.Trim().ToUpper()).ToArray();
// Get the column name where you want to hide the list items, assume distant column is "ZZ"
string countryDVDataCellColumn = ConfigurationManager.AppSettings["countryDVDataCellColumn"].Trim().ToString();
int DVRowLimit = (Int16.MaxValue);
// Creating and Assigning Settings for Data Validation
CreateDropDownUsingCellReference(workbook, countryDV, "CountryConstraint", countryDVDataCellColumn);
CellRangeAddressList countryDVAddList = new CellRangeAddressList(1, DVRowLimit, targetFirstCol, targetLastCol);
dvConstraint = (XSSFDataValidationConstraint)validationHelper.CreateFormulaListConstraint("=CountryConstraint");
dvConstraint.Validate();
dataValidation = (XSSFDataValidation)validationHelper.CreateValidation(dvConstraint, countryDVAddList);
dataValidation.ShowErrorBox = true;
dataValidation.SuppressDropDownArrow = true;
dataValidation.ErrorStyle = 0;
dataValidation.CreateErrorBox("InvalidValue", "Select Valid country.");
dataValidation.ShowErrorBox = true;
dataValidation.CreatePromptBox("country Data Validation", "Enter country.");
dataValidation.ShowPromptBox = true;
sheet.AddValidationData(dataValidation);
private void CreateDropDownUsingCellReference(XSSFWorkbook wb, string[] csvListOfValues, string listName, string headerName)
{
int columnIndex = CellReference.ConvertColStringToIndex(headerName);
try
{
XSSFName namedCell = (XSSFName)wb.CreateName();
namedCell.NameName = listName;
//Creating Cell and Assigning Values from CSVListOfValues;
for (int i = 0; i < csvListOfValues.Length; i++)
{
var namedRow = wb.GetSheetAt(0).CreateRow(i + 1);
namedRow.CreateCell(columnIndex).SetCellValue(csvListOfValues[i]);
}
//Assigning the Reference for sheet 0 With Cell Range, where list items iscopied
String reference = wb.GetSheetAt(0).SheetName + "!$" + headerName + "$2:$" + headerName + "$" + (csvListOfValues.Length + 1).ToString();
namedCell.RefersToFormula = reference;
//Hiding the Column now;
wb.GetSheetAt(0).SetColumnHidden(columnIndex, true);
}
catch (Exception Ex)
{ }
}
Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow