利用Struts下载Excel文件(JXL)

一、导入jxls的jar包

二、配置Struts的xml文件

<action name="exportKhxxExportAction" class="com.crm.action.KhxxExportAction" >
	<result name="success" type="stream">
		<param name="contentType">
			application/text/plain
		</param>
		<param name="contentDisposition">
			attachment;filename=${khxxExcelFileName}
		</param>
		<param name="inputName">khxxExcel</param>
	</result>
</action>

三、配置模板Excel

四、写代码传数据

public class KhxxExportAction extends BaseStruts2Action {

	private static final long serialVersionUID = 1L;
	private final static Log log = LogFactory.getLog(KhxxExportAction.class);

	public InputStream getKhxxExcel() {

		String xlsTemplateFileName = getSession().getServletContext()
				.getRealPath("/resources/khxx/khxx.xls");
		String tempFileName = getSession().getServletContext().getRealPath(
				"/resources/khxx/" + System.currentTimeMillis());
		Map beans = new HashMap();
		Map report = new HashMap();
		List rows = new ArrayList();
		List<Map<String, Object>> datas = null;
		
		String sql = "SELECT * from jpas_jznx_khexport_temp where 1=1 ";
		try {
			datas = JdbcUtils.queryToMapList(sql);

			if (datas != null && datas.size() != 0) {
				rows.addAll(datas);
				report.put("rows", rows);
				beans.put("report", report);
				XLSTransformer transformer = new XLSTransformer();
				transformer.transformXLS(xlsTemplateFileName, beans,
						tempFileName);
				InputStream io = new FileInputStream(tempFileName);
				return io;
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
		return null;
	}

	public String getKhxxExcelFileName()
			throws UnsupportedEncodingException, SQLException {

		String filename = new String(("客户信息表.xls").getBytes(), "ISO8859-1");
		return filename;
	}

}

这样就OK了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值