zoukankan      html  css  js  c++  java
  • 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();
        }
    }
    
  • 相关阅读:
    matlab图像对比度增强,拉伸和灰度变换
    javascript 数据结构----集合
    reduce ,redceRight 在react中的使用
    找前端私活
    数组对象 按某个属性排序
    原生js获取scrollTop
    react 绑定事件 总是默认触发
    横向柱状图
    css 四角辉光
    Android学习笔记_点九绘图与软键盘和事件传递
  • 原文地址:https://www.cnblogs.com/ghostwolf1/p/14000242.html
Copyright © 2011-2022 走看看