实时报表CSV导出调整为后台

This commit is contained in:
leijun
2019-01-04 18:53:18 +08:00
parent 19bb8a916e
commit 8e53249325
4 changed files with 105 additions and 14 deletions

View File

@@ -416,6 +416,9 @@ public class ExcelCsv {
// 写入文件头部
for (String title : titles) {
StringBuffer sb = new StringBuffer();
if(isDate(title)){
title=title + "\t";
}
String rowStr = sb.append("\"").append(title).append("\",").toString();
csvWriter.write(rowStr);
}
@@ -437,9 +440,9 @@ public class ExcelCsv {
tag = tag.replace("\"", "");
}
// \t解决数字0开头字符串0显示不出来的问题比如邮编
if(tag.startsWith("0") && !tag.contains(".")){
/*if(tag.startsWith("0") && !tag.contains(".")){
tag = tag + "\t";
}
}*/
if(isDate(tag)){
tag=tag + "\t";
}
@@ -509,18 +512,70 @@ public class ExcelCsv {
public static boolean isDate(String strDate) {
Pattern pattern = Pattern
.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
if(strDate.trim().length()>= 10){
String date=strDate.trim().substring(0, 10);
Matcher m = pattern.matcher(date);
if (m.matches()) {
return true;
} else {
return false;
}
boolean flag=false;
if(strDate.trim().length()>=10){
strDate=strDate.trim().substring(0, 10);
Matcher m = pattern.matcher(strDate);
if (m.matches()) {
return true;
} else {
return false;
}
}else{
return false;
if(strDate.trim().length()==7){
pattern = Pattern.compile("^\\d{4}-\\d{2}$");
Matcher m = pattern.matcher(strDate);
if (m.matches()) {
return true;
} else {
return false;
}
}else{
return false;
}
}
}
public static <T> void ajaxCSVFile(HttpServletResponse response,List<String>titleList,List<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(!StringUtils.isEmpty(titleTime)){
cs.write(titleTime);
cs.newLine();
}
// 写入文件头部
writeHead(titleList, cs);
// 写入文件内容
for (List<String> row: dataMap) {
writeRow(row, cs);
}
cs.flush();
OutputStream out = response.getOutputStream();
byte[] b = new byte[10240];
File fileLoad = new File(tempFile.getCanonicalPath());
response.reset();
response.setContentType("application/octet-stream; charset=utf-8");
response.setHeader("Content-Disposition", "attachment; filename="+fileName);
long fileLength = fileLoad.length();
String length1 = String.valueOf(fileLength);
response.setHeader("Content_Length", length1);
FileInputStream in = new FileInputStream(fileLoad);
int n;
while ((n = in.read(b)) != -1) {
out.write(b, 0, n); // 每次写入out1024字节
}
in.close();
out.close();
deleteFile(tempFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@@ -2291,7 +2291,22 @@ public class BaseController {
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
}
new ExportExcel(msgProp.getProperty(code, code),code,titleTime, headerList).ajaxDataList(dataList, code).write(response, fileName).dispose();
}
public void _ajaxCsv(HttpServletRequest request, HttpServletResponse response,
String code,String titleTime,List<String> headerList, List<List<String>> dataList)
throws Exception{
Properties msgProp = getMsgProp();
String fileName = msgProp.getProperty(code, code) + "_" + DateUtils.getDate("yyyyMMddHHmmss")
+ ".csv";
fileName = fileName.replaceAll(" ", "_");
if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {
fileName = URLEncoder.encode(fileName, "UTF-8");
} else {
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
}
ExcelCsv.ajaxCSVFile(response,headerList,dataList,fileName,titleTime,msgProp);
}
public String initLogMap(BaseLogEntity log,String title){

View File

@@ -34,6 +34,27 @@ public class ExportController extends BaseController {
}
_ajaxExport(request,response,titleCode, titleTime, heard, list);
}
@RequestMapping(value = "ajaxCsv")
public void ajaxCsv(String exports,HttpServletRequest request,HttpServletResponse response) throws Exception{
JSONObject jsonObject = JSONObject.fromObject(StringEscapeUtils.unescapeHtml(exports));
Map<String,Object> map=JSONObject.fromObject(jsonObject);
List<List<String>> list=(List<List<String >>) map.get("book");
List<String> heard=(List<String>) map.get("heard");
String titleTime=String.valueOf(map.get("titleTime"));
String titleCode=String.valueOf(map.get("titleCode"));
if(!StringUtil.isEmpty(titleTime)){
if(titleTime.contains("\"")){
titleTime = titleTime.replaceAll("\"", "").trim();
}
if(titleTime.startsWith("[")){
titleTime = titleTime.substring(1, titleTime.length()-1);
}
}
_ajaxCsv(request,response,titleCode, titleTime, heard, list);
}
}

View File

@@ -631,9 +631,9 @@ white-space:nowrap;
<i class="fa fa-wrench"></i> <spring:message code="export"></spring:message>
<i class="fa fa-angle-down"></i>
</button>
<ul class="dropdown-menu pull-right" style="min-width: 110px;" >
<ul class="dropdown-menu pull-right" style="min-width: 110px;" >
<li><a href="javascript:;" onclick="getExport('${ctx}/export/ajaxExport')" ><i class="fa fa-download"> <spring:message code="Excel"/></i></a></li>
<li><a href="javascript:;" class="export-btn" data-export-type="csv"><i class="fa fa-download"> <spring:message code="CSV"/></i></a></li>
<li><a href="javascript:;" onclick="getExport('${ctx}/export/ajaxCsv')" ><i class="fa fa-download"> <spring:message code="CSV"/></i></a></li>
</ul>
</div>
<div class="btn-group">