导出excel方式1

 
String[] columnNames = {"产品名称", "产品内部编码", "产品状态","基金管理人", "管理人代码", "引入机构", "服务人员", "引入人员", "成立日期", "关系确认状态","考核机构","考核人员"};
String fileName = "产品托管关系" + DateUtil.getYYMMDDHHMMSS();
exportExcel(fileName, fileName, columnNames, List<T>, response, "2003");
/**
     * @param fileName 表格名称
     * @param title    表格名称
     * @param headers  第一行标题
     * @param dataset  数据列表List<List<String>
     * @param req
     * @param response
     */
    public void exportExcel(String fileName, String title, String[] headers, Collection<T> dataset,
                            HttpServletRequest req, HttpServletResponse response) {
        try {
            response.setContentType("application/vnd.ms-excel");
            String userAgent = req.getHeader("User-Agent").toLowerCase();
            if (userAgent.contains("msie") || userAgent.contains("trident")) {
                response.addHeader("Content-Disposition",
                        "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls");
            } else {
                // 非IE内核浏览器的处理:
                response.addHeader("Content-Disposition",
                        "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1") + ".xls");
            }
            // 声明一个工作薄
            XSSFWorkbook workbook = new XSSFWorkbook();
            // 生成一个表格
            XSSFSheet sheet = workbook.createSheet(title);
            // 设置表格默认列宽度为15个字节
            sheet.setDefaultColumnWidth(20);
            // 生成一个样式
            CellStyle style = getCellStyleOfTitle(workbook);
            // 产生表格标题行
            XSSFRow row = sheet.createRow(0);
            XSSFCell cellHeader;
            for (int i = 0; i < headers.length; i++) {
                cellHeader = row.createCell(i);
                cellHeader.setCellStyle(style);
                cellHeader.setCellValue(new XSSFRichTextString(headers[i]));
            }
            // 遍历集合数据,产生数据行
            Iterator<T> it = dataset.iterator();
            int index = 0;
            try {
                while (it.hasNext()) {
                    index = ++index;
                    List<String> nextList = (List<String>) it.next();
                    XSSFRow row2 = sheet.createRow(index);
                    for (int i = 0; i < nextList.size(); i++) {
                        XSSFCell cell = row2.createCell(i);
                        XSSFCellStyle cellStyle = cell.getCellStyle();
                        cellStyle.setAlignment(HorizontalAlignment.CENTER);
                        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
                        cell.setCellValue(new XSSFRichTextString(nextList.get(i) == null ? "" : nextList.get(i)));
                    }
                }
                workbook.write(response.getOutputStream());
            } catch (Exception e) {
                LOGGER.error("导出异常:", e);
                throw new GException("导出异常,请重试");
            } finally {
                try {
                    if (response != null) {
                        response.getOutputStream().flush();
                        response.getOutputStream().close();
                    }
                    workbook.close();
                } catch (IOException e) {
                    LOGGER.error("资源关闭异常:", e);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

posted @ 2022-06-08 17:45  老王的日常  阅读(21)  评论(0)    收藏  举报