zoukankan      html  css  js  c++  java
  • poi导出excel

    package com.daat.manager.base.web.tools;
    
    import java.io.FileOutputStream;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFCellStyle;
    import org.apache.poi.hssf.usermodel.HSSFFont;
    import org.apache.poi.hssf.usermodel.HSSFRichTextString;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.hssf.util.HSSFColor;
    import org.apache.poi.ss.util.CellRangeAddress;
    
    public class ExcelUtils {
    
        /**
         * @param exclPath
         *            文件生成的路径
         * @param exclName
         *            文件名称
         * @param title
         *            文件的TITLE
         * @param data
         *            数据文件
         * @param columnName
         *            列名 对应map中key 用来从数据map中取数据用
         * @param columnTitle
         *            列的title 展示用
         * @return 成功返回文件完成路径+名字 异常返回null
         */
        public static String createExcel(String exclPath, String exclName,
                String title, List<String> columnTitle, List<String> columnName,
                List<Map> data, String remark) {
            // 校验参数
            if (exclPath == null) {
                return null;
            }
            if (exclName == null) {
                return null;
            }
            if (title == null) {
                return null;
            }
            if (data == null) {
                return null;
            }
            if (columnName == null) {
                return null;
            }
            if (columnTitle == null) {
                return null;
            }
            HSSFWorkbook workbook = new HSSFWorkbook();// 创建一个Excel文件
            HSSFSheet sheet = workbook.createSheet();// 创建一个Excel的Sheet
            // sheet.createFreezePane(1, 3);// 冻结
            // 设置列宽
            for (int i = 0; i < columnTitle.size(); i++) {
                sheet.setColumnWidth(i, 6000);
            }
            // Sheet样式
            HSSFCellStyle sheetStyle = workbook.createCellStyle();
            // 背景色的设定
            sheetStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);
            // 前景色的设定
            sheetStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
            // 填充模式
            sheetStyle.setFillPattern(HSSFCellStyle.FINE_DOTS);
            // 设置列的样式
            for (int i = 0; i <= columnTitle.size() * 2; i++) {
                sheet.setDefaultColumnStyle((short) i, sheetStyle);
            }
            // 设置字体
            HSSFFont headfont = workbook.createFont();
            headfont.setFontName("黑体");
            headfont.setFontHeightInPoints((short) 22);// 字体大小
            headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗
            // 另一个样式
            HSSFCellStyle headstyle = workbook.createCellStyle();
            headstyle.setFont(headfont);
            headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
            headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
            headstyle.setLocked(true);
            headstyle.setWrapText(true);// 自动换行
            // 另一个字体样式
            HSSFFont columnHeadFont = workbook.createFont();
            columnHeadFont.setFontName("宋体");
            columnHeadFont.setFontHeightInPoints((short) 10);
            columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            // 列头的样式
            HSSFCellStyle columnHeadStyle = workbook.createCellStyle();
            columnHeadStyle.setFont(columnHeadFont);
            columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
            columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
            columnHeadStyle.setLocked(true);
            columnHeadStyle.setWrapText(true);
            columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);// 左边框的颜色
            columnHeadStyle.setBorderLeft((short) 1);// 边框的大小
            columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);// 右边框的颜色
            columnHeadStyle.setBorderRight((short) 1);// 边框的大小
            columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
            columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色
            // 设置单元格的背景颜色(单元格的样式会覆盖列或行的样式)
            columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);
    
            HSSFFont font = workbook.createFont();
            font.setFontName("宋体");
            font.setFontHeightInPoints((short) 10);
            // 普通单元格样式
            HSSFCellStyle style = workbook.createCellStyle();
            style.setFont(font);
            style.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中
            style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);// 上下居中
            style.setWrapText(true);
            style.setLeftBorderColor(HSSFColor.BLACK.index);
            style.setBorderLeft((short) 1);
            style.setRightBorderColor(HSSFColor.BLACK.index);
            style.setBorderRight((short) 1);
            style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
            style.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.
            style.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色.
            // 另一个样式
            HSSFCellStyle centerstyle = workbook.createCellStyle();
            centerstyle.setFont(font);
            centerstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
            centerstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
            centerstyle.setWrapText(true);
            centerstyle.setLeftBorderColor(HSSFColor.BLACK.index);
            centerstyle.setBorderLeft((short) 1);
            centerstyle.setRightBorderColor(HSSFColor.BLACK.index);
            centerstyle.setBorderRight((short) 1);
            centerstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
            centerstyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.
            centerstyle.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色.
    
            try {
                // 创建第一行
                HSSFRow row0 = sheet.createRow(0);
                // 设置行高
                row0.setHeight((short) 900);
                // 创建第一列
                HSSFCell cell0 = row0.createCell(0);
                // 第一列设置Title
                cell0.setCellValue(new HSSFRichTextString(title));
                cell0.setCellStyle(headstyle);
                /**
                 * 合并单元格 第一个参数:第一个单元格的行数(从0开始) 第二个参数:第二个单元格的行数(从0开始)
                 * 第三个参数:第一个单元格的列数(从0开始) 第四个参数:第二个单元格的列数(从0开始)
                 */
                CellRangeAddress range = new CellRangeAddress(0, 0, 0,
                        columnName.size() - 1);
                sheet.addMergedRegion(range);
                // 创建第二行
                HSSFRow row1 = sheet.createRow(1);
                HSSFCell cell1 = row1.createCell(0);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                cell1.setCellValue(new HSSFRichTextString("导出数据:数据时间:"
                        + sdf.format(new Date()) + "" + remark));
                cell1.setCellStyle(centerstyle);
                // 合并单元格
                range = new CellRangeAddress(1, 2, 0, columnName.size() - 1);
                sheet.addMergedRegion(range);
                // 第三行
                HSSFRow row2 = sheet.createRow(3);
                row2.setHeight((short) 750);
                HSSFCell cell = row2.createCell(0);
    
                for (int i = 0; i < columnTitle.size(); i++) {
                    cell.setCellValue(new HSSFRichTextString(columnTitle.get(i)));
                    cell.setCellStyle(columnHeadStyle);
                    cell = row2.createCell(i + 1);
                }
    
                // 得到数据集
                int m = 4;
                for (int i = 0; i < data.size(); i++) {
                    Map element = data.get(i);
                    HSSFRow row = sheet.createRow(m + i);
                    cell = row.createCell(0);
                    cell.setCellValue(new HSSFRichTextString(""));
                    cell.setCellStyle(centerstyle);
                    // 合并单元格
    
                    for (int j = 0; j < columnName.size(); j++) {
                        String clKey = columnName.get(j);
                        cell = row.createCell(j);
                        
                        Object obj = element.get(clKey);// 加入了判空 用""代替null
                        if (obj == null) {
                            obj = "";
                        }
                        cell.setCellValue(obj.toString());
                        cell.setCellStyle(centerstyle);
                    }
                }
    
                // 列尾
                int footRownumber = sheet.getLastRowNum();
                HSSFRow footRow = sheet.createRow(footRownumber + 1);
                HSSFCell footRowcell = footRow.createCell(0);
                footRowcell.setCellValue(new HSSFRichTextString(
                        "                    审  定:XXX      审  核:XXX     汇  总:XX"));
                footRowcell.setCellStyle(centerstyle);
                range = new CellRangeAddress(footRownumber + 1, footRownumber + 1,
                        0, columnName.size() - 1);
                sheet.addMergedRegion(range);
    
                String filename = exclPath + "/" + exclName;// 设置下载时客户端Excel的名称
                FileOutputStream fileOut = new FileOutputStream(filename);
                workbook.write(fileOut);
                fileOut.flush();
                fileOut.close();
                return exclPath + "/" + exclName;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    
        public static void main(String[] args) {
    
            ExcelUtils el = new ExcelUtils();
            String exclPath = "d://";
            String exclName = "testAAA.xls";
            String title = "我的测试";
            List columnTitle = new ArrayList();
            columnTitle.add("第一列");
            columnTitle.add("第二列");
            columnTitle.add("第三列");
            columnTitle.add("第四列");
            columnTitle.add("第五列");
            List columnName = new ArrayList();
            columnName.add("name");
            columnName.add("age");
            columnName.add("bb");
            columnName.add("cc");
            columnName.add("dd");
            List data = new ArrayList();
            Map tem1 = new HashMap();
            tem1.put("name", "张1");
            tem1.put("age", "age1");
            tem1.put("bb", "bb1");
            tem1.put("cc", "cc1");
            tem1.put("dd", "dd1");
            data.add(tem1);
    
            Map tem2 = new HashMap();
            tem2.put("name", "张2");
            tem2.put("age", "age2");
            tem2.put("bb", "bb2");
            tem2.put("cc", "cc2");
            tem2.put("dd", "dd1");
            data.add(tem2);
    
            Map tem3 = new HashMap();
            tem3.put("name", "张3");
            tem3.put("age", "age3");
            tem3.put("bb", "bb3");
            tem3.put("cc", "cc3");
            tem3.put("dd", "dd1");
            data.add(tem3);
    
            Map tem4 = new HashMap();
            tem4.put("name", "张3");
            tem4.put("age", "age3");
            tem4.put("bb", "bb3");
            tem4.put("cc", "cc3");
            tem4.put("dd", "dd1");
            data.add(tem4);
    
            el.createExcel(exclPath, exclName, title, columnTitle, columnName,
                    data, "");
        }
    }
  • 相关阅读:
    BZOJ3105: [cqoi2013]新Nim游戏 博弈论+线性基
    BZOJ3759: Hungergame 博弈论+线性基
    NOI模拟赛Day2
    期望dp BZOJ3450+BZOJ4318
    NOI模拟赛 Day1
    NOI模拟 热身赛T1
    【BZOJ4260】 Codechef REBXOR 可持久化Trie
    【BZOJ3673】&&【BZOJ3674】: 可持久化并查集 by zky 可持久化线段树
    【BZOJ3207】花神的嘲讽计划I 可持久化线段树/莫队
    【bzoj3527】[Zjoi2014]力 FFT
  • 原文地址:https://www.cnblogs.com/fxwl/p/6070458.html
Copyright © 2011-2022 走看看