您的位置:首页 > 其它

jxls导出excel

2015-12-14 00:00 381 查看
JxlsUtils.java:

/**
*
*/
package com.linewell.common;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import javax.servlet.ServletContext;

import net.sf.jxls.transformer.XLSTransformer;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

/**
* jxls 生成excel并的导出的工具类
* @author cjianquan
* 2015-1-29
*/
public class JxlsUtils {

public static void exportExcel(String templateFile,Map<String,Object> beans,String destFileName){
XLSTransformer xlsTransformer = new XLSTransformer();
ServletContext sc = (ServletContext) ActionContext.getContext().get(ServletActionContext.SERVLET_CONTEXT);
String path = sc.getRealPath("/");
templateFile = path+"\\"+templateFile;
destFileName = path+"\\"+destFileName;
try{
xlsTransformer.transformXLS(templateFile, beans,destFileName);
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException("导出excel错误");
}
}

}


调用上面的方法进行生成excel:

/**
* 导出所选记录(装备)
* @param request
* @param response
* @author cjianquan
* 2015-1-29
*/
@RequestMapping(params = "exportZbxx")
public void exportZbxx (HttpServletRequest request,HttpServletResponse response){

String[] ids = request.getParameterValues("ids[]");
String conIds = "";
for(String s:ids){
conIds+="'"+s+"',";
}
conIds = conIds.substring(0,conIds.length()-1);
StringBuilder querySql = new StringBuilder("select ZHUANGBEI_ZBMC,");
querySql.append("ZHUANGBEI_ZBBH,(select zblx_mc  from TBL_BASE_ZBLX a where a.zblx_unid=t.ZHUANGBEI_ZBLX) ZHUANGBEI_ZBLX,")
.append("ZHUANGBEI_SSDWMC,")
.append("ZHUANGBEI_ZBSJ,")
.append("(select typename from t_s_type  where typegroupid =(select id  from t_s_typegroup where typegroupcode='xxcg_jczb_zwzt') and typecode=t.ZHUANGBEI_ZBZT) ZHUANGBEI_ZWZT,")
.append("ZHUANGBEI_SCCJ,")
.append("(select typename from t_s_type  where typegroupid =(select id  from t_s_typegroup where typegroupcode='xxcg_jczb_zlzk') and typecode=t.ZHUANGBEI_ZBZT) ZHUANGBEI_ZLZK,")
.append("(select typename from t_s_type  where typegroupid =(select id  from t_s_typegroup where typegroupcode='xxlb_zbgl_zbzt') and typecode=t.ZHUANGBEI_ZBZT) ZHUANGBEI_ZBZT")
.append(" from TBL_BASE_ZHUANGBEI t where ZHUANGBEI_UNID in ("+conIds+")");
List<Map<String,Object>> zbxxList = this.systemService.findForJdbc(querySql.toString());

Map<String,Object> exportBeans = new HashMap<String, Object>();
String templateFile = "export/xlsTemplates/zbxxExportTemplate.xls";
String destFileName = "export/output/zbxx.xls";
exportBeans.put("resultList", zbxxList);
JxlsUtils.exportExcel(templateFile, exportBeans, destFileName);

}


js导出Excel:

//导出所选记录
//add by cjianquan on 2015-1-29
function exportXls(){
var ids = [];
var rows = $('#zbxxList').datagrid('getSelections');
if(rows.length<=0){
alert("请选择要导出的记录!");
return false;
}
for(var i=0; i<rows.length; i++){
ids.push(rows[i].id);
}
//ids.join(',');
$.ajax({
type:'POST',
async:false,
data:{"ids":ids},
url:'./zbxxController.do?exportZbxx',
success:function(data){
window.location.href='export/output/zbxx.xls';
},
error:function(XMLHttpRequest,textStatus,errorThrown){
alert("导出Excel失败!");
}
});
}


zbxxExportTemplate.xls模板文件内容如下:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: