1
0
mirror of https://github.com/chatopera/cosin.git synced 2025-08-01 16:38:02 +08:00

Fix UKExcelUtil

This commit is contained in:
dengchao@xgtl 2020-04-17 14:45:03 +08:00
parent b4865d8869
commit 55c1d2b159
2 changed files with 846 additions and 865 deletions

View File

@ -19,9 +19,6 @@ package com.chatopera.cc.util.bi;
import com.chatopera.cc.util.bi.model.FirstTitle;
import com.chatopera.cc.util.bi.model.Level;
import com.chatopera.cc.util.bi.model.ValueData;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
@ -39,34 +36,26 @@ import java.util.regex.Pattern;
/**
* 导出Excel
* @author Jason Chen
*
* @author Jason Chen
*/
public class UKExcelUtil {
private final static int ROW_LIMIT = 100;
int page = 1;
private ReportData reportData;
private SXSSFWorkbook wb;
private Sheet sheet;
private final static int ROW_LIMIT = 100 ;
private int rowNum = 0;
private int rowTitleNum = 0;
private int cellNumber = 0; //总列数
private String headTitle = "报表";
private String startTime = "";
private String endTime = "";
private CellStyle firstStyle = null;
private CellStyle style = null;
private CellStyle datastyle = null;
int page = 1 ;
private OutputStream out;
public UKExcelUtil() {
super();
wb = new SXSSFWorkbook(ROW_LIMIT);
@ -74,6 +63,7 @@ public class UKExcelUtil{
style = createContentCellStyle();
datastyle = createContentCellStyle();
}
public UKExcelUtil(ReportData reportData, OutputStream out, String title) {
this.reportData = reportData;
this.out = out;
@ -119,6 +109,7 @@ public class UKExcelUtil{
sheet.setColumnWidth(i, 5000);
}
}
private void getnewCol() {
List<List<Level>> title = reportData.getCol().getTitle();
if (title != null) {
@ -144,7 +135,7 @@ public class UKExcelUtil{
row.setHeight((short) 1100);
// 定义单元格为字符串类型
cell.setCellType(HSSFCell.ENCODING_UTF_16);
cell.setCellType(CellType.STRING);
cell.setCellValue(new XSSFRichTextString(this.headTitle));
// 指定合并区域
@ -154,20 +145,20 @@ public class UKExcelUtil{
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐
cellStyle.setAlignment(HorizontalAlignment.CENTER); // 指定单元格居中对齐
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 指定单元格垂直居中对齐
cellStyle.setWrapText(true);// 指定单元格自动换行
// 设置单元格字体
Font font = wb.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setBold(true);
font.setFontName("宋体");
font.setFontHeight((short) 400);
cellStyle.setFont(font);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderBottom(BorderStyle.THIN);
cell.setCellStyle(cellStyle);
for (int i = 1; i < this.cellNumber; i++) {
Cell cell3 = row.createCell(i);
@ -183,50 +174,47 @@ public class UKExcelUtil{
private void createSubHead() {
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐
cellStyle.setAlignment(HorizontalAlignment.CENTER); // 指定单元格居中对齐
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 指定单元格垂直居中对齐
cellStyle.setWrapText(true);// 指定单元格自动换行
// 设置单元格字体
Font font = wb.createFont();
//font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//font.setBold(true);
font.setFontName("宋体");
font.setFontHeight((short) 180);
cellStyle.setFont(font);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderBottom(BorderStyle.THIN);
CellStyle leftStyle = wb.createCellStyle();
leftStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT); // 指定单元格居中对齐
leftStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐
leftStyle.setAlignment(HorizontalAlignment.LEFT); // 指定单元格居中对齐
leftStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 指定单元格垂直居中对齐
leftStyle.setWrapText(true);// 指定单元格自动换行
leftStyle.setFont(font);
leftStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
leftStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
leftStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
leftStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
leftStyle.setBorderTop(BorderStyle.THIN);
leftStyle.setBorderLeft(BorderStyle.THIN);
leftStyle.setBorderRight(BorderStyle.THIN);
leftStyle.setBorderBottom(BorderStyle.THIN);
Row row1 = sheet.createRow(rowNum);
row1.setHeight((short) 440);
StringBuffer strb = new StringBuffer();
// if(false){ //增加 过滤器
//
// }else{
//
// }
strb.append("报表生成日期:").append(getNowDate()) ;
Cell cell2 = row1.createCell(0);
cell2.setCellType(HSSFCell.ENCODING_UTF_16);
cell2.setCellValue(new XSSFRichTextString(strb.toString()));
cell2.setCellType(CellType.STRING);
cell2.setCellValue(new XSSFRichTextString("报表生成日期:" + getNowDate()));
cell2.setCellStyle(leftStyle);
// 指定合并区域
if (rowNum > 1) {
@ -304,10 +292,10 @@ public class UKExcelUtil{
*/
private synchronized void createContent() {
StringBuffer sbRol = new StringBuffer();
StringBuffer sbCol = new StringBuffer();
StringBuffer sbDataCol = new StringBuffer();
StringBuffer sbDataRol = new StringBuffer();
StringBuilder sbRol = new StringBuilder();
StringBuilder sbCol = new StringBuilder();
StringBuilder sbDataCol = new StringBuilder();
StringBuilder sbDataRol = new StringBuilder();
if (reportData.getRow() != null) {
List<List<Level>> rowlst = reportData.getRow().getTitle();
List<List<ValueData>> dataList = reportData.getData();
@ -330,12 +318,11 @@ public class UKExcelUtil{
cell2.setCellStyle(firstStyle);
//weidu
for(int c = 0;c<rowlst.size();c++){
for (List<Level> levels : rowlst) {
int rowspan = 0;
Cell cell3 = row2.createCell(cellNum);
List<Level> leveList = rowlst.get(c) ;
Level currentLevel = null;
for(Level level : leveList){
for (Level level : levels) {
if (rowspan == r) {
currentLevel = level;
break;
@ -346,9 +333,9 @@ public class UKExcelUtil{
if (currentLevel != null) {
String value = String.valueOf(currentLevel.getName());
if (!"".equals(value)) {
if(value.indexOf("%") >-1){
if (value.contains("%")) {
value = value.replace("%", "");
if(checkIsNumber(String.valueOf(value)) ){
if (checkIsNumber(value)) {
cell3.setCellValue(Double.parseDouble(value) / 100);
datastyle.setDataFormat(wb.createDataFormat().getFormat("0.00%"));
cell3.setCellStyle(datastyle);
@ -359,11 +346,11 @@ public class UKExcelUtil{
}
} else {
if(checkIsNumber(String.valueOf(value)) ){
cell3.setCellValue(Double.parseDouble(String.valueOf(value)));
if (checkIsNumber(value)) {
cell3.setCellValue(Double.parseDouble(value));
cell3.setCellStyle(style);
} else {
cell3.setCellValue(String.valueOf(value));
cell3.setCellValue(value);
cell3.setCellStyle(style);
}
}
@ -372,9 +359,9 @@ public class UKExcelUtil{
cell3.setCellValue("");
cell3.setCellStyle(style);
}
if(currentLevel!=null && currentLevel.getRowspan()>1){
if (currentLevel.getRowspan() > 1) {
sbRol.append(rowNum).append(",").append(cellNum).append(",").append(currentLevel.getRowspan()).append(";");//开始行开始列合并行数
}else if(currentLevel!=null && currentLevel.getColspan()>1){
} else if (currentLevel.getColspan() > 1) {
sbCol.append(rowNum).append(",").append(cellNum).append(",").append(currentLevel.getColspan()).append(";");//开始行开始列合并列数
}
} else {
@ -393,9 +380,9 @@ public class UKExcelUtil{
sbDataCol.append(rowNum).append(",").append(cellNum).append(",").append(dataList.get(r).get(j).getColspan()).append(";");//开始行开始列合并列数
}
if (!"".equals(value)) {
if(value.indexOf("%") >-1){
if (value.contains("%")) {
value = value.replace("%", "");
if(checkIsNumber(String.valueOf(value)) ){
if (checkIsNumber(value)) {
cell3.setCellValue(Double.parseDouble(value) / 100);
datastyle.setDataFormat(wb.createDataFormat().getFormat("0.00%"));
cell3.setCellStyle(datastyle);
@ -409,7 +396,7 @@ public class UKExcelUtil{
// cell3.setCellValue(Double.parseDouble(String.valueOf(value)));
// cell3.setCellStyle(style);
// }else{
cell3.setCellValue(String.valueOf(value));
cell3.setCellValue(value);
cell3.setCellStyle(style);
// }
}
@ -453,19 +440,18 @@ public class UKExcelUtil{
}
}
/**
* 首列样式
* @return
*/
@SuppressWarnings("deprecation")
private CellStyle createFirstCellStyle() {
CellStyle cellStyle = baseCellStyle();
Font font = wb.createFont();
font.setFontHeight((short) 180);
cellStyle.setFont(font);
cellStyle.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellStyle.setFillForegroundColor(HSSFColor.HSSFColorPredefined.LIGHT_GREEN.getIndex());
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
return cellStyle;
@ -473,17 +459,15 @@ public class UKExcelUtil{
/**
* 标题样式
* @return
*/
@SuppressWarnings("deprecation")
private CellStyle createTitleCellStyle() {
CellStyle cellStyle = baseCellStyle();
Font font = wb.createFont();
font.setFontHeight((short) 180);
cellStyle.setFont(font);
cellStyle.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellStyle.setFillForegroundColor(HSSFColor.HSSFColorPredefined.LIGHT_GREEN.getIndex());
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
return cellStyle;
@ -492,33 +476,30 @@ public class UKExcelUtil{
/**
* 内容样式
* @return
*/
@SuppressWarnings("deprecation")
private CellStyle createContentCellStyle() {
CellStyle cellStyle = baseCellStyle();
Font font = wb.createFont();
font.setFontHeight((short) 200);
font.setBoldweight((short)0);
font.setBold(false);
cellStyle.setFont(font);
return cellStyle;
}
@SuppressWarnings("deprecation")
private CellStyle baseCellStyle() {
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setWrapText(true);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderBottom(BorderStyle.THIN);
Font font = wb.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setBold(true);
font.setFontName("宋体");
font.setFontHeight((short) 200);
cellStyle.setFont(font);
@ -533,9 +514,6 @@ public class UKExcelUtil{
/**
* 输入EXCEL文件
*
* @param fileName 文件名
* @throws IOException
*/
private void outputExcel() throws IOException {
if (this.out != null) {
@ -549,32 +527,31 @@ public class UKExcelUtil{
return m.matches();
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
public String getHeadTitle() {
// TODO Auto-generated method stub
return this.headTitle;
}
public void setHeadTitle(String title) {
this.headTitle = title;
}
public String getStartTime() {
// TODO Auto-generated method stub
return this.startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getEndTime() {
// TODO Auto-generated method stub
return this.endTime;
}
public void setHeadTitle(String title) {
this.headTitle = title ;
public void setEndTime(String endTime) {
this.endTime = endTime;
}
public void createSheet(String sheetName) throws Exception {
@ -582,18 +559,15 @@ public class UKExcelUtil{
this.sheet = wb.createSheet(sheetName);
}
public void setReportData(ReportData reportData) throws Exception {
public int getRowNum() throws Exception {
// TODO Auto-generated method stub
this.reportData = reportData;
return rowNum;
}
public void setRowNum(int rowNum) {
this.rowNum = rowNum;
}
public int getRowNum() throws Exception {
// TODO Auto-generated method stub
return rowNum;
}
public void close() {
try {
wb.write(out);
@ -602,19 +576,23 @@ public class UKExcelUtil{
e.printStackTrace();
}
}
public void setPage(int page) throws Exception {
// TODO Auto-generated method stub
this.page=page;
}
public void setOut(OutputStream out) throws Exception {
// TODO Auto-generated method stub
this.out = out;
}
public int getPage() throws Exception {
public int getPage() {
// TODO Auto-generated method stub
return this.page;
}
public void writeHead(ReportData reportData) throws Exception {
public void setPage(int page) {
// TODO Auto-generated method stub
this.page = page;
}
public void writeHead(ReportData reportData) {
if (this.reportData == null) {
this.reportData = reportData;
}
@ -623,15 +601,22 @@ public class UKExcelUtil{
this.createSubHead();
this.createTitle();
}
public synchronized void writeRow(ReportData reportData) throws Exception {
public synchronized void writeRow(ReportData reportData) {
if (reportData != null) {
this.reportData = reportData;
this.createContent();
}
}
public ReportData getReportData() {
// TODO Auto-generated method stub
return this.reportData;
}
public void setReportData(ReportData reportData) {
// TODO Auto-generated method stub
this.reportData = reportData;
}
}

View File

@ -23,8 +23,6 @@ import com.chatopera.cc.model.SysDic;
import com.chatopera.cc.model.TableProperties;
import com.chatopera.cc.persistence.interfaces.DataExchangeInterface;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
@ -36,17 +34,14 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@SuppressWarnings("deprecation")
public class ExcelExporterProcess {
private HSSFWorkbook wb;
private Sheet sheet;
private CellStyle firstStyle = null ;
private final HSSFWorkbook wb;
private final Sheet sheet;
private final CellStyle firstStyle;
private final List<Map<String, Object>> values;
private final MetadataTable table;
private final OutputStream output;
private int rowNum;
private List<Map<String ,Object>> values ;
private MetadataTable table ;
private OutputStream output ;
private Row titleRow;
public ExcelExporterProcess(List<Map<String, Object>> values, MetadataTable table, OutputStream output) {
@ -58,6 +53,7 @@ public class ExcelExporterProcess {
firstStyle = createFirstCellStyle();
createHead();
}
public void process() throws IOException {
createContent();
if (table != null) {
@ -86,8 +82,8 @@ public class ExcelExporterProcess {
private CellStyle createContentStyle() {
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐
cellStyle.setAlignment(HorizontalAlignment.CENTER); // 指定单元格居中对齐
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 指定单元格垂直居中对齐
cellStyle.setWrapText(false);// 指定单元格自动换行
// 设置单元格字体
@ -95,16 +91,15 @@ public class ExcelExporterProcess {
font.setFontName("微软雅黑");
font.setFontHeight((short) 200);
cellStyle.setFont(font);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderBottom(BorderStyle.THIN);
return cellStyle;
}
/**
* 首列样式
* @return
*/
private CellStyle createFirstCellStyle() {
CellStyle cellStyle = baseCellStyle();
@ -115,8 +110,8 @@ public class ExcelExporterProcess {
cellStyle.setWrapText(false);
cellStyle.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellStyle.setFillForegroundColor(HSSFColor.HSSFColorPredefined.LIGHT_GREEN.getIndex());
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
return cellStyle;
@ -128,7 +123,7 @@ public class ExcelExporterProcess {
if (table != null && table.getTableproperty() != null) {
for (Map<String, Object> value : values) {
Row row2 = sheet.createRow(rowNum);
List<ExportData> tempExportDatas = new ArrayList<ExportData>();
List<ExportData> tempExportDatas = new ArrayList<>();
int cols = 0;
for (TableProperties tp : table.getTableproperty()) {
Cell cell2 = row2.createCell(cols++);
@ -155,7 +150,7 @@ public class ExcelExporterProcess {
List<SysDic> dicItemList = Dict.getInstance().getSysDic(tp.getSeldatacode());
if (dicItemList != null && dicItemList.size() > 0) {
for (SysDic dicItem : dicItemList) {
String s = "";
String s;
Object obj = value.get(tp.getFieldname());
if (obj instanceof Boolean) {
s = (Boolean) obj ? "1" : "0";
@ -163,7 +158,8 @@ public class ExcelExporterProcess {
s = String.valueOf(value.get(tp.getFieldname()));
}
if (dicItem.getCode().equals(s)) {
cell2.setCellValue(new HSSFRichTextString(dicItem.getName())); break ;
cell2.setCellValue(new HSSFRichTextString(dicItem.getName()));
break;
}
}
}
@ -207,18 +203,18 @@ public class ExcelExporterProcess {
private CellStyle baseCellStyle() {
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setWrapText(true);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderBottom(BorderStyle.THIN);
Font font = wb.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setBold(true);
font.setFontName("宋体");
font.setFontHeight((short) 200);
cellStyle.setFont(font);