JXLS模板导出多个sheet文件

时间:2024-01-31 13:10:30
/**
     * 宋发准:这个方法主要用于根据模板导出多个sheet的excel文件.
     *
     * @param inputStream:输入流:用于输入模板
     * @param response:用于下载
     * @param filename:文件名
     * @param sheetNamelist:sheet名称,注意:list的size决定了sheet的个数.
     * @param objects:用于输出的文件,是一个list<sheet>的数据,一个sheet代表一个页面.
     * @param tagname:用于页面取值的标记
     */
    public void buildDownloadExcelByMultiSheet(InputStream inputStream, HttpServletResponse response, String filename, List<String> sheetNamelist, ArrayList<SheetBean> objects, String tagname) {
        OutputStream out = null;

        try {
            response.setContentType(HttpUtils.getContentType(filename));
            response.setHeader("Content-Disposition", HttpUtils.getDisposition(filename, false));
            response.setHeader("Pragma", "public");
            response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
            response.setHeader("Cache-Control", "public");
            response.setHeader("Expires", "0");
            response.setHeader("Content-Transfer-Encoding", "binary");
            XLSTransformer transformer = new XLSTransformer();
            Workbook workbook = transformer.transformMultipleSheetsList(inputStream, objects, sheetNamelist, tagname, new HashMap(), 0);
            out = response.getOutputStream();
            workbook.write(out);
        } catch (IOException e) {
            e.printStackTrace();
            _log.error(e);
        } catch (InvalidFormatException e) {
            e.printStackTrace();
            _log.error(e);
        } finally {
            try {
                if (out != null)
                    out.close();
                out = null;
            } catch (IOException e) {
                e.printStackTrace();

            }
        }
    }