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 वर्णों से अधिक लंबा हो सकता है और उस स्थिति में प्रत्यक्ष बंधन काम नहीं करेगा।
हालाँकि, एक्सेल फ़ाइल सूची आइटम के 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)
{ }
}