java 导出Excel文件

Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能。

今天整合了下代码,就POI导出Excel写成功了一个工具类

public class PoiExcelUtil {

    public static void export(String title, List<String> keyList, List<String> fieldList, List<Map<String, Object>> dataList,
                              HttpServletResponse response, String fileName) throws Exception {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet(fileName);

        CellStyle style1 = workbook.createCellStyle();
        CellStyle style3 = workbook.createCellStyle();
        CellStyle style2 = workbook.createCellStyle();

        style1.setAlignment(CellStyle.ALIGN_CENTER);
        style1.setVerticalAlignment(CellStyle.VERTICAL_CENTER);

        style2.setAlignment(CellStyle.ALIGN_CENTER);
        style2.setBorderBottom(CellStyle.BORDER_THIN);
        style2.setBorderTop(CellStyle.BORDER_THIN);
        style2.setBorderLeft(CellStyle.BORDER_THIN);
        style2.setBorderRight(CellStyle.BORDER_THIN);
        style2.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        style2.setWrapText(true);

        style3.setAlignment(CellStyle.ALIGN_CENTER);
        style3.setBorderBottom(CellStyle.BORDER_THIN);
        style3.setBorderTop(CellStyle.BORDER_THIN);
        style3.setBorderLeft(CellStyle.BORDER_THIN);
        style3.setBorderRight(CellStyle.BORDER_THIN);
        style3.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        style3.setDataFormat(workbook.createDataFormat().getFormat("@"));
        style3.setWrapText(true);

        Font font1 = workbook.createFont();
        Font font2 = workbook.createFont();
        Font font3 = workbook.createFont();

        font1.setFontHeightInPoints((short) 18);
        font1.setFontName("宋体");
        font1.setBoldweight(Font.BOLDWEIGHT_BOLD);

        font2.setFontHeightInPoints((short) 11);
        font2.setFontName("宋体");
        font2.setBoldweight(Font.BOLDWEIGHT_BOLD);

        font3.setFontHeightInPoints((short) 10);
        font3.setFontName("宋体");

        style1.setFont(font1);
        style2.setFont(font2);
        style3.setFont(font3);

        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0, Cell.CELL_TYPE_STRING);
        cell.setCellStyle(style1);
        cell.setCellValue(title);

        CellRangeAddress region = new CellRangeAddress(0, 0, 0, fieldList.size());
        RegionUtil.setBorderBottom(CellStyle.BORDER_THIN, region, sheet,
                workbook);
        RegionUtil
                .setBorderLeft(CellStyle.BORDER_THIN, region, sheet, workbook);
        RegionUtil.setBorderRight(CellStyle.BORDER_THIN, region, sheet,
                workbook);
        RegionUtil.setBorderTop(CellStyle.BORDER_THIN, region, sheet, workbook);
        sheet.addMergedRegion(region);

        for (int i = 0; i < fieldList.size(); i++) {
            if (i == 0) {
                sheet.setColumnWidth(i, 30 * 256);
            } else {
                sheet.setColumnWidth(i, 20 * 256);
            }
        }

        row = sheet.createRow(1);
        for (int i = 0; i < fieldList.size(); i++) {
            cell = row.createCell(i, Cell.CELL_TYPE_STRING);

            cell.setCellStyle(style2);
            cell.setCellValue(fieldList.get(i));
        }

        for (int i = 0; i < dataList.size(); i++) {
            row = sheet.createRow(i + 2);
            Map<String, Object> dataMap = dataList.get(i);

            for (int j = 0; j < keyList.size(); j++) {
                cell = row.createCell(j, Cell.CELL_TYPE_STRING);
                cell.setCellValue(MapUtils.getString(dataMap, keyList.get(j), "-"));
                cell.setCellStyle(style3);
            }
        }

        response.setContentType("application/octet-stream");
        response.addHeader("Content-Disposition", "attachment; filename =" + URLEncoder.encode(fileName + ".xlsx", "utf-8"));
        OutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        outputStream.close();
    }
}
posted @ 2020-11-18 15:44  山上的人  阅读(315)  评论(0编辑  收藏  举报