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