POIはExcelドロップダウンボックスに書き込み[Selectオプション]
レコード部分メソッド、操作POI
POI version
1.まず経路に従ってExcelテンプレートを読み込む
2.workbookを取得した後、どの行列がドロップダウンボックスであるかを設定し、データを設定する
3.workbook読み取り後、元のexcelを保存中にエラーが発生するため、これはpoiの小さなバグでしょう.
どのように解釈することも見られず、修正した後、新しいExcelテンプレートを名前を付けて保存するしかありません.
POI version
<poi.version>3.10-FINAL</poi.version>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
1.まず経路に従ってExcelテンプレートを読み込む
/**
* excel
*
* @param path
* @return
*/
public static Workbook readWorkBook(String path) {
Workbook wb = null;
try {
wb = WorkbookFactory.create(new File(path));
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return wb;
}
2.workbookを取得した後、どの行列がドロップダウンボックスであるかを設定し、データを設定する
public String rewriteExcelTempAtEmp(String instance, String path, String upload) {
// Excel
Workbook workbook = ExcelUtils.readWorkBook(path);
Sheet sheet = workbook.getSheetAt(0);
DataValidationHelper helper = sheet.getDataValidationHelper();
//CellRangeAddressList(firstRow, lastRow, firstCol, lastCol)
CellRangeAddressList addressList = new CellRangeAddressList(3, 500, 17, 17);
//
String[]pos = posStatusName(instance);
DataValidationConstraint constraint = helper.createExplicitListConstraint(pos);
DataValidation dataValidation = helper.createValidation(constraint, addressList);
// Excel
if(dataValidation instanceof XSSFDataValidation) {
dataValidation.setSuppressDropDownArrow(true);
dataValidation.setShowErrorBox(true);
}else {
dataValidation.setSuppressDropDownArrow(false);
}
sheet.addValidationData(dataValidation);
String fileName = StringUtils.substringAfterLast(path, "/");
String newPath = upload+ExcelUtils.getRename(fileName, false);
// POI bug , Excel
ExcelUtils.createExcel(workbook, newPath);
return newPath;
}
3.workbook読み取り後、元のexcelを保存中にエラーが発生するため、これはpoiの小さなバグでしょう.
どのように解釈することも見られず、修正した後、新しいExcelテンプレートを名前を付けて保存するしかありません.
/**
* Excel
* @author lance
* 2014 8 13 4:06:10
* @param workbook
* @param path
*/
public static void createExcel(Workbook workbook, String path) {
FileOutputStream fileOut = null;
try {
fileOut = new FileOutputStream(path);
workbook.write(fileOut);
} catch (Exception e) {
logger.error("Error create excel: ", e.getMessage());
} finally {
try {
if(fileOut != null) {
fileOut.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}