标签:基金会 set $2 down pdo param cell reg 图片
目录
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。
实际开发中,用POI来读写excel文档的比较多。所以,这里记录下用POI读取excel文档的一些难点做下记录。
在实际开发中,有时会遇到如图示例,需要按照如图所示进行导出excel文件。这就涉及到了合并单元格和给单元格赋值的问题。
eventSheet.addMergedRegion(new CellRangeAddress(lineNum, fields.size() + lineNum - 1, 1, 1));
按照正常给单元格赋值即可,只是在合并的单元格第一行赋值就行,不用重复赋值;
cell = row.createCell((short) 1);
cell.setCellValue(eventVO.getEventId());
cell.setCellStyle(align_center_style);
在导出excel文件时,需要添加数据验证的下拉框。如图:
/**
* 设置 excel数据验证
*
* @param firstRow
* @param firstCol
* @param endRow
* @param endCol
* @param strList 可供选择的值
* @return
* @since @ 2018年3月14日
*/
XSSFDataValidation getDataValidationList(XSSFSheet sheet, short firstRow, short firstCol, short endRow, short endCol, List<String> strList) {
String[] datas = strList.toArray(new String[0]);
XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet);
XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper
.createExplicitListConstraint(datas);
CellRangeAddressList addressList = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
XSSFDataValidation validation = (XSSFDataValidation) dvHelper.createValidation(dvConstraint, addressList);
return validation;
}
sheet.addValidationData(line2ValidationList);
有时候在excel文件的某列添加数据时,需要保证某列的数据唯一。这就需要使用excel的数据验证-自定义,设置为:=COUNTIF($B$2:B2,B2)=1(说明:从B2开始,B列的数据保证唯一,若是重复会给出提示,不能保存)
public static XSSFDataValidation getDataValidationCustomFormula(XSSFSheet sheet, short firstRow, short firstCol, short endRow, short endCol, String formula) {
XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet);
XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper
.createCustomConstraint(formula);
CellRangeAddressList addressList = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
XSSFDataValidation validation = (XSSFDataValidation) dvHelper.createValidation(dvConstraint, addressList);
validation.createErrorBox("Error", "Repeating Date.");
validation.setShowErrorBox(true);
validation.setEmptyCellAllowed(true);
validation.setSuppressDropDownArrow(true);
validation.setShowPromptBox(true);
validation.setErrorStyle(DataValidation.ErrorStyle.STOP);
return validation;
}
XSSFDataValidation dataValidation = XX.getDataValidationCustomFormula((XSSFSheet) eventSheet, (short) 1, (short) 1, (short) (lineNum + 5), (short) 1, "=COUNTIF($B$2:B2,B2)=1");
if (dataValidation != null) {
eventSheet.addValidationData(dataValidation);
}
标签:基金会 set $2 down pdo param cell reg 图片
原文地址:https://www.cnblogs.com/wsygdb/p/9670234.html