Excel corrput due to creation of dropdown list with apache-poi library

Royce :

Need description

I need to create a dropdown list in Excel with Apache POI. This list should contains strings.

Code

XSSFSheet sheet = workbook.getSheet(fieldName.getTabName());
XSSFDataValidationHelper helper = new XSSFDataValidationHelper(sheet);
XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) helper.createExplicitListConstraint(myarray);
CellRangeAddressList addressList = new CellRangeAddressList(3, 10, 5, 5);
XSSFDataValidation validation = (XSSFDataValidation) helper.createValidation(dvConstraint, addressList);
validation.setEmptyCellAllowed(false);
validation.setShowErrorBox(true);
sheet.addValidationData(validation);

Error

For some content it is working fine. But if the array myarray contains some values like STUDENT or HOUSE, DEF, I got an error when I want to open the Excel:

We found a problem with some content in 'file.xlsx'. Do you want us to try to recover as much as we can? If you trust the source of the workbook, Click Yes.

Additional information

my array is List<WebElement>. I got this list with:

Select select = new Select(By.xpath("myxpath"));
List<WebElement> opt = select.getOptions();
myarray = new String[opt.size()];
for(int i = 0; i < opt.size(); i++) 
    myarray[i] = opt.get(i).getText();

But I think that this code is not the problem because I'm able to get all options with associated text. It is when I put this content in createExplicitListConstraint that something strange happens.

When I print the content of myarray, all values are displayed correctly in the console.

Axel Richter :

There is a limitation of formula length of explicit data validation lists. Max length is 255. If formulas of explicit data validation lists exceed length of 255, then Excel fails while opening with error We found a problem with some content in .... If you click Yes, Excel tells you that it had repaired /xl/worksheet/sheet*.xml part. After that the data validation list does not work.

Example to reproduce:

import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddressList;

class CreateExcelDataValidationExplicitList {

 public static void main(String[] args) throws Exception {

  int arrSize = 50;
  String[] myarray = new String[arrSize+1];

  for (int i = 0; i < arrSize; i++) {
   myarray[i] = "abcd";
  }

  myarray[arrSize] = "abcde"; // this works
  //myarray[arrSize] = "abcdef"; // this fails

  String arrayFormula = String.join(",", myarray);
System.out.println(arrayFormula.length()); // up to 255 works, bigger lengths fail

  //Workbook workbook = new HSSFWorkbook();
  Workbook workbook = new XSSFWorkbook();

  Sheet sheet = workbook.createSheet();

  DataValidationHelper dvHelper = sheet.getDataValidationHelper();
  CellRangeAddressList addressList = new CellRangeAddressList(3, 10, 5, 5);
  DataValidationConstraint dvConstraint = dvHelper.createExplicitListConstraint(myarray);

  DataValidation validation = dvHelper.createValidation(dvConstraint, addressList);
  validation.setEmptyCellAllowed(false);
  if (workbook instanceof XSSFWorkbook) validation.setShowErrorBox(true);

  sheet.addValidationData(validation);

  FileOutputStream out = null;
  if (workbook instanceof HSSFWorkbook) {
   out = new FileOutputStream("CreateExcelDataValidationExplicitList.xls");
  } else if (workbook instanceof XSSFWorkbook) {
   out = new FileOutputStream("CreateExcelDataValidationExplicitList.xlsx");
  }
  workbook.write(out);
  workbook.close();
  out.close();

 }
}

Only solution would be having the list items in cells of a separate sheet and using createFormulaListConstraint instead of createExplicitListConstraint then. I have shown this here: Apache POI Double Values in ComboBox.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=170986&siteId=1