封装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
封装poi导出篇的更多相关文章
- 封装poi导入篇
前言 有封装的想法好久了,项目中有不少地方需要使用导入功能,导入虽说不能,但是每次都要为了特定类写一个特定方法,很麻烦,我很讨厌一直写这种东西了,正好趁着这次机会就写一个可以解决这种问题的小项目. m ...
- 使用POI导出excel基础篇
最近搞了下POI导出Excel,听说很多次,却是第一次搞. 在pom.xml中引入依赖 <dependency> <groupId>org.apache.poi</gro ...
- 重构:以Java POI 导出EXCEL为例
重构 开头先抛出几个问题吧,这几个问题也是<重构:改善既有代码的设计>这本书第2章的问题. 什么是重构? 为什么要重构? 什么时候要重构? 接下来就从这几个问题出发,通过这几个问题来系统的 ...
- 使用Apache POI导出Excel小结--导出XLS格式文档
使用Apache POI导出Excel小结 关于使用Apache POI导出Excel我大概会分三篇文章去写 使用Apache POI导出Excel小结--导出XLS格式文档 使用Apache POI ...
- poi导出excel
Java使用poi组件导出excel报表,能导出excel报表的还可以使用jxl组件,但jxl想对于poi功能有限,jxl应该不能载excel插入浮动层图片,poi能很好的实现输出excel各种功能, ...
- POI导出EXCEL经典实现
1.Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. .NET的开发人员则 ...
- Java POI 导出EXCEL经典实现 Java导出Excel
转自http://blog.csdn.net/evangel_z/article/details/7332535 在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者 ...
- 重构:以Java POI 导出EXCEL为例2
前言 上一篇博文已经将一些对象抽象成成员变量以及将一些代码块提炼成函数.这一节将会继续重构原有的代码,将一些函数抽象成类,增加成员变量,将传入的参数合成类等等. 上一篇博文地址:http://www. ...
- POI导出时写一份到ftp服务器,一份下载给客户端
导语: 昨天接到项目经理这么一个需求,让我在POI导出Excel的时候写一份到我之前搭建的ftp服务器上.所以就有了这篇博客首先我们来分析下之前的业务逻辑:我们创建并构造了一个workbook,然后构 ...
随机推荐
- margin外边距属性
外边距属性: 设置元素与元素之间的距离(外边距),4个方向(上右下左). margin:长度值|百分比|auto margin-top margin-right margin-bottom margi ...
- PTA——求n以内k个质数和
PTA 7-51 求n以内最大的k个素数以及它们的和 #include<stdio.h> #include<math.h> int isPrime(int n); int ma ...
- Nginx在CentOS7下的安装
一,安装前的准备(Nginx安装之前,需要的工具以及依赖包:wget.gcc.pcre.openssl.zlib ) 1,wget安装 yum -y install wget 2,gcc安装 yum ...
- Appium环境搭建——安卓模拟器(AVD)调试 1-创建模拟器失败点的总结
一.先安装好Android SDK 系统变量—>新建,变量名:ANDROID_HOME,变量值:SDK的安装路径 系统变量—>path—>点击编辑,变量值添加:;%ANDROID_H ...
- 引擎设计跟踪 ShadowMap 细节和分析
之前在工作总汇总了shadowmap的各种问题 [工作积累] shadow map问题汇总 最近有点时间再仔细研究了shadowmap的一些算法.主要修复了LiSPSM(上面链接里后面有更新),实现了 ...
- LG3211 [HNOI2011]XOR和路径
题意 题目描述 给定一个无向连通图,其节点编号为 1 到 N,其边的权值为非负整数.试求出一条从 1 号节点到 N 号节点的路径,使得该路径上经过的边的权值的"XOR 和"最大.该 ...
- 20175202 《Java程序设计》迭代和JDB
一.任务详情 二.设计过程的问题及解决 1.程序编译时一直提示编译出现错误. 原因及解决:本以为声明对象和创建对象一起进行时,可以直接采用如zhubajie = new Xiyoujirenwu(); ...
- linux后台执行命令:&与nohup的用法
& 这种方法很简单,就是在命令之后加个“&”符号就可以了,如下: ./test & 这样一来,test程序就在后台运行了.但是,这样处理还不够,因为这样做虽然程序是在后台运行了 ...
- 详解java定时任务---Timer篇
一.简介 在java的jdk中提供了Timer.TimerTask两个类来做定时任务. Timer是一种定时器工具,用来在一个后台线程计划执行指定任务,而TimerTask一个抽象类,它的子 ...
- 获取邮箱使用情况、以及最后一次logon时间
# 每天收发邮件数 # https://gallery.technet.microsoft.com/scriptcenter/Count-sent-and-recceived-f9c66cf7 # 获 ...