六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 30|回复: 0

使用Jxls操作Excel的方式,针对excel中的数据为List/Map,或非List组合的操作

[复制链接]

升级  90.67%

56

主题

56

主题

56

主题

秀才

Rank: 2

积分
186
 楼主| 发表于 2013-2-3 11:18:58 | 显示全部楼层 |阅读模式
Jxls操作excel的时候,数据里面同时有规则的List/Map数据,同时又有不规则的,零星组织的数据,在这里,统统可以用此方法解决,具体方法如下,包括完整的类文件,导出Excel的时候需要有一个模板,模板中的各项数据请使用EL表达式的方式${list.userName...}诸如此类方式即可写入值...
//#################start##################//public class JxlsResult extends StrutsResultSupport {    /**     *      */    private static final long serialVersionUID = -9095565761135848980L;    private String fileName;    private String root;    public JxlsResult() {        super();    }    public JxlsResult(String location) {        super(location);    }    @SuppressWarnings("unchecked")    protected void doExecute(String location, ActionInvocation invocation)            throws Exception {        HttpServletResponse response = (HttpServletResponse) invocation                .getInvocationContext().get(StrutsStatics.HTTP_RESPONSE);        ServletContext servletContext = (ServletContext) invocation                .getInvocationContext().get(StrutsStatics.SERVLET_CONTEXT);        InputStream template = new FileInputStream(servletContext                .getRealPath(location));        Map beans = getBeans(invocation);        String fileName = getFileName();        ByteArrayOutputStream out = new ByteArrayOutputStream();        Workbook workbook = new XLSTransformer().transformXLS(template, beans);        workbook.write(out);        byte[] result = out.toByteArray();        fileName = fileName == null ? invocation.getProxy().getActionName()                : conditionalParse(fileName, invocation) + ".xls";        response.setContentType("application/vnd.ms-excel");        response.setContentLength(result.length);        response.setHeader("Content-Disposition", "attachment; filename=\""                + fileName + "\"");        response.getOutputStream().write(result);    }    @SuppressWarnings("unchecked")    private Map getBeans(ActionInvocation invocation) {        Object rootObj = root == null ? invocation.getAction() : invocation                .getStack().findValue(root);        return new BeanMap(rootObj);    }    private String getFileName() {        return new StringBuffer()                .append(fileName == null ? "report" : fileName).append(".xls")                .toString();    }    public void setFileName(String fileName) {        this.fileName = fileName;    }    public void setRoot(String root) {        this.root = root;    }}//#################end ##################//
以下为第二种方法//#################start##################///**     * export Excel_2     *      * @author Lgscofield     */    @SuppressWarnings("unchecked")    public void exportExcel() {        try {            listBuildLandOwnership = (List<BuildLandOwnershipVO>) ServletActionContext.getRequest()                    .getSession().getAttribute("listBuildLandOwnership");            BuildLandOwnershipVO blVO = (BuildLandOwnershipVO) ServletActionContext.getRequest()                    .getSession().getAttribute("blVO");            List<BuildLandOwnershipVO> header = new ArrayList<BuildLandOwnershipVO>();            if (blVO != null) {                blVO.setHmj(blVO.getHmj() + "");                blVO.setGmj(blVO.getGmj() + "");                blVO.setHmj(blVO.getHmj() + "");                blVO.setNydHj(blVO.getNydHj() + "");                blVO.setNydHjg(blVO.getNydHjg() + "");                blVO.setNydHjj(blVO.getNydHjj() + "");                blVO.setMj01(blVO.getMj01() + "");                blVO.setMj01g(blVO.getMj01g() + "");                blVO.setMj01j(blVO.getMj01j() + "");                blVO.setMj02(blVO.getMj02() + "");                blVO.setMj02g(blVO.getMj02g() + "");                blVO.setMj02j(blVO.getMj02j() + "");                blVO.setMj03(blVO.getMj03() + "");                blVO.setMj03g(blVO.getMj03g() + "");                blVO.setMj03j(blVO.getMj03j() + "");                blVO.setMj04(blVO.getMj04() + "");                blVO.setMj04g(blVO.getMj04g() + "");                blVO.setMj04j(blVO.getMj04j() + "");                blVO.setQtNyd(blVO.getQtNyd() + "");                blVO.setQtNydg(blVO.getQtNydg() + "");                blVO.setQtNydj(blVO.getQtNydj() + "");                blVO.setJmjgkyd(blVO.getJmjgkyd() + "");                blVO.setJmjgkydg(blVO.getJmjgkydg() + "");                blVO.setJmjgkydj(blVO.getJmjgkydj() + "");                blVO.setJtyd(blVO.getJtyd() + "");                blVO.setJtydg(blVO.getJtydg() + "");                blVO.setJtydj(blVO.getJtydj() + "");                blVO.setSlyd(blVO.getSlyd() + "");                blVO.setSlydg(blVO.getSlydg() + "");                blVO.setSlydj(blVO.getSlydj() + "");                blVO.setJsdHj(blVO.getJsdHj() + "");                blVO.setJsdHjg(blVO.getJsdHjg() + "");                blVO.setJsdHjj(blVO.getJsdHjj() + "");                blVO.setWlytd(blVO.getWlytd() + "");                blVO.setWlytdg(blVO.getWlytdg() + "");                blVO.setWlytdj(blVO.getWlytdj() + "");                blVO.setQtWlytd(blVO.getQtWlytd() + "");                blVO.setQtWlytdg(blVO.getQtWlytdg() + "");                blVO.setQtWlytdj(blVO.getQtWlytdj() + "");                blVO.setWlyHj(blVO.getWlyHj() + "");                blVO.setWlyHjg(blVO.getWlyHjg() + "");                blVO.setWlyHjj(blVO.getWlyHjj() + "");                header.add(blVO);            }            if (listBuildLandOwnership != null) {                String fileName = null;                try {                    fileName = URLEncoder.encode("土地利用现状权属审核(分析)表.xls", "utf-8");                } catch (UnsupportedEncodingException e1) {                    e1.printStackTrace();                }                Map beans = new HashMap();                beans.put("header", header);                beans.put("listBuildLandOwnership", listBuildLandOwnership);                ServletContext sc = (ServletContext) ActionContext.getContext().get(                        ServletActionContext.SERVLET_CONTEXT);                String location = sc.getRealPath("/export/BloAnalysis.xls");                ServletActionContext.getResponse()                        .setContentType("text/vnd.ms-excel;charset=UTF-8");                ServletActionContext.getResponse().setHeader("Content-Disposition",                        "attachment; filename=\"" + fileName + "\"");                InputStream template = new FileInputStream(location);                ByteArrayOutputStream out = new ByteArrayOutputStream();                Workbook workbook = new XLSTransformer().transformXLS(template, beans);                workbook.write(out);                byte[] result = out.toByteArray();                ServletActionContext.getResponse().setContentLength(result.length);                ServletActionContext.getResponse().getOutputStream().write(result);            }        } catch (Exception e) {            e.printStackTrace();        }    }//##################end####################//
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表