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

@ -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,193 +34,191 @@ 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 Row titleRow;
private int rowNum ; public ExcelExporterProcess(List<Map<String, Object>> values, MetadataTable table, OutputStream output) {
this.values = values;
this.table = table;
this.output = output;
wb = new HSSFWorkbook();
sheet = wb.createSheet();
firstStyle = createFirstCellStyle();
createHead();
}
private List<Map<String ,Object>> values ; public void process() throws IOException {
private MetadataTable table ; createContent();
private OutputStream output ; if (table != null) {
private Row titleRow ; for (TableProperties tp : table.getTableproperty()) {
sheet.autoSizeColumn(table.getTableproperty().indexOf(tp));
}
wb.write(this.output);
}
}
public ExcelExporterProcess(List<Map<String ,Object>> values , MetadataTable table , OutputStream output) { /**
this.values = values ; * 构建头部
this.table = table ; */
this.output = output; private void createHead() {
wb = new HSSFWorkbook(); titleRow = sheet.createRow(rowNum);
sheet = wb.createSheet(); if (table != null && table.getTableproperty() != null) {
firstStyle = createFirstCellStyle(); for (TableProperties tp : table.getTableproperty()) {
createHead() ; Cell cell2 = titleRow.createCell(table.getTableproperty().indexOf(tp));
} cell2.setCellStyle(firstStyle);
public void process() throws IOException{ cell2.setCellValue(new HSSFRichTextString(tp.getName()));
createContent(); }
if(table!=null){ }
for(TableProperties tp : table.getTableproperty()){ rowNum++;
sheet.autoSizeColumn(table.getTableproperty().indexOf(tp)) ; }
}
wb.write(this.output);
}
}
/** private CellStyle createContentStyle() {
* 构建头部 CellStyle cellStyle = wb.createCellStyle();
*/
private void createHead(){
titleRow = sheet.createRow(rowNum);
if(table!=null && table.getTableproperty()!=null){
for(TableProperties tp : table.getTableproperty()){
Cell cell2 = titleRow.createCell(table.getTableproperty().indexOf(tp));
cell2.setCellStyle(firstStyle);
cell2.setCellValue(new HSSFRichTextString(tp.getName()));
}
}
rowNum ++ ;
}
private CellStyle createContentStyle(){ cellStyle.setAlignment(HorizontalAlignment.CENTER); // 指定单元格居中对齐
CellStyle cellStyle = wb.createCellStyle(); cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 指定单元格垂直居中对齐
cellStyle.setWrapText(false);// 指定单元格自动换行
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐 // 设置单元格字体
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐 Font font = wb.createFont();
cellStyle.setWrapText(false);// 指定单元格自动换行 font.setFontName("微软雅黑");
font.setFontHeight((short) 200);
cellStyle.setFont(font);
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderBottom(BorderStyle.THIN);
return cellStyle;
}
// 设置单元格字体 /**
Font font = wb.createFont(); * 首列样式
font.setFontName("微软雅黑"); */
font.setFontHeight((short) 200); private CellStyle createFirstCellStyle() {
cellStyle.setFont(font); CellStyle cellStyle = baseCellStyle();
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); Font font = wb.createFont();
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); font.setFontName("微软雅黑");
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); font.setFontHeight((short) 180);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); cellStyle.setFont(font);
return cellStyle ;
}
/** cellStyle.setWrapText(false);
* 首列样式
* @return
*/
private CellStyle createFirstCellStyle(){
CellStyle cellStyle = baseCellStyle();
Font font = wb.createFont();
font.setFontName("微软雅黑");
font.setFontHeight((short) 180);
cellStyle.setFont(font);
cellStyle.setWrapText(false); cellStyle.setFillForegroundColor(HSSFColor.HSSFColorPredefined.LIGHT_GREEN.getIndex());
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellStyle.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
return cellStyle; return cellStyle;
} }
private synchronized void createContent(){ private synchronized void createContent() {
CellStyle cellStyle = createContentStyle() ; CellStyle cellStyle = createContentStyle();
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++);
cell2.setCellStyle(cellStyle); cell2.setCellStyle(cellStyle);
if(value.get(tp.getFieldname())!=null){ if (value.get(tp.getFieldname()) != null) {
if(tp.isModits()) { if (tp.isModits()) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<String> list = (List<String>)value.get(tp.getFieldname()); List<String> list = (List<String>) value.get(tp.getFieldname());
if(list.size()>0) { if (list.size() > 0) {
cell2.setCellValue(new HSSFRichTextString(list.remove(0))); cell2.setCellValue(new HSSFRichTextString(list.remove(0)));
} }
ExportData expData = new ExportData(tp , list) ; ExportData expData = new ExportData(tp, list);
if(list.size()>0) { if (list.size() > 0) {
tempExportDatas.add(expData) ; tempExportDatas.add(expData);
if(list.size() > expData.getMaxcols()) { if (list.size() > expData.getMaxcols()) {
expData.setMaxcols(list.size()); expData.setMaxcols(list.size());
} }
} }
}else if(tp.isSeldata()){ } else if (tp.isSeldata()) {
SysDic sysDic = Dict.getInstance().getDicItem(String.valueOf(value.get(tp.getFieldname()))) ; SysDic sysDic = Dict.getInstance().getDicItem(String.valueOf(value.get(tp.getFieldname())));
if(sysDic!=null) { if (sysDic != null) {
cell2.setCellValue(new HSSFRichTextString(sysDic.getName())); cell2.setCellValue(new HSSFRichTextString(sysDic.getName()));
}else { } else {
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";
}else { } else {
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;
} }
} }
} }
}else if(tp.isReffk() && !StringUtils.isBlank(tp.getReftbid())){ }
String key = (String) value.get(tp.getFieldname()) ; } else if (tp.isReffk() && !StringUtils.isBlank(tp.getReftbid())) {
String orgi = (String) value.get("orgi") ; String key = (String) value.get(tp.getFieldname());
if(!StringUtils.isBlank(key) && !StringUtils.isBlank(orgi)) { String orgi = (String) value.get("orgi");
DataExchangeInterface exchange = (DataExchangeInterface) MainContext.getContext().getBean(tp.getReftbid()) ; if (!StringUtils.isBlank(key) && !StringUtils.isBlank(orgi)) {
Object refvalue = exchange.getDataByIdAndOrgi(key, orgi) ; DataExchangeInterface exchange = (DataExchangeInterface) MainContext.getContext().getBean(tp.getReftbid());
if(refvalue!=null) { Object refvalue = exchange.getDataByIdAndOrgi(key, orgi);
cell2.setCellValue(new HSSFRichTextString(refvalue.toString())); if (refvalue != null) {
} cell2.setCellValue(new HSSFRichTextString(refvalue.toString()));
} }
}else{ }
cell2.setCellValue(new HSSFRichTextString(String.valueOf(value.get(tp.getFieldname())))); } else {
} cell2.setCellValue(new HSSFRichTextString(String.valueOf(value.get(tp.getFieldname()))));
} }
} }
if(tempExportDatas.size() > 0) { }
for(ExportData expData : tempExportDatas) { if (tempExportDatas.size() > 0) {
for(int i=0 ; i<expData.getMaxcols() ; i++) { for (ExportData expData : tempExportDatas) {
if(titleRow.getCell(cols + i) == null) { for (int i = 0; i < expData.getMaxcols(); i++) {
Cell title = titleRow.createCell(cols + i); if (titleRow.getCell(cols + i) == null) {
title.setCellStyle(firstStyle); Cell title = titleRow.createCell(cols + i);
title.setCellValue(new HSSFRichTextString(expData.getTp().getName())); title.setCellStyle(firstStyle);
} title.setCellValue(new HSSFRichTextString(expData.getTp().getName()));
} }
}
for(String itemValue : expData.getValues()) { for (String itemValue : expData.getValues()) {
Cell cell2 = row2.createCell(cols++); Cell cell2 = row2.createCell(cols++);
cell2.setCellValue(new HSSFRichTextString(itemValue)); cell2.setCellValue(new HSSFRichTextString(itemValue));
} }
} }
} }
rowNum ++ ; rowNum++;
} }
} }
} }
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);
return cellStyle; return cellStyle;
} }
} }