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

This commit is contained in:
leijun
2019-01-03 18:44:13 +08:00
parent d632670e45
commit ec7e93504c
6 changed files with 210 additions and 13 deletions

View File

@@ -2616,5 +2616,60 @@ public class ExportExcel {
log.debug("Export success.");
*/
}
public <E> ExportExcel ajaxDataList(List<List<Object>>dataMap,String code){
for (int i = 0; i < dataMap.size(); i++) {
Row row = this.addRow(sheets.get(code),rownums.get(code),code);
int colunm = 0;
List<Object> list=dataMap.get(i);
for (int j = 0; j < list.size(); j++) {
this.addCell(row, colunm++,list.get(j), 0,null);
}
}
return this;
}
public ExportExcel(String titleName,String code,String titleTime,List<String> headerList){
this.wb = new SXSSFWorkbook(500);
int j=0;
sheets=new HashMap<>();
rownums=new HashMap<>();
Sheet sheet = wb.createSheet(titleName);
if(!StringUtils.isEmpty(titleTime)){
rownums.put(code, 1);
}else{
rownums.put(code, 0);
}
sheets.put(code, sheet);
this.styles = createStyles(wb);
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.setCellValue(titleTime);
sheet.addMergedRegion(callRangeAddress);
sheet.autoSizeColumn(0);
setBorderStyle(CellStyle.BORDER_THIN,callRangeAddress,sheet,wb);
}
Integer rownum=rownums.get(code);
Row headerRow = sheet.createRow(rownum++);
headerRow.setHeightInPoints(16);
for (int i = 0; i < headerList.size(); i++) {
Cell cell = headerRow.createCell(i);
cell.setCellStyle(styles.get("header"));
cell.setCellValue(headerList.get(i));
sheet.autoSizeColumn(i);
}
for (int i = 0; i < headerList.size(); i++) {
int colWidth = sheet.getColumnWidth(i)*2;
sheet.setColumnWidth(i, colWidth < 3000 ? 3000 : colWidth);
}
j++;
}
}

View File

@@ -2277,6 +2277,24 @@ public class BaseController {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ExcelCsv.writeCSVFile(response,titleList,headMap,dataList,fileName,titleTime,msgProp);
}
public void _ajaxExport(HttpServletRequest request, HttpServletResponse response,
String code,String titleTime,List<String> headerList, List<List<Object>> dataList)
throws Exception{
Properties msgProp = getMsgProp();
String fileName = msgProp.getProperty(code, code) + "_" + DateUtils.getDate("yyyyMMddHHmmss")
+ ".xlsx";
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");
}
new ExportExcel(msgProp.getProperty(code, code),code,titleTime, headerList).ajaxDataList(dataList, code).write(response, fileName).dispose();
}
public String initLogMap(BaseLogEntity log,String title){
Properties msgProp = getMsgProp();
String logTime=msgProp.getProperty(title,title);;

View File

@@ -0,0 +1,29 @@
package com.nis.web.controller.report;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.nis.web.controller.BaseController;
import net.sf.json.JSONObject;
@Controller
@RequestMapping("${adminPath}/export")
public class ExportController extends BaseController {
@RequestMapping(value = "ajaxExport")
public void ajaxExport(String exports,HttpServletRequest request,HttpServletResponse response) throws Exception{
JSONObject jsonObject = JSONObject.fromObject(StringEscapeUtils.unescapeHtml(exports));
Map<String,Object> map=JSONObject.fromObject(jsonObject);
List<List<Object>> list=(List<List<Object>>) 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"));
_ajaxExport(request,response,titleCode, titleTime, heard, list);
}
}

View File

@@ -1,7 +1,7 @@
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/include/taglib.jsp"%>
<script>
function pagination() {
function pagination(pageSizes) {
pageNo = 1;
count = $('tbody').children().size();// 总记录数
@@ -15,8 +15,10 @@ function pagination() {
//$('tbody').children().slice(0, preCount).remove();
} */
pageSize = 30; // 页面大小
if(pageSizes==null || pageSizes=='' ){
pageSizes=30;
}
pageSize = pageSizes; // 页面大小
first = 1;// 首页索引
last = Math.ceil(count/pageSize);// 尾页索引
length = 8;// 显示页面长度

View File

@@ -74,13 +74,13 @@ $(document).ready(function() {
loading('<spring:message code="onloading"/>');
var suffix = $(this).data("export-type");
totaltb(1,-1);
//totaltb(1,-1);
getExportHead();
var te = $(".in table").tableExport({
headings:true,
footers:true,
formats:[suffix],
fileName: getFileName(),
fileName: getFileName('csv'),
bootstrap:false
});
$(".exportFirst").remove();
@@ -114,16 +114,21 @@ $(document).ready(function() {
}
function getFileName() {
function getFileName(type) {
var cfgName = $("[name=cfgName2]").val() + "_";
if ("${bean.serviceId}") {
var action = $("[name=serviceId]").find('[value=' + "${bean.serviceId}" + ']').text() + "_";
} else {
var action = "";
}
var reportType = $("[name=reportType]").find('[value=' + "${bean.reportType}" + ']').text() + "_";
var reportType = $("[name=reportType]").find('[value=' + "${bean.reportType}" + ']').text()
//var reportTime = "${bean.reportTime}";
return cfgName + action + reportType + getStringFormatDate();
if(type=='xlsx'){
return cfgName + action + reportType;
}else{
return cfgName + action + reportType + "_"+ getStringFormatDate();
}
}
function getStringFormatDate() {
@@ -353,10 +358,11 @@ function customColumnClick(){
if(rows.length > 1){
var trtotal="";
if(reportBusinessType=="src_ip_report"){
trtotal+="<td colspan='2' style='text-align: left;' ><spring:message code='report_total' /></td>";
trtotal+="<td style='text-align: left;' ><spring:message code='report_total' /></td><td></td>";
}else{
trtotal+="<td style='text-align: left;'><spring:message code='report_total' /></td>";
}
for (var k = 0; k < list.length; k++) {
trtotal+="<td>"+list[k]+"</td>";
}
@@ -437,8 +443,76 @@ function customColumnClick(){
iframe: true,
append: null
});
pagination();
pagination(30);
}
function getExport(url){
loading('<spring:message code="onloading"/>');
var reportBusinessType=$("#reportBusinessType").val()
var tableId="tagTable";
if(reportBusinessType=="label_report"){
tableId="tagTable";
}else if(reportBusinessType=="lwhh_report"){
tableId="lwhhTable";
}else if(reportBusinessType=="src_ip_report"){
tableId="contentTable1";
}else if(reportBusinessType=="attr_type_report"){
tableId="attrTypeTable";
}else if(reportBusinessType=="dest_ip_report"){
tableId="destIpTable";
}else if(reportBusinessType=="isp_report"){
tableId="entranceIdTable";
}
var timeType = $("#reportType").find("option:selected").text();
var time = $("#intype").val();
var tabName = $("#liwhite").find("li[class='active'] a").attr("title");
var titleCode= getFileName('xlsx');
var heardTime="<spring:message code='${bean.cfgName}'/> "+timeType+""+time+" "+tabName;
pagination(99999999);
var tb=document.getElementById(tableId);
var rows=tb.rows;
var list=new Array();
list.splice(0,list.length);
var column=[]; //获取标题
var index=[];//获取数据列下标
var num=0;
$("#"+tableId+" thead tr th").each(function(){
if($(this).is(":visible")){
column.push($(this).text().trim());
index.push(num);
}
num++;
});
for(var i=1 ; i<rows.length;i++){
var rowMap=[];
for (var j = 0; j <index.length ; j++) {
var n=index[j];
if(rows[i].cells[n].innerHTML.trim().indexOf("...") !=-1){
if(rows[i].cells[n].title.length > 20){
rowMap.push(rows[i].cells[n].title);
}else{
rowMap.push(rows[i].cells[n].innerHTML.trim());
}
}else{
rowMap.push(rows[i].cells[n].innerHTML.trim());
}
}
list[i-1]=rowMap;
}
var map={};
map.heard=column;
map.book=list;
map.titleTime=heardTime;
map.titleCode=titleCode;
var exports=JSON.stringify(map);
aJaxImportPost(url,{"exports":exports});
pagination(30);
closeTip();
}
</script>
<style type="text/css">
li{
@@ -482,7 +556,6 @@ white-space:nowrap;
<form:form id="searchForm" modelAttribute="bean" action="${ctx}/report/list" method="post" class="form-search">
<input id="functionId" name="functionId" type="hidden" value="${bean.functionId}"/>
<input name="cfgName" type="hidden" value="${bean.cfgName}"/>
<input name="cfgName2" type="hidden" value="<spring:message code="${bean.cfgName}"/>"/>
<input name="serviceId2" type="hidden" value="<spring:message code="${bean.serviceId}"/>"/>
@@ -552,7 +625,7 @@ white-space:nowrap;
<i class="fa fa-angle-down"></i>
</button>
<ul class="dropdown-menu pull-right" style="min-width: 110px;" >
<li><a href="javascript:;" class="export-btn" data-export-type="xlsx"><i class="fa fa-download"> <spring:message code="Excel"/></i></a></li>
<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>
</ul>
</div>

View File

@@ -112,4 +112,24 @@ Date.prototype.Format = function (fmt) {
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
}
function aJaxImportPost(url, params) {
// 创建form元素
var temp_form = document.createElement("form");
// 设置form属性
temp_form .action = url;
temp_form .target = "_self";
temp_form .method = "post";
temp_form .style.display = "none";
// 处理需要传递的参数
for (var x in params) {
var opt = document.createElement("textarea");
opt.name = x;
opt.value = params[x];
temp_form .appendChild(opt);
}
document.body.appendChild(temp_form);
// 提交表单
temp_form .submit();
}