jxls导出excel

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 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> zbxxList = this.systemService.findForJdbc(querySql.toString());
		
		Map exportBeans = new HashMap();
		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

zbxxExportTemplate.xls模板文件内容如下:



你可能感兴趣的:(java)