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<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模板文件内容如下:



转载于:https://my.oschina.net/u/2552902/blog/543861

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值