Excel导出方法 添加时间范围
This commit is contained in:
@@ -58,6 +58,21 @@ public class BaseCfg<T> extends BaseEntity<T> implements Cloneable{
|
||||
|
||||
private Map<Integer,Integer> compileGroupMap;
|
||||
protected Integer compileIsIssued;
|
||||
protected String exType;//导出类型
|
||||
protected String hcolumn;//导出隐藏列
|
||||
|
||||
public String getExType() {
|
||||
return exType;
|
||||
}
|
||||
public void setExType(String exType) {
|
||||
this.exType = exType;
|
||||
}
|
||||
public String getHcolumn() {
|
||||
return hcolumn;
|
||||
}
|
||||
public void setHcolumn(String hcolumn) {
|
||||
this.hcolumn = hcolumn;
|
||||
}
|
||||
public Integer getCompileIsIssued() {
|
||||
return compileIsIssued;
|
||||
}
|
||||
|
||||
@@ -9,9 +9,11 @@ import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -25,6 +27,7 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.nis.domain.basics.ServiceDictInfo;
|
||||
import com.nis.util.DictUtils;
|
||||
import com.nis.util.Encodes;
|
||||
import com.nis.util.Reflections;
|
||||
@@ -277,10 +280,16 @@ public class ExcelCsv {
|
||||
// If is dict, get dict label
|
||||
if (StringUtils.isNotBlank(ef.dictType())){
|
||||
String valStr=val==null?"":val.toString();
|
||||
if("type".equals(ef.dictType()) || "attribute".equals(ef.dictType())
|
||||
|| "label".equals(ef.dictType())){
|
||||
// Get basic info
|
||||
val = getBasicInfo(ef.dictType(),map,valStr);
|
||||
}else{
|
||||
//字典数据已做国际化处理
|
||||
String dict=DictUtils.getDictLabel(ef.dictType(), valStr, valStr);
|
||||
//如果找不到字典国际化值,把字典本身作为默认值放进去,不然导出就是空了
|
||||
val = msgProp.getProperty(dict,dict);
|
||||
}
|
||||
|
||||
}
|
||||
if(ef.title().equals("is_hex") && !StringUtil.isEmpty(val)){
|
||||
@@ -303,6 +312,12 @@ public class ExcelCsv {
|
||||
val ="";
|
||||
}
|
||||
}
|
||||
if(!StringUtil.isEmpty(val)) {
|
||||
if (val instanceof Date){
|
||||
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
val=sdf.format(val);
|
||||
}
|
||||
}
|
||||
}catch(Exception ex) {
|
||||
log.error("Get entity value failed",ex);
|
||||
val = "";
|
||||
@@ -316,24 +331,40 @@ public class ExcelCsv {
|
||||
return dataList;
|
||||
}
|
||||
|
||||
public static <T> void writeCSVFile(HttpServletResponse response,List<String> titleList,Map<String, List<String>> headMap, Map<String, List<String>> dataMap,String fileName) {
|
||||
public static <T> void writeCSVFile(HttpServletResponse response,List<String> titleList,Map<String, List<String>> headMap, Map<String, List<String>> dataMap,String fileName,
|
||||
String titleTime,Properties msgProp) {
|
||||
try {
|
||||
// 写入临时文件
|
||||
File tempFile = File.createTempFile("vehicle", ".csv");
|
||||
cs = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tempFile), "utf-8"), 1024);
|
||||
cs.write(new String(bom, charset));
|
||||
//获取头部数据
|
||||
if(titleList.size()>0){
|
||||
List<String> heads=headMap.get(titleList.get(0));
|
||||
for (int i = 0; i < titleList.size(); i++) {
|
||||
if(i==0){
|
||||
//写入时间范围
|
||||
if(!StringUtils.isEmpty(titleTime)){
|
||||
cs.write(titleTime);
|
||||
cs.newLine();
|
||||
}
|
||||
}
|
||||
List<String> heads=headMap.get(titleList.get(i));
|
||||
if(i>0){
|
||||
//写入域配置标题
|
||||
cs.newLine();
|
||||
if(!StringUtils.isEmpty(titleList.get(i))){
|
||||
cs.write(msgProp.getProperty(titleList.get(i), titleList.get(i)));
|
||||
cs.newLine();
|
||||
}
|
||||
}
|
||||
// 写入文件头部
|
||||
writeHead(heads, cs);
|
||||
//获取文件内容
|
||||
List<String> datas=dataMap.get(titleList.get(0));
|
||||
List<String> datas=dataMap.get(titleList.get(i));
|
||||
// 写入文件内容
|
||||
for (String row : datas) {
|
||||
writeRow(row, cs);
|
||||
}
|
||||
}
|
||||
}
|
||||
cs.flush();
|
||||
OutputStream out = response.getOutputStream();
|
||||
byte[] b = new byte[10240];
|
||||
@@ -409,6 +440,56 @@ public class ExcelCsv {
|
||||
}
|
||||
csvWriter.newLine();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置性质、分类、标签等信息
|
||||
* @param dictType
|
||||
* @param map
|
||||
* @param val
|
||||
* @return
|
||||
*/
|
||||
public static String getBasicInfo(String dictType,Map map,String val) {
|
||||
String basicInfo="";
|
||||
List<ServiceDictInfo> list=new ArrayList<ServiceDictInfo>();
|
||||
if("type".equals(dictType)){
|
||||
list=(List<ServiceDictInfo>) map.get("fls");
|
||||
for (String info : val.split(",")) {
|
||||
for (ServiceDictInfo dictInfo : list) {
|
||||
if(info.equals(dictInfo.getServiceDictId().toString())){
|
||||
basicInfo+=","+dictInfo.getItemValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if("attribute".equals(dictType)){
|
||||
list=(List<ServiceDictInfo>) map.get("xzs");
|
||||
for (String info : val.split(",")) {
|
||||
for (ServiceDictInfo dictInfo : list) {
|
||||
if(info.equals(dictInfo.getServiceDictId().toString())){
|
||||
basicInfo+=","+dictInfo.getItemValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if("label".equals(dictType)){
|
||||
list=(List<ServiceDictInfo>) map.get("labels");
|
||||
for (String info : val.split(",")) {
|
||||
for (ServiceDictInfo dictInfo : list) {
|
||||
if(info.equals(dictInfo.getServiceDictId().toString())){
|
||||
basicInfo+=","+dictInfo.getItemValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!StringUtil.isEmpty(basicInfo)){
|
||||
basicInfo=basicInfo.substring(1);
|
||||
}
|
||||
return basicInfo;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 功能:判断字符串是否为日期格式
|
||||
*
|
||||
|
||||
@@ -1778,6 +1778,7 @@ public class ExportExcel {
|
||||
|
||||
return styles;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 添加一行
|
||||
@@ -1963,6 +1964,10 @@ public class ExportExcel {
|
||||
}
|
||||
}
|
||||
if(!StringUtil.isEmpty(val)){
|
||||
if (val instanceof Date){
|
||||
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
val=sdf.format(val);
|
||||
}
|
||||
val = Encodes.unescapeHtml(String.valueOf(val));
|
||||
}
|
||||
|
||||
@@ -2108,7 +2113,7 @@ public class ExportExcel {
|
||||
* @param groups 导入分组
|
||||
* @return
|
||||
*/
|
||||
public ExportExcel(Properties msgProp,List<String> titleList,Map<String,String> noExportMap,Map<String, Class<?>> clsMap, int type, int... groups){
|
||||
public ExportExcel(Properties msgProp,List<String> titleList,Map<String,String> noExportMap,Map<String, Class<?>> clsMap, int type,String titleTime, int... groups){
|
||||
Map<String, List<String>> headerMap=new HashMap<String, List<String>>();
|
||||
Map<String, List<String>> commentMap=new HashMap<String, List<String>>();
|
||||
for (String title : titleList) {
|
||||
@@ -2226,7 +2231,7 @@ public class ExportExcel {
|
||||
commentMap.put(title, commentList);
|
||||
annotationMap.put(title, annotationList);
|
||||
}
|
||||
initializeMultiSheet(msgProp,titleList, headerMap,commentMap);
|
||||
initializeMultiSheet(msgProp,titleList, headerMap,commentMap,titleTime);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2234,7 +2239,7 @@ public class ExportExcel {
|
||||
* @param title 表格标题,传“空值”,表示无标题
|
||||
* @param headerList 表头列表
|
||||
*/
|
||||
private void initializeMultiSheet(Properties msgProp,List<String> titleList,Map<String, List<String>> headerMap,Map<String,List<String>> commentMap) {
|
||||
private void initializeMultiSheet(Properties msgProp,List<String> titleList,Map<String, List<String>> headerMap,Map<String,List<String>> commentMap,String titleTime) {
|
||||
this.wb = new SXSSFWorkbook(500);
|
||||
int j=0;
|
||||
sheets=new HashMap<>();
|
||||
@@ -2249,13 +2254,31 @@ public class ExportExcel {
|
||||
}
|
||||
Sheet sheet = wb.createSheet(titleName);
|
||||
num++;
|
||||
rownums.put(title, 0);
|
||||
if(!StringUtils.isEmpty(titleTime)){
|
||||
rownums.put(title, 1);
|
||||
}else{
|
||||
rownums.put(title, 0);
|
||||
}
|
||||
sheets.put(title, sheet);
|
||||
this.styles = createStyles(wb);
|
||||
// Create header
|
||||
if (headerList == null){
|
||||
throw new RuntimeException("headerList not null!");
|
||||
}
|
||||
|
||||
if(!StringUtils.isEmpty(titleTime)){
|
||||
//添加一行数据
|
||||
CellRangeAddress callRangeAddress = new CellRangeAddress(0,0,0,headerList.size()-1);
|
||||
CellStyle style = styles.get("data");
|
||||
Row timeRow = sheet.createRow(0);
|
||||
timeRow.setHeightInPoints(16);
|
||||
Cell cellt = timeRow.createCell(0);
|
||||
cellt.setCellStyle(style);
|
||||
cellt.setCellValue(titleTime);
|
||||
sheet.addMergedRegion(callRangeAddress);
|
||||
sheet.autoSizeColumn(0);
|
||||
//--------------------
|
||||
}
|
||||
Integer rownum=rownums.get(title);
|
||||
Row headerRow = sheet.createRow(rownum++);
|
||||
headerRow.setHeightInPoints(16);
|
||||
@@ -2283,9 +2306,6 @@ public class ExportExcel {
|
||||
log.debug("Initialize success.");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 输出数据流
|
||||
* @param os 输出数据流
|
||||
|
||||
@@ -1987,10 +1987,12 @@ public class BaseController {
|
||||
} else {
|
||||
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
|
||||
}
|
||||
new ExportExcel(msgProp, titleList, noExportMap, classMap, 1).setDataList(msgProp, dataMap, map)
|
||||
String titleTime=noExportMap.get("timeRange");
|
||||
new ExportExcel(msgProp, titleList, noExportMap, classMap, 1,titleTime).setDataList(msgProp, dataMap, map)
|
||||
.write(response, fileName).dispose();
|
||||
}
|
||||
|
||||
|
||||
public void _exportCsv(Model model, HttpServletRequest request, HttpServletResponse response,
|
||||
RedirectAttributes redirectAttributes, String functionName, List<String> titleList,
|
||||
Map<String, Class<?>> classMap, Map<String, List> dataMap, Map<String, String> noExportMap)
|
||||
@@ -2015,11 +2017,48 @@ public class BaseController {
|
||||
} else {
|
||||
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
|
||||
}
|
||||
|
||||
String titleTime=noExportMap.get("timeRange");
|
||||
Map<String, List<String>> headMap=ExcelCsv.ExcelCsvHeader(msgProp, titleList, noExportMap, classMap, 1);
|
||||
Map<String, List<String>> dataList=ExcelCsv.setDataList(msgProp, dataMap, map);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ExcelCsv.writeCSVFile(response,titleList,headMap,dataList,fileName);
|
||||
ExcelCsv.writeCSVFile(response,titleList,headMap,dataList,fileName,titleTime,msgProp);
|
||||
}
|
||||
|
||||
|
||||
public String initTimeMap(BaseCfg cfg){
|
||||
Properties msgProp = getMsgProp();
|
||||
String titleTime=msgProp.getProperty(cfg.getMenuNameCode(),cfg.getMenuNameCode());
|
||||
if(cfg.getSearch_create_time_start()!=null){
|
||||
titleTime+=" "+msgProp.getProperty("config_time")+":"+DateUtils.formatDateTime(cfg.getSearch_create_time_start());
|
||||
if(cfg.getSearch_create_time_end()!=null){
|
||||
titleTime+="—"+DateUtils.formatDateTime(cfg.getSearch_create_time_end());
|
||||
}
|
||||
}else{
|
||||
if(cfg.getSearch_create_time_end()!=null){
|
||||
titleTime+=" "+msgProp.getProperty("config_time")+":—"+DateUtils.formatDateTime(cfg.getSearch_create_time_end());
|
||||
}
|
||||
}
|
||||
if(cfg.getSearch_edit_time_start()!=null){
|
||||
titleTime+=" "+msgProp.getProperty("edit_time")+":"+DateUtils.formatDateTime(cfg.getSearch_edit_time_start());
|
||||
if(cfg.getSearch_edit_time_end()!=null){
|
||||
titleTime+="—"+DateUtils.formatDateTime(cfg.getSearch_edit_time_end());
|
||||
}
|
||||
}else{
|
||||
if(cfg.getSearch_edit_time_end()!=null){
|
||||
titleTime+=" "+msgProp.getProperty("edit_time")+":—"+DateUtils.formatDateTime(cfg.getSearch_edit_time_end());
|
||||
}
|
||||
}
|
||||
if(cfg.getSearch_audit_time_start()!=null){
|
||||
titleTime+=" "+msgProp.getProperty("audit_time")+":"+DateUtils.formatDateTime(cfg.getSearch_audit_time_start());
|
||||
if(cfg.getSearch_audit_time_end()!=null){
|
||||
titleTime+="—"+DateUtils.formatDateTime(cfg.getSearch_audit_time_end());
|
||||
}
|
||||
}else{
|
||||
if(cfg.getSearch_audit_time_end()!=null){
|
||||
titleTime+=" "+msgProp.getProperty("audit_time")+": —"+DateUtils.formatDateTime(cfg.getSearch_audit_time_end());
|
||||
}
|
||||
}
|
||||
return titleTime;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -25,9 +25,19 @@
|
||||
<c:when test="${label eq 'cancelPass'}">
|
||||
<a href="javascript:void(0);" onclick="cancelPassOpt('${url}')"><i class="fa fa-undo"></i> <spring:message code="cancel_approved"/></a>
|
||||
</c:when>
|
||||
<c:when test="${label eq 'cfg_excel'}">
|
||||
<a href="javascript:void(0);" onclick="exportData('${url}',${fns:getStringProperty('maxExportSize',1000000)},'${searchUrl}')" data-toggle="tooltip" data-placement="top">
|
||||
<i class="fa fa-download"> <spring:message code="Excel"/></i>
|
||||
</a>
|
||||
</c:when>
|
||||
<c:when test="${label eq 'export'}">
|
||||
<a href="javascript:void(0);" class="btn btn-default" onclick="exportData('${url}',${fns:getStringProperty('maxExportSize',1000000)},'${searchUrl}')" data-toggle="tooltip" data-placement="top">
|
||||
<i class="fa fa-download"> <spring:message code="export"/></i>
|
||||
<a href="javascript:void(0);" class="btn btn-default" onclick="exportData('${url}',${fns:getStringProperty('maxExportSize',1000000)},'${searchUrl}')" data-toggle="tooltip" data-placement="top">
|
||||
<i class="fa fa-download"> <spring:message code="Excel"/></i>
|
||||
</a>
|
||||
</c:when>
|
||||
<c:when test="${label eq 'cfg_csv'}">
|
||||
<a href="javascript:void(0);" onclick="exportData('${url}',${fns:getStringProperty('maxExportSize',1000000)},'${searchUrl}')" data-toggle="tooltip" data-placement="top">
|
||||
<i class="fa fa-download"> <spring:message code="CSV"/></i>
|
||||
</a>
|
||||
</c:when>
|
||||
<c:when test="${label eq 'excel'}">
|
||||
|
||||
Reference in New Issue
Block a user