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

@ -1,228 +1,224 @@
/* /*
* Copyright (C) 2017 优客服-多渠道客服系统 * Copyright (C) 2017 优客服-多渠道客服系统
* Modifications copyright (C) 2018-2019 Chatopera Inc, <https://www.chatopera.com> * Modifications copyright (C) 2018-2019 Chatopera Inc, <https://www.chatopera.com>
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.chatopera.cc.util.dsdata.export; package com.chatopera.cc.util.dsdata.export;
import com.chatopera.cc.basic.MainContext; import com.chatopera.cc.basic.MainContext;
import com.chatopera.cc.model.Dict; import com.chatopera.cc.model.Dict;
import com.chatopera.cc.model.MetadataTable; import com.chatopera.cc.model.MetadataTable;
import com.chatopera.cc.model.SysDic; 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.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*; import java.io.IOException;
import java.io.OutputStream;
import java.io.IOException; import java.util.ArrayList;
import java.io.OutputStream; import java.util.List;
import java.util.ArrayList; import java.util.Map;
import java.util.List;
import java.util.Map; public class ExcelExporterProcess {
private final HSSFWorkbook wb;
@SuppressWarnings("deprecation") private final Sheet sheet;
public class ExcelExporterProcess { private final CellStyle firstStyle;
private HSSFWorkbook wb; private final List<Map<String, Object>> values;
private Sheet sheet; private final MetadataTable table;
private CellStyle firstStyle = null ; private final OutputStream output;
private int rowNum;
private int rowNum ; private Row titleRow;
private List<Map<String ,Object>> values ; public ExcelExporterProcess(List<Map<String, Object>> values, MetadataTable table, OutputStream output) {
private MetadataTable table ; this.values = values;
private OutputStream output ; this.table = table;
private Row titleRow ; this.output = output;
wb = new HSSFWorkbook();
public ExcelExporterProcess(List<Map<String ,Object>> values , MetadataTable table , OutputStream output) { sheet = wb.createSheet();
this.values = values ; firstStyle = createFirstCellStyle();
this.table = table ; createHead();
this.output = output; }
wb = new HSSFWorkbook();
sheet = wb.createSheet(); public void process() throws IOException {
firstStyle = createFirstCellStyle(); createContent();
createHead() ; if (table != null) {
} for (TableProperties tp : table.getTableproperty()) {
public void process() throws IOException{ sheet.autoSizeColumn(table.getTableproperty().indexOf(tp));
createContent(); }
if(table!=null){ wb.write(this.output);
for(TableProperties tp : table.getTableproperty()){ }
sheet.autoSizeColumn(table.getTableproperty().indexOf(tp)) ; }
}
wb.write(this.output); /**
} * 构建头部
} */
private void createHead() {
/** titleRow = sheet.createRow(rowNum);
* 构建头部 if (table != null && table.getTableproperty() != null) {
*/ for (TableProperties tp : table.getTableproperty()) {
private void createHead(){ Cell cell2 = titleRow.createCell(table.getTableproperty().indexOf(tp));
titleRow = sheet.createRow(rowNum); cell2.setCellStyle(firstStyle);
if(table!=null && table.getTableproperty()!=null){ cell2.setCellValue(new HSSFRichTextString(tp.getName()));
for(TableProperties tp : table.getTableproperty()){ }
Cell cell2 = titleRow.createCell(table.getTableproperty().indexOf(tp)); }
cell2.setCellStyle(firstStyle); rowNum++;
cell2.setCellValue(new HSSFRichTextString(tp.getName())); }
}
} private CellStyle createContentStyle() {
rowNum ++ ; CellStyle cellStyle = wb.createCellStyle();
}
cellStyle.setAlignment(HorizontalAlignment.CENTER); // 指定单元格居中对齐
private CellStyle createContentStyle(){ cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 指定单元格垂直居中对齐
CellStyle cellStyle = wb.createCellStyle(); 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);
Font font = wb.createFont(); cellStyle.setBorderTop(BorderStyle.THIN);
font.setFontName("微软雅黑"); cellStyle.setBorderLeft(BorderStyle.THIN);
font.setFontHeight((short) 200); cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setFont(font); cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); return cellStyle;
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); }
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); /**
return cellStyle ; * 首列样式
} */
private CellStyle createFirstCellStyle() {
/** CellStyle cellStyle = baseCellStyle();
* 首列样式 Font font = wb.createFont();
* @return font.setFontName("微软雅黑");
*/ font.setFontHeight((short) 180);
private CellStyle createFirstCellStyle(){ cellStyle.setFont(font);
CellStyle cellStyle = baseCellStyle();
Font font = wb.createFont(); cellStyle.setWrapText(false);
font.setFontName("微软雅黑");
font.setFontHeight((short) 180); cellStyle.setFillForegroundColor(HSSFColor.HSSFColorPredefined.LIGHT_GREEN.getIndex());
cellStyle.setFont(font); cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellStyle.setWrapText(false);
return cellStyle;
cellStyle.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index); }
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
private synchronized void createContent() {
return cellStyle; CellStyle cellStyle = createContentStyle();
} if (table != null && table.getTableproperty() != null) {
for (Map<String, Object> value : values) {
Row row2 = sheet.createRow(rowNum);
private synchronized void createContent(){ List<ExportData> tempExportDatas = new ArrayList<>();
CellStyle cellStyle = createContentStyle() ; int cols = 0;
if(table!=null && table.getTableproperty()!=null){ for (TableProperties tp : table.getTableproperty()) {
for(Map<String , Object> value:values){ Cell cell2 = row2.createCell(cols++);
Row row2 = sheet.createRow(rowNum); cell2.setCellStyle(cellStyle);
List<ExportData> tempExportDatas = new ArrayList<ExportData>(); if (value.get(tp.getFieldname()) != null) {
int cols = 0 ; if (tp.isModits()) {
for(TableProperties tp : table.getTableproperty()){ @SuppressWarnings("unchecked")
Cell cell2 = row2.createCell(cols++); List<String> list = (List<String>) value.get(tp.getFieldname());
cell2.setCellStyle(cellStyle); if (list.size() > 0) {
if(value.get(tp.getFieldname())!=null){ cell2.setCellValue(new HSSFRichTextString(list.remove(0)));
if(tp.isModits()) { }
@SuppressWarnings("unchecked") ExportData expData = new ExportData(tp, list);
List<String> list = (List<String>)value.get(tp.getFieldname()); if (list.size() > 0) {
if(list.size()>0) { tempExportDatas.add(expData);
cell2.setCellValue(new HSSFRichTextString(list.remove(0))); if (list.size() > expData.getMaxcols()) {
} expData.setMaxcols(list.size());
ExportData expData = new ExportData(tp , list) ; }
if(list.size()>0) { }
tempExportDatas.add(expData) ; } else if (tp.isSeldata()) {
if(list.size() > expData.getMaxcols()) { SysDic sysDic = Dict.getInstance().getDicItem(String.valueOf(value.get(tp.getFieldname())));
expData.setMaxcols(list.size()); if (sysDic != null) {
} cell2.setCellValue(new HSSFRichTextString(sysDic.getName()));
} } else {
}else if(tp.isSeldata()){ List<SysDic> dicItemList = Dict.getInstance().getSysDic(tp.getSeldatacode());
SysDic sysDic = Dict.getInstance().getDicItem(String.valueOf(value.get(tp.getFieldname()))) ; if (dicItemList != null && dicItemList.size() > 0) {
if(sysDic!=null) { for (SysDic dicItem : dicItemList) {
cell2.setCellValue(new HSSFRichTextString(sysDic.getName())); String s;
}else { Object obj = value.get(tp.getFieldname());
List<SysDic> dicItemList = Dict.getInstance().getSysDic(tp.getSeldatacode()); if (obj instanceof Boolean) {
if(dicItemList!=null && dicItemList.size() > 0) { s = (Boolean) obj ? "1" : "0";
for(SysDic dicItem : dicItemList) { } else {
String s = ""; s = String.valueOf(value.get(tp.getFieldname()));
Object obj = value.get(tp.getFieldname()); }
if(obj instanceof Boolean) { if (dicItem.getCode().equals(s)) {
s = (Boolean)obj?"1":"0"; cell2.setCellValue(new HSSFRichTextString(dicItem.getName()));
}else { break;
s= String.valueOf(value.get(tp.getFieldname())); }
} }
if(dicItem.getCode().equals(s)) { }
cell2.setCellValue(new HSSFRichTextString(dicItem.getName())); break ; }
} } else if (tp.isReffk() && !StringUtils.isBlank(tp.getReftbid())) {
} String key = (String) value.get(tp.getFieldname());
} String orgi = (String) value.get("orgi");
} if (!StringUtils.isBlank(key) && !StringUtils.isBlank(orgi)) {
}else if(tp.isReffk() && !StringUtils.isBlank(tp.getReftbid())){ DataExchangeInterface exchange = (DataExchangeInterface) MainContext.getContext().getBean(tp.getReftbid());
String key = (String) value.get(tp.getFieldname()) ; Object refvalue = exchange.getDataByIdAndOrgi(key, orgi);
String orgi = (String) value.get("orgi") ; if (refvalue != null) {
if(!StringUtils.isBlank(key) && !StringUtils.isBlank(orgi)) { cell2.setCellValue(new HSSFRichTextString(refvalue.toString()));
DataExchangeInterface exchange = (DataExchangeInterface) MainContext.getContext().getBean(tp.getReftbid()) ; }
Object refvalue = exchange.getDataByIdAndOrgi(key, orgi) ; }
if(refvalue!=null) { } else {
cell2.setCellValue(new HSSFRichTextString(refvalue.toString())); 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) {
} for (int i = 0; i < expData.getMaxcols(); i++) {
} if (titleRow.getCell(cols + i) == null) {
if(tempExportDatas.size() > 0) { Cell title = titleRow.createCell(cols + i);
for(ExportData expData : tempExportDatas) { title.setCellStyle(firstStyle);
for(int i=0 ; i<expData.getMaxcols() ; i++) { title.setCellValue(new HSSFRichTextString(expData.getTp().getName()));
if(titleRow.getCell(cols + i) == null) { }
Cell title = titleRow.createCell(cols + i); }
title.setCellStyle(firstStyle);
title.setCellValue(new HSSFRichTextString(expData.getTp().getName())); for (String itemValue : expData.getValues()) {
} Cell cell2 = row2.createCell(cols++);
} cell2.setCellValue(new HSSFRichTextString(itemValue));
}
for(String itemValue : expData.getValues()) { }
Cell cell2 = row2.createCell(cols++); }
cell2.setCellValue(new HSSFRichTextString(itemValue)); rowNum++;
} }
} }
} }
rowNum ++ ;
}
} private CellStyle baseCellStyle() {
} CellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
private CellStyle baseCellStyle(){ cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); cellStyle.setWrapText(true);
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setWrapText(true); cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); Font font = wb.createFont();
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); font.setBold(true);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); font.setFontName("宋体");
font.setFontHeight((short) 200);
Font font = wb.createFont(); cellStyle.setFont(font);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontName("宋体"); return cellStyle;
font.setFontHeight((short) 200); }
cellStyle.setFont(font); }
return cellStyle;
}
}