poi 3.17 导出excel

package Utils;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
/**
 * cellstyle工具类  poi版本3.17
 * auth lancx
 * since 2019-9-10
 */
public class CellStyleTool {

    /**
     * 添加边框
     * @param cellStyle
     * @return
     */
    public static CellStyle addBorder(CellStyle cellStyle){
        cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
        cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
        cellStyle.setBorderTop(BorderStyle.THIN);//上边框
        cellStyle.setBorderRight(BorderStyle.THIN);//右边框
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        return cellStyle;
    }

    /**
     *
     * @param cellStyle
     * @param workbook
     * @param color   参数可选择常量值 eg:XSSFFont.COLOR_RED
     * @param isBold
     * @param fontSize
     * @param fontName
     * @return
     */
    public static CellStyle setFont(CellStyle cellStyle, XSSFWorkbook workbook, Short color,Boolean isBold,int fontSize, String fontName){
        XSSFFont font = workbook.createFont();
        font.setColor(color);
        font.setBold(isBold);
        font.setFontHeightInPoints((short)fontSize);
        font.setFontName(fontName);
        cellStyle.setFont(font);
        return cellStyle;
    }
    /**
     * 设置居中
     * @param cellStyle
     * @return
     */
    public static CellStyle setAlignment(CellStyle cellStyle){
        cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        return cellStyle;
    }

    /**
     * 设置背景颜色
     * @param cellStyle
     * @param IndexedColors
     * @param fillPatternType
     * @return
     */
    public static CellStyle setBackgroundColor(CellStyle cellStyle, Short IndexedColors, FillPatternType fillPatternType){
        cellStyle.setFillForegroundColor(IndexedColors);
        cellStyle.setFillPattern(fillPatternType);
        return cellStyle;
    }

    /**
     * 自动换行   内容中加入\r\n
     * 最后一个换行符使用String.substring(0, sbContent.toString().length() - "\r\n".length() 去掉
     * @param cellStyle
     * @return
     */
    public static CellStyle autoNewLine (CellStyle cellStyle){
        //自动换行
        cellStyle.setWrapText(true);
        return cellStyle;
    }
    //未完待续。。。
}

导出excel Demo

package Utils;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class ExportExcel1 {

    //插入数据返回对象
    public XSSFWorkbook insertData(String[] titles, String[] rowName, ArrayList<Object[]> dataList) {
        //创建工作薄对象
        XSSFWorkbook workbook = new XSSFWorkbook();//这里也可以设置sheet的Name
        //创建工作表对象
        XSSFSheet sheet = workbook.createSheet();
        /**********标题、任务行*********/
        //设置标题、任务样式
        CellStyle titleCellStyle = workbook.createCellStyle();
        titleCellStyle = CellStyleTool.addBorder(titleCellStyle);
        titleCellStyle = CellStyleTool.setAlignment(titleCellStyle);
        titleCellStyle = CellStyleTool.setFont(titleCellStyle, workbook,null,false ,18, "等线");
        CellStyle taskNameCellStyle = workbook.createCellStyle();
        taskNameCellStyle = CellStyleTool.addBorder(taskNameCellStyle);
        taskNameCellStyle = CellStyleTool.setAlignment(taskNameCellStyle);
        taskNameCellStyle = CellStyleTool.setFont(taskNameCellStyle, workbook,null,false , 14, "等线");
        //创建行
        XSSFRow titleRow = sheet.createRow(0);
        XSSFRow taskNameRow = sheet.createRow(1);
        //设置标题行高颜色
        titleRow.setHeightInPoints(24.0f);
        taskNameRow.setHeightInPoints(18.0f);
        //合并单元格
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, rowName.length - 1));
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, rowName.length - 1));
        for (int t = 0; t < rowName.length; t++) {
            Cell title1Cell = titleRow.createCell(t);
            Cell title2Cell = taskNameRow.createCell(t);
            // 设置标题样式
            title1Cell.setCellStyle(titleCellStyle);
            title2Cell.setCellStyle(taskNameCellStyle);
            if (t == 0) {
                title1Cell.setCellValue(titles[0]);
                title2Cell.setCellValue(titles[1]);
            }
        }


        /********** 列标题行********/
        //新建样式
        CellStyle colNameCellStyle = workbook.createCellStyle();
        colNameCellStyle = CellStyleTool.addBorder(colNameCellStyle);
        colNameCellStyle = CellStyleTool.setAlignment(colNameCellStyle);
        colNameCellStyle = CellStyleTool.setBackgroundColor(colNameCellStyle, IndexedColors.GREY_25_PERCENT.getIndex(), FillPatternType.SOLID_FOREGROUND);
        colNameCellStyle = CellStyleTool.setAlignment(colNameCellStyle);
        colNameCellStyle = CellStyleTool.setFont(colNameCellStyle, workbook, (short)0,false,11, "等线");

        //创建每列标题
        XSSFRow colNameRow = sheet.createRow(2);
        colNameRow.setHeightInPoints(14.0f);
        for (int r = 0; r < rowName.length; r++) {
            //创建单元格填充数据
            colNameRow.createCell(r).setCellValue(rowName[r]);
            //设置单元格样式
            colNameRow.getCell(r).setCellStyle(colNameCellStyle);
            //设置宽度
            sheet.setColumnWidth(r, 40 * 100);
            if (r == rowName.length - 1) {
                sheet.setColumnWidth(r, 160 * 100);
            }
            if (r == (rowName.length - 2)) {
                sheet.setColumnWidth(r, 100 * 100);
            }
        }
        /************数据行*************/
        //新建样式
        CellStyle dataCellStyle = workbook.createCellStyle();
        dataCellStyle = CellStyleTool.addBorder(dataCellStyle);
        //获取所有数据行中数据最多的一行记录最大值
        int maxLength = 0;
        for (int m =0 ;m<dataList.size();m++){
            Object[] tempArry = dataList.get(m);
            if (maxLength < tempArry.length) {
                maxLength = tempArry.length;
            }
        }
        //填充数据
        for (int i = 0; i < dataList.size(); i++) {
            Object[] objArry = dataList.get(i);//遍历每个对象
            XSSFRow dataRow = sheet.createRow(i + 3);
            dataRow.setHeightInPoints(28.0f);
            //创建表格
            for (int j = 0; j < maxLength; j++) {
                Cell datacell = dataRow.createCell(j);
                datacell.setCellStyle(dataCellStyle);
            }
            for (int k = 0;k<objArry.length;k++){
                dataRow.getCell(k).setCellValue(objArry[k].toString());
            }

        }
        return workbook;
    }

    //导出数据
    public void Export(XSSFWorkbook workbook) {
        //文档输出
        FileOutputStream out = null;
        try {
            out = new FileOutputStream("D:/temp" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()).toString() + ".xlsx");
            workbook.write(out);
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


你可能感兴趣的:(填坑)