利用Struts下载Excel文件(JXL)

一、导入jxls的jar包

二、配置Struts的xml文件


	
		
			application/text/plain
		
		
			attachment;filename=${khxxExcelFileName}
		
		khxxExcel
	

三、配置模板Excel

利用Struts下载Excel文件(JXL)_第1张图片

四、写代码传数据

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> 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了。

你可能感兴趣的:(#,struts,#,JXL)