封装poi导出篇

前言

先写的导入,以为导出会很简单,没想到导出的东西也不少,基于常用的几种样式和校验写了一个简单的导出,包括时间,数字,文字长度,下拉框,提示框校验,基础样式包括字体,字体颜色,背景颜色等功能,可以使用rgb,基本上满足了我的需求。

基础校验类

package cc.vvxtoys.vhelper.excelhelper.exporter;

import java.text.SimpleDateFormat;

import org.apache.poi.hssf.usermodel.DVConstraint;
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationConstraint.ValidationType;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
import org.apache.poi.xssf.usermodel.XSSFDataValidationConstraint;
import org.apache.poi.xssf.usermodel.XSSFDataValidationHelper;
import org.apache.poi.xssf.usermodel.XSSFSheet; /**
* @author vvxtoys
* @date 2018年11月8日下午5:30:02
* @Description: TODO
* @version V1.0
*/
public class BaseValidation implements Validation{ private int version; //版本
private Sheet sheet;
private ValidationConfig config; public BaseValidation(ValidationConfig config){
this.config = config;
}
@Override
public DataValidation createPrompt(){
if(version == 0){
return xlsPrompt();
}else{
return xlsxPrompt();
}
}
private HSSFDataValidation xlsPrompt(){
CellRangeAddressList addressList = new CellRangeAddressList(config.getStartRow(), config.getEndRow(),
config.getStartCol(), config.getEndCol());
DVConstraint dv = DVConstraint.createCustomFormulaConstraint("BB1");
HSSFDataValidation dataValidation = new HSSFDataValidation(addressList, dv);
dataValidation.setSuppressDropDownArrow(false);
dataValidation.createPromptBox("输入提示", config.getPromptText());
dataValidation.setShowPromptBox(true);
return dataValidation;
}
private XSSFDataValidation xlsxPrompt(){
XSSFDataValidationHelper help = new XSSFDataValidationHelper((XSSFSheet)sheet);
CellRangeAddressList addressList = new CellRangeAddressList(config.getStartRow(), config.getEndRow(),
config.getStartCol(), config.getEndCol());
XSSFDataValidationConstraint dv = (XSSFDataValidationConstraint)help.createCustomConstraint("BB1");
XSSFDataValidation validation = (XSSFDataValidation)help.createValidation(dv, addressList);
validation.setSuppressDropDownArrow(false);
validation.createPromptBox("输入提示",config.getPromptText());
validation.setShowPromptBox(true);
return validation;
}
@Override
public DataValidation createDropDownBox(){
if(version == 0){
return xlsDropDownBox();
}else{
return xlsxDropDownBox();
}
} private HSSFDataValidation xlsDropDownBox(){
CellRangeAddressList regions = new CellRangeAddressList(config.getStartRow(), config.getEndRow(),
config.getStartCol(), config.getEndCol());
DVConstraint constraint = DVConstraint.createExplicitListConstraint(config.getTextlist());
HSSFDataValidation datavalidation = new HSSFDataValidation(regions,constraint);
datavalidation.setSuppressDropDownArrow(false);
datavalidation.createPromptBox("输入提示", config.getPromptText());
datavalidation.setShowPromptBox(true);
return datavalidation;
}
private XSSFDataValidation xlsxDropDownBox(){
XSSFDataValidationHelper help = new XSSFDataValidationHelper((XSSFSheet)sheet);
XSSFDataValidationConstraint dvConstraint= (XSSFDataValidationConstraint) help.createExplicitListConstraint(config.getTextlist());
CellRangeAddressList addressList = new CellRangeAddressList(config.getStartRow(), config.getEndRow(),
config.getStartCol(), config.getEndCol());
XSSFDataValidation validation = (XSSFDataValidation) help.createValidation(
dvConstraint, addressList);
validation.setSuppressDropDownArrow(true);
validation.createPromptBox("输入提示", config.getPromptText());
validation.setShowPromptBox(true);
return validation;
}
@Override
public DataValidation createValidateTime(){
if(version == 0){
return xlsValidateTime();
}else{
return xlsxValidateTime();
}
} private HSSFDataValidation xlsValidateTime(){
CellRangeAddressList addressList = new CellRangeAddressList(config.getStartRow(), config.getEndRow(),
config.getStartCol(), config.getEndCol());
DVConstraint dvConstraint = DVConstraint.createDateConstraint(DVConstraint.OperatorType.BETWEEN, config.getStartDate(),
config.getEndDate(), config.getPattern());
HSSFDataValidation dataValidation = new HSSFDataValidation(addressList, dvConstraint);
dataValidation.setSuppressDropDownArrow(false);
dataValidation.createPromptBox("输入提示",config.getPromptText());
dataValidation.createErrorBox("日期格式错误提示", config.getErrorText());
dataValidation.setShowPromptBox(true);
return dataValidation;
}
private XSSFDataValidation xlsxValidateTime(){
XSSFDataValidation validation = null;
try{
SimpleDateFormat sdf = new SimpleDateFormat(config.getPattern());
XSSFDataValidationHelper help = new XSSFDataValidationHelper((XSSFSheet)sheet);
CellRangeAddressList addressList = new CellRangeAddressList(config.getStartRow(), config.getEndRow(),
config.getStartCol(), config.getEndCol());
XSSFDataValidationConstraint dvConstraint=(XSSFDataValidationConstraint)
help.createDateConstraint(DataValidationConstraint.OperatorType.BETWEEN,
""+DateUtil.getExcelDate(sdf.parse(config.getStartDate())),
""+DateUtil.getExcelDate(sdf.parse(config.getEndDate())), ""); validation = (XSSFDataValidation) help.createValidation(
dvConstraint, addressList); validation.createPromptBox("输入提示",config.getPromptText());
validation.createErrorBox("日期格式错误提示",config.getErrorText());
validation.setShowPromptBox(true);
validation.setShowErrorBox(true);
validation.setErrorStyle(0);
sheet.addValidationData(validation);
}catch(Exception e){
e.printStackTrace();
}
return validation;
}
@Override
public DataValidation createTextLength(){
if(version == 0){
return xlsTextLength();
}else{
return xlsxTextLength();
}
}
private HSSFDataValidation xlsTextLength(){
DVConstraint constraint = DVConstraint.createNumericConstraint(
DVConstraint.ValidationType.TEXT_LENGTH,
DVConstraint.OperatorType.BETWEEN, String.valueOf(config.getMinLen()), String.valueOf(config.getMaxLen()));
CellRangeAddressList addressList = new CellRangeAddressList(config.getStartRow(), config.getEndRow(),
config.getStartCol(), config.getEndCol());
HSSFDataValidation dataValidation = new HSSFDataValidation(addressList, constraint);
dataValidation.setSuppressDropDownArrow(false);
dataValidation.createPromptBox("输入提示", config.getPromptText());
// 设置输入错误提示信息
dataValidation.createErrorBox("格式错误",config.getErrorText());
dataValidation.setShowPromptBox(true);
return dataValidation;
}
private XSSFDataValidation xlsxTextLength(){
XSSFDataValidationHelper help = new XSSFDataValidationHelper((XSSFSheet)sheet);
CellRangeAddressList addressList = new CellRangeAddressList(config.getStartRow(), config.getEndRow(),
config.getStartCol(), config.getEndCol());
XSSFDataValidationConstraint dvConstraint=(XSSFDataValidationConstraint)
help.createNumericConstraint(ValidationType.TEXT_LENGTH, DataValidationConstraint.OperatorType.BETWEEN, String.valueOf(config.getMinLen()), String.valueOf(config.getMaxLen()));
XSSFDataValidation validation = (XSSFDataValidation) help.createValidation(
dvConstraint, addressList);
validation.createPromptBox("输入提示", config.getPromptText());
validation.createErrorBox("格式错误提示", config.getErrorText());
validation.setShowPromptBox(true);
validation.setShowErrorBox(true);
return validation;
}
@Override
public DataValidation createValidateNumber(){
if(version == 0){
return xlsValidateNumber();
}else{
return xlsxValidateNumber();
}
} private HSSFDataValidation xlsValidateNumber(){
DVConstraint constraint = DVConstraint.createNumericConstraint(
DVConstraint.ValidationType.DECIMAL,
DVConstraint.OperatorType.BETWEEN, String.valueOf(config.getMin()), String.valueOf(config.getMax()));
CellRangeAddressList addressList = new CellRangeAddressList(config.getStartRow(), config.getEndRow(),
config.getStartCol(), config.getEndCol());
HSSFDataValidation dataValidation = new HSSFDataValidation(addressList, constraint);
dataValidation.createPromptBox("输入提示", config.getPromptText());
dataValidation.createErrorBox("格式错误", config.getErrorText());
dataValidation.setShowPromptBox(true);
dataValidation.setShowErrorBox(true);
return dataValidation;
}
private XSSFDataValidation xlsxValidateNumber(){
XSSFDataValidationHelper help = new XSSFDataValidationHelper((XSSFSheet)sheet);
CellRangeAddressList addressList = new CellRangeAddressList(config.getStartRow(), config.getEndRow(),
config.getStartCol(), config.getEndCol());
XSSFDataValidationConstraint dvConstraint=(XSSFDataValidationConstraint)
help.createNumericConstraint(ValidationType.DECIMAL, DataValidationConstraint.OperatorType.BETWEEN, String.valueOf(config.getMin()), String.valueOf(config.getMax()));
XSSFDataValidation validation = (XSSFDataValidation) help.createValidation(
dvConstraint, addressList);
validation.createPromptBox("输入提示", config.getPromptText());
validation.createErrorBox("格式错误", config.getErrorText());
validation.setShowPromptBox(true);
validation.setShowErrorBox(true);
return validation;
} public void generatorValidation(){
if(version == 0 && config.getEndRow() == 0){
config.setEndRow(65535);
}else if(version == 1 && config.getEndRow() == 0){
config.setEndRow(1048575);
}
if(config.getCustomize()){
addValidation(config.getDataValidation());
}else{
switch (config.getEv()) {
case PROMPT:
addValidation(createPrompt());
break;
case DROPDOWN:
addValidation(createDropDownBox());
break;
case DATE:
addValidation(createValidateTime());
break;
case LENGTH:
addValidation(createTextLength());
break;
case NUMBER:
addValidation(createValidateNumber());
break;
default:
break;
}
}
}
public void setSheet(Sheet sheet){
if(sheet instanceof HSSFSheet){
this.version = 0;
this.sheet = (HSSFSheet)sheet;
}else if(sheet instanceof XSSFSheet){
this.version = 1;
this.sheet = (XSSFSheet)sheet;
}
} public void addValidation(DataValidation dataValidation){
sheet.addValidationData(dataValidation);
}
}

基础样式类

package cc.vvxtoys.vhelper.excelhelper.exporter;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.DefaultIndexedColorMap;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFDataFormat;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class BaseStyle { private int version;
private Workbook workbook;
private StyleConfig sconfig; public BaseStyle(Workbook workbook,StyleConfig config) {
if(workbook instanceof HSSFWorkbook){
this.version = 0;
this.workbook = workbook;
this.sconfig = config;
}else if(workbook instanceof XSSFWorkbook){
version = 1;
this.workbook = workbook;
this.sconfig = config;
}
} /**
* @author vvxtoys
* @date 2018年11月13日下午3:38:58
* @Description: 调用优先级 rgb index
* @return
*/
public CellStyle generatorStyle(){
if(sconfig.isOuter()){
return sconfig.getCellStyle();
}
if(version == 0){
HSSFCellStyle style = ((HSSFWorkbook)workbook).createCellStyle();
Object bgColor = sconfig.getBackground();
if(bgColor instanceof byte[]){
byte[] bs = (byte[])bgColor;
style.setFillForegroundColor(HSSFColorPredefined.WHITE.getIndex());
HSSFPalette palette = ((HSSFWorkbook) workbook).getCustomPalette();
palette.setColorAtIndex(HSSFColorPredefined.WHITE.getIndex(), bs[0], bs[1], bs[2]);
}else{
short bgIndex = (short)bgColor;
if(bgIndex == -1){
style.setFillForegroundColor(HSSFColorPredefined.GREY_40_PERCENT.getIndex());
}else{
style.setFillForegroundColor(bgIndex);
}
}
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setBorderBottom(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setAlignment(HorizontalAlignment.CENTER);
HSSFDataFormat format = ((HSSFWorkbook)workbook).createDataFormat();
style.setDataFormat(format.getFormat("@"));
style.setWrapText(true); HSSFFont font = ((HSSFWorkbook)workbook).createFont();
Object fColor = sconfig.getFontColor();
if(fColor instanceof byte[]){
byte[] frgb = (byte[])fColor;
font.setColor(HSSFColorPredefined.BLACK.getIndex());
style.setFillForegroundColor(HSSFColorPredefined.BLACK.getIndex());
HSSFPalette palette1 = ((HSSFWorkbook) workbook).getCustomPalette();
palette1.setColorAtIndex(HSSFColorPredefined.BLACK.getIndex(), frgb[0], frgb[1], frgb[2]);
}else{
short fIndex = (short)fColor;
if(fIndex == -1){
font.setColor(HSSFColorPredefined.BLACK.getIndex());
}else{
font.setColor(fIndex);
}
}
font.setFontHeightInPoints(sconfig.getHeight());
font.setFontName(sconfig.getFontName());
font.setBold(sconfig.getBlob()); style.setFont(font); return style;
}else{
XSSFCellStyle style = ((XSSFWorkbook)workbook).createCellStyle();
Object bgColor = sconfig.getBackground();
if(bgColor instanceof byte[]){
byte[] rgb = (byte[])bgColor;
XSSFColor color = new XSSFColor(rgb, new DefaultIndexedColorMap());
style.setFillForegroundColor(color);
}else{
short bgIndex = (short)bgColor;
if(bgIndex == -1){
style.setFillForegroundColor(HSSFColorPredefined.GREY_40_PERCENT.getIndex());
}else{
style.setFillForegroundColor(bgIndex);
}
} style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setBorderBottom(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setAlignment(HorizontalAlignment.CENTER);
style.setWrapText(true);
XSSFDataFormat format = ((XSSFWorkbook)workbook).createDataFormat();
style.setDataFormat(format.getFormat("@")); XSSFFont font = ((XSSFWorkbook)workbook).createFont();
Object fColor = sconfig.getFontColor();
if(fColor instanceof byte[]){
byte[] frgb = (byte[])fColor;
XSSFColor color1 = new XSSFColor(frgb,new DefaultIndexedColorMap());
style.setFillForegroundColor(color1);
}else{
short fIndex = (short)fColor;
if(fIndex == -1){
font.setColor(HSSFColorPredefined.BLACK.getIndex());
}else{
font.setColor(fIndex);
}
}
font.setFontHeightInPoints(sconfig.getHeight());
font.setFontName(sconfig.getFontName());
font.setBold(sconfig.getBlob());
style.setFont(font); return style;
}
} public CellStyle getDefaultStyle(){
if(version == 0){
HSSFCellStyle style = ((HSSFWorkbook)workbook).createCellStyle();
style.setBorderBottom(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setAlignment(HorizontalAlignment.CENTER);
HSSFDataFormat format = ((HSSFWorkbook)workbook).createDataFormat();
style.setDataFormat(format.getFormat("@"));
style.setWrapText(true); HSSFFont font = ((HSSFWorkbook)workbook).createFont();
font.setFontHeightInPoints(sconfig.getHeight());
font.setFontName(sconfig.getFontName());
font.setBold(sconfig.getBlob()); style.setFont(font); return style;
}else{
XSSFCellStyle style = ((XSSFWorkbook)workbook).createCellStyle();
style.setBorderBottom(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setAlignment(HorizontalAlignment.CENTER);
style.setWrapText(true);
XSSFDataFormat format = ((XSSFWorkbook)workbook).createDataFormat();
style.setDataFormat(format.getFormat("@")); XSSFFont font = ((XSSFWorkbook)workbook).createFont();
font.setFontHeightInPoints(sconfig.getHeight());
font.setFontName(sconfig.getFontName());
font.setBold(sconfig.getBlob());
style.setFont(font); return style;
}
}
}

结语

导出代码量比较大,贴出来两个比较核心的类,完整项目地址为:https://gitee.com/vvxtoys/vhelper

上一篇:mysql 8.0 改变数据目录和日志目录(二)


下一篇:重新开始学习javase_控制程序流程