我们做系统时经常遇到到处excel的需求,比如表格的导出,报表的导出。
表格的导出相对简单点,excel没有特殊要求,就是一行一列一行一列的导出,我们的系统前端表格用jqgrid,所以点击导出时用下面的js重新调用表格数据的加载方法
/**
HZ.expGrid(ggridId,['no'],["id:id"],false);
*/
HZ.expGrid=function(ggridId,filterCols,addCols,currentPage){
var url = jQGridSupport.getParam(ggridId,"url");
var colModel = jQGridSupport.getParam(ggridId,"colModel");
var pstrlist=[];
for(var i=0;i
var add=true;
if(filterCols)//过滤不需要导出的列
for(var j=0;j
}
if(add)pstrlist.push(colModel[i].name+":"+colModel[i].label);
}
}
//添加页面没有显示的列
if(addCols)for(var k=0;k
var rowNum=false;
if(currentPage)
rowNum= jQGridSupport.getParam(ggridId,"rowNum");
HZ.exportExcel(url,pstrlist.join(","),data,rowNum);
}
HZ.exportExcel=function(url,expCols,postData,rownum){
var rows=60000;
if(rownum)rows=rownum;
var data={page:1,rows:rows,exportFlg:"true",expCols:expCols};
if(postData)
{
postData.page=1;
data=$.extend(true,{},postData,data);
}
$.ajax({
url : url,
success:function(downUrl){
var path=ctx+"/static-content?download=true&contentPath="+downUrl.url;
window.location.href=path;
},
type:"POST",
cache:false,
data:data,
beforeSend:function(){
$("").appendTo(document.body) ;
},
complete:function(){ $("#loading_img").remove();}
});
}
当然服务端的数据加载方法要做aop拦截,当知道这是导出请求时做导出动作,请看aop类
package org.hzjun.hlt.aop;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.hzjun.hlt.security.ShiroDbRealm.ShiroUser;
import org.hzjun.hlt.utils.DateUtil;
import org.hzjun.hlt.utils.PropertiesUtils;
import org.hzjun.hlt.web.LtSecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* 基于注解的AOP日志示例 ,顺便封装导出功能
*
* @author
*/
@Component
@Aspect
public class AopLog {
private static Loggerlogger = LoggerFactory.getLogger( AopLog.class );
// 方法执行的前后调用
@SuppressWarnings("unchecked")
public Object runOnAround(ProceedingJoinPoint point) throws Throwable {
long start = System.currentTimeMillis();
Object object = point.proceed();
ShiroUser su = LtSecurityUtils.getShiroUser();
String str = "";
if (point.getArgs().length > 0) {
Object[] args = point.getArgs();
for (Object o : args)
if (o instanceof HttpServletRequest)
// if (point.getArgs()[0] instanceof HttpServletRequest)
{
HttpServletRequest request = (HttpServletRequest) o;
String export = request.getParameter( "exportFlg" );
String expCols = request.getParameter( "expCols" );
if (StringUtils.isNotBlank( export )) {// 客户端掉的导出。。。。
if (StringUtils.isBlank( expCols )) {
} else if (object instanceof Map) {
Map
List list = (List) map.get( "rows" );
List> rowList = new ArrayList
>();
// 解析要导出的列
String[] cols = StringUtils.split( expCols, "," );
List
至于有时候我们客户没钱买报表系统,但是又要报表,我们可能只能自己画页面来生成,
这时我们只要把页面搞出来,后面的导出excel其实也可以直接搞定,不用再写代码。