提交POI导出Excel导出功能

This commit is contained in:
zhangqinbin
2021-01-06 09:18:54 +08:00
parent cbaf76a56b
commit dbbe6342a9
7 changed files with 624 additions and 0 deletions

View File

@ -0,0 +1,37 @@
package com.chinaunicom.mall.ebtp.common.poiExport.constant;
import java.io.*;
import java.util.List;
public class AbstractConstant {
public static <T> List<T> deepCopy(List<T> src) throws IOException, ClassNotFoundException {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(src);
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream in = new ObjectInputStream(byteIn);
@SuppressWarnings("unchecked")
List<T> dest = (List<T>) in.readObject();
return dest;
}
public static String[] getExcelResultValue(String resultValue){
String[] value = null;
if(resultValue.indexOf(")")>0){
resultValue = resultValue.substring(1,resultValue.length());
value = resultValue.split("\\)");
}
return value;
}
//初审选项
public static String[] EXCEL_FIRST_RADIO_VALUE = new String[]{"合格", "不合格"};
//详审选项
public static String[] EXCEL_DETAILED_CHECKBOX_VALUE = new String[]{"满足", "不满足"};
}

View File

@ -0,0 +1,250 @@
package com.chinaunicom.mall.ebtp.common.poiExport.constant;
import com.chinaunicom.mall.ebtp.common.poiExport.entity.ExcelTable;
import com.chinaunicom.mall.ebtp.common.poiExport.entity.ExcelTd;
import com.chinaunicom.mall.ebtp.common.poiExport.entity.ExcelTr;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ExportConstant {
public static File generateExcelByTable(ExcelTable table) throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();//工作流
XSSFSheet sheet = wb.createSheet(table.getTableSheetName());
List<ExcelTr> trList = table.getExcelTrList();
Map<String,String> mergeColMap = new HashMap();//列相同屯行
for(int r = 0 ; r < trList.size() ; r++){
XSSFRow row = sheet.createRow(r);//行
ExcelTr tr = trList.get(r);
if(tr.getHeight()!=null) row.setHeight((short) tr.getHeight().intValue());
//行隐藏
if(tr.getIsHidde()){
row.getCTRow().setHidden(tr.getIsHidde());
}
List<ExcelTd> tdList = tr.getExcelTdList();
for(int col = 0 ; col < tdList.size() ; col++){
XSSFCell ce = row.createCell(col);//列
ExcelTd td = tdList.get(col);
if(td.getIsAoutSizeColumn()){
sheet.autoSizeColumn(col,td.getIsAoutSizeColumn());
}
if(td.getWidth()!=null) sheet.setColumnWidth(col,td.getWidth());//列宽
if(td.getIsRowMerge()) {
CellRangeAddress cra = new CellRangeAddress(r, r, col, col+td.getRowMergeNum()-1);//吞行
sheet.addMergedRegion(cra);
}
if(td.getColEqMerge()) {
String value = td.getTdValue();
if (mergeColMap.get(value) != null) {
String merge = mergeColMap.get(value);
merge = merge.substring(0, merge.lastIndexOf("_")) + "_" + r;
mergeColMap.put(value, merge);
} else {
String merge = col + "_" + col + "_" + r + "_" + r;
mergeColMap.put(value, merge);
}
}
//列隐藏
if(td.getIsHidde()){
sheet.setColumnHidden(col,td.getIsHidde());
}
if(td.getIsSelect()){
ExportConstant.setExplicitList(td.getSelect(),sheet, r, r, col, col);
}
ce.setCellValue(td.getTdValue());
ce.setCellStyle(ExportConstant.getStyle(td.getCellStyleKey(),wb));
}
}
//吞列
for(String key : mergeColMap.keySet()){
String merges = mergeColMap.get(key);
String[] merge = merges.split("_");
if(!merge[2].equals(merge[3])) {
//第一行吞行
CellRangeAddress mergCra = new CellRangeAddress(Integer.valueOf(merge[2]),
Integer.valueOf(merge[3]),
Integer.valueOf(merge[0]),
Integer.valueOf(merge[1]));//吞行
sheet.addMergedRegion(mergCra);
}
}
//FileOutputStream out = new FileOutputStream( new File("D:/恒熠/联通/需求/3.0/评审/测试.xlsx"));
File file = new File(table.getFileName()+".xlsx");
FileOutputStream out = new FileOutputStream(file);
wb.write(out);
out.close();
return file;
}
/**
* 生成下拉选
* @param strs 下拉选内容
* @param sheet 生成sheet
* @param firstRow 起始列
* @param lastRow 结束列
* @param irstCol 起始行
* @param lastCol 结束行
*/
public static void setExplicitList(String[] strs,XSSFSheet sheet,int firstRow,int lastRow,int irstCol,int lastCol){
//
DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
DataValidationConstraint createExplicitListConstraint = dataValidationHelper.createExplicitListConstraint(strs);
CellRangeAddressList regions = new CellRangeAddressList(firstRow,lastRow, irstCol, lastCol);
DataValidation createValidation = dataValidationHelper.createValidation(createExplicitListConstraint, regions);
//处理Excel兼容性问题
if (createValidation instanceof XSSFDataValidation) {
createValidation.setSuppressDropDownArrow(true);
createValidation.setShowErrorBox(true);
} else {
createValidation.setSuppressDropDownArrow(false);
}
sheet.addValidationData(createValidation);
}
public static XSSFCellStyle getStyle(String key,XSSFWorkbook wb){
return ExportConstant.getStyle(key, wb,null);
}
public static XSSFCellStyle getStyle(String key,XSSFWorkbook wb,java.awt.Color color){
if(color==null) color = new java.awt.Color(0x0A0A0A);//黑色
if("name".equals(key)){
return ExportConstant.getNameStyle(wb);
}else if("title1".equals(key)){
return ExportConstant.getTitleStyle(wb,"1");
}else if("title2".equals(key)){
return ExportConstant.getTitleStyle(wb,"2");
}else if("content_black".equals(key)){
return ExportConstant.getContentStyle(wb,"宋体",11,color);
}
return ExportConstant.getContentStyle(wb,"宋体",11,color);
}
/**
* 获取表头项目名称一列的样式
* @param wb 文本对象
* @return
*/
public static XSSFCellStyle getNameStyle(XSSFWorkbook wb){
XSSFCellStyle style_name = wb.createCellStyle(); //项目名称样式
style_name.setAlignment(HorizontalAlignment.CENTER);//水平居中
style_name.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
XSSFFont font_name = wb.createFont();//项目名称字体样式
font_name.setBold(true);//加粗
font_name.setFontName("宋体");
font_name.setColor(new XSSFColor(new java.awt.Color(0xFF0000)));
font_name.setFontHeightInPoints((short) 18);
style_name.setFont(font_name);
return style_name;
}
/**
* 获取表头一列的样式 序号 评审类别 评审项 评审因素 评审标准 供应商1、2、3
* @param wb 文本对象
* @param type 标题类型 1、标题 2、供应商
* @return
*/
public static XSSFCellStyle getTitleStyle(XSSFWorkbook wb,String type){
XSSFCellStyle style_title = wb.createCellStyle(); //标题样式1
XSSFFont font_title = wb.createFont();//标题字体样式1
if(type.equals("1")) {
style_title.setFillForegroundColor(new XSSFColor(new java.awt.Color(0x8D8B8B)));//背景色
style_title.setBorderBottom(BorderStyle.THIN);
style_title.setBorderTop(BorderStyle.THIN);
style_title.setBorderLeft(BorderStyle.THIN);
style_title.setBorderRight(BorderStyle.THIN);
style_title.setAlignment(HorizontalAlignment.CENTER);
style_title.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
font_title.setBold(true);//加粗
font_title.setFontName("宋体");
font_title.setColor(new XSSFColor(new java.awt.Color(0x070707)));//字体颜色
font_title.setFontHeightInPoints((short) 12);
style_title.setFont(font_title);
}else if(type.equals("2")){
style_title.setFillForegroundColor(new XSSFColor(new java.awt.Color(0x898888)));//背景色
style_title.setBorderBottom(BorderStyle.THIN);
style_title.setBorderTop(BorderStyle.THIN);
style_title.setBorderLeft(BorderStyle.THIN);
style_title.setBorderRight(BorderStyle.THIN);
style_title.setAlignment(HorizontalAlignment.CENTER);
style_title.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
font_title.setBold(true);//加粗
font_title.setFontName("宋体");
font_title.setColor(new XSSFColor(new java.awt.Color(0xF50303)));
font_title.setFontHeightInPoints((short) 12);
style_title.setFont(font_title);
}
return style_title;
}
/**
* 获取表头一列的样式 序号 评审类别 评审项 评审因素 评审标准 供应商1、2、3
* @param wb 文本对象
* @param fontName 字体
* @param points 字号
* @param color 颜色 1、黑 2、红
* @return
*/
public static XSSFCellStyle getContentStyle(XSSFWorkbook wb,String fontName,int points,java.awt.Color color){
XSSFCellStyle style_content = wb.createCellStyle(); //内容样式1
style_content.setBorderBottom(BorderStyle.THIN);
style_content.setBorderTop(BorderStyle.THIN);
style_content.setBorderLeft(BorderStyle.THIN);
style_content.setBorderRight(BorderStyle.THIN);
style_content.setAlignment(HorizontalAlignment.CENTER);
style_content.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
style_content.setWrapText(true);
XSSFFont font_content = wb.createFont();//内容字体样式
font_content.setFontName(fontName);//宋体
font_content.setFontHeightInPoints((short) points);//11
font_content.setColor(new XSSFColor(color));
/* if(color.equals("1")) {
font_content.setColor(new XSSFColor(new java.awt.Color(0x0A0A0A)));
}else if(color.equals("2")){
font_content.setColor(new XSSFColor(new java.awt.Color(0xF50303)));
}*/
style_content.setFont(font_content);
return style_content;
}
/**
* 设置列宽
* @param sheet
* @param colList
*/
public static void setColumnWidth(XSSFSheet sheet,List<Integer> colList){
for(int i = 0 ; i < colList.size() ; i ++){
sheet.setColumnWidth(i,colList.get(i));//列宽
}
}
}

View File

@ -0,0 +1,43 @@
package com.chinaunicom.mall.ebtp.common.poiExport.constant;
import com.chinaunicom.mall.ebtp.common.poiExport.entity.ExcelTable;
import com.chinaunicom.mall.ebtp.common.poiExport.entity.ExcelTd;
import com.chinaunicom.mall.ebtp.common.poiExport.entity.ExcelTr;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class ExportText {
public static void main(String[] arge){
ExcelTable table = new ExcelTable("测试1");
List<ExcelTd> list = new ArrayList<>();
list.add(new ExcelTd().setTdValue("1111"));
list.add(new ExcelTd().setTdValue("1222"));
list.add(new ExcelTd().setTdValue("1333"));
list.add(new ExcelTd().setTdValue("1444"));
list.add(new ExcelTd().setTdValue("1555"));
table.add(new ExcelTr().setExcelTdList(list));
OutputStream os = null;
try {
String path = "D:/恒熠/联通/需求/3.0/评审/common测试.xlsx";
File file = ExportConstant.generateExcelByTable(table);
InputStream inputStream = new FileInputStream(file);
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 输出的文件流保存到本地文件
File tempFile = new File(path);
os = new FileOutputStream(tempFile);
// 开始读取
while ((len = inputStream.read(bs)) != -1) {
os.write(bs, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,108 @@
package com.chinaunicom.mall.ebtp.common.poiExport.constant;
import com.chinaunicom.mall.ebtp.common.poiExport.entity.ExcelTd;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
/**
*
* 详细审查 表表格模型
* @author fqj
*/
@Data
public class TrModel {
public List<String> rowList;//行内容
//表名称
public List<ExcelTd> tableNameList;
//详审固定表头
public List<ExcelTd> detailedTdList;
//详审固定隐藏ID
public List<Integer> detailedHiddenId;
//初审固定表头
public List<ExcelTd> firstTdList;
public TrModel() {
//固定头三行
this.setRow();
//详审固定表头
this.setDetailedTd();
//初审固定表头
this.setFirstTd();
}
public TrModel(String tableName){
tableNameList = new ArrayList<>();
ExcelTd td0_0 = new ExcelTd();//表名称
td0_0.setTdValue(tableName);
td0_0.setIsRowMerge(true);//是否向右合并
td0_0.setCellStyleKey("name");
tableNameList.add(td0_0);
//固定头三行
this.setRow();
//详审固定表头
this.setDetailedTd();
//初审固定表头
this.setFirstTd();
//详审固定列
/* detailedList = new ArrayList<>();
detailedList.add("序号");
detailedList.add("评审类别");
detailedList.add("评审项编号");
detailedList.add("评分项");
detailedList.add("评分因素");
detailedList.add("评分因素ID");
detailedList.add("分值");
//初审固定列
firstList = new ArrayList<>();
firstList.add("序号");
firstList.add("评审类别");
firstList.add("评审项编号");
firstList.add("评审因素");
firstList.add("评审标准");*/
}
public void setRow(){
//固定行格式
rowList = new ArrayList<>();
rowList.add("表名称");
rowList.add("表头");
rowList.add("供应商隐藏域");
}
public List<ExcelTd> getTdByType(String type){
if("1".equals(type)){
return this.firstTdList;
}else if("2".equals(type)){
return this.detailedTdList;
}
return new ArrayList<>();
}
public void setDetailedTd(){
detailedTdList = new ArrayList<>();
detailedTdList.add(new ExcelTd().setTdValue("序号").setWidth(3700).setCellStyleKey("title1"));
detailedTdList.add(new ExcelTd().setTdValue("评审类别").setWidth(3700).setCellStyleKey("title1"));
detailedTdList.add(new ExcelTd().setTdValue("评审项编号").setWidth(3700).setCellStyleKey("title1").setIsHidde(true));
detailedTdList.add(new ExcelTd().setTdValue("评分项").setWidth(3700).setCellStyleKey("title1"));
detailedTdList.add(new ExcelTd().setTdValue("评审标准").setWidth(3700).setCellStyleKey("title1"));
detailedTdList.add(new ExcelTd().setTdValue("评分因素ID").setWidth(3700).setCellStyleKey("title1").setIsHidde(true));
detailedTdList.add(new ExcelTd().setTdValue("评分因素").setWidth(6700).setCellStyleKey("title1"));
detailedTdList.add(new ExcelTd().setTdValue("分值").setWidth(6700).setCellStyleKey("title1"));
}
public void setFirstTd(){
firstTdList = new ArrayList<>();
firstTdList.add(new ExcelTd().setTdValue("序号").setWidth(3700).setCellStyleKey("title1"));
firstTdList.add(new ExcelTd().setTdValue("评审类别").setWidth(3700).setCellStyleKey("title1"));
firstTdList.add(new ExcelTd().setTdValue("评审项编号").setWidth(3700).setCellStyleKey("title1").setIsHidde(true));
firstTdList.add(new ExcelTd().setTdValue("评审因素").setWidth(6900).setCellStyleKey("title1"));
firstTdList.add(new ExcelTd().setTdValue("评审标准").setWidth(9800).setCellStyleKey("title1"));
}
}

View File

@ -0,0 +1,60 @@
package com.chinaunicom.mall.ebtp.common.poiExport.entity;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
*
*
* @author fqj
*/
@Data
@Accessors(chain = true)
@ApiModel
public class ExcelTable implements Serializable {
private static final long serialVersionUID = 1L;
public ExcelTable(String tableSheetName){
this.setTableSheetName(tableSheetName);
}
/**
* 文件名称
*/
private String fileName;
/**
* sheet名称
*/
private String tableSheetName;
/**
* sheet序号
*/
private int tableSheetSort = 0;
private List<ExcelTr> excelTrList;
public void add(ExcelTr tr){
if(excelTrList!=null){
}else{
excelTrList = new ArrayList<>();
}
excelTrList.add(tr);
}
public void addll(List<ExcelTr> trList){
if(excelTrList!=null){
}else{
excelTrList = new ArrayList<>();
}
excelTrList.addAll(trList);
}
}

View File

@ -0,0 +1,72 @@
package com.chinaunicom.mall.ebtp.common.poiExport.entity;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
/**
*
*
* @author fqj
*/
@Data
@Accessors(chain = true)
@ApiModel
public class ExcelTd implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 名称
*/
private String tdName;
/**
* 内容 显示内容
*/
private String tdValue;
/**
* 行序号
*/
private int rowNum = 0;
/**
* 列序号
*/
private int colNum =0;
/**
* 列是否隐藏
*/
private Boolean isHidde = false;
/**
* 样式
*/
private String cellStyleKey;
/**
* 是否行合并
*/
private Boolean isRowMerge = false;
/**
* 行合并参数 当前位置向右吞行数量
*/
private Integer rowMergeNum;
/**
* 是否列相同合并
*/
private Boolean colEqMerge = false;
/**
* 宽
*/
private Integer width;
private Boolean isSelect = false;
private String[] select;
/**
* 自适应行高
*/
private Boolean isAoutSizeColumn = false;
}

View File

@ -0,0 +1,54 @@
package com.chinaunicom.mall.ebtp.common.poiExport.entity;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
*
*
* @author fqj
*/
@Data
@Accessors(chain = true)
@ApiModel
public class ExcelTr implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 样式
*/
private XSSFCellStyle cellStyle;
/**
* 字体
*/
private XSSFFont font;
/**
* 行是否隐藏
*/
private Boolean isHidde = false;
private List<ExcelTd> excelTdList;
/**
* 高
*/
private Integer height;
public void add(ExcelTd td){
if(excelTdList!=null){
}else{
excelTdList = new ArrayList<>();
}
excelTdList.add(td);
}
}