`

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

    博客分类:
  • Java
阅读更多
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####################//
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics