POI 边框样式BorderStyle

1. 单元格边框样式

单元格边框样式包含框线样式和边框颜色,可以对单元格的四边设置不同的样式
设置单元格样式:

style.setBorderBottom(BorderStyle.DOUBLE);
style.setBottomBorderColor(IndexedColors.BLUE.getIndex());
style.setBorderLeft(BorderStyle.THICK);
style.setLeftBorderColor(IndexedColors.GREEN.getIndex());
style.setBorderRight(BorderStyle.DASH_DOT);
style.setRightBorderColor(IndexedColors.RED.getIndex());
style.setBorderTop(BorderStyle.MEDIUM);
style.setTopBorderColor(IndexedColors.ORANGE.getIndex());

2. 单元格框线样式

org.apache.poi.ss.usermodel.BorderStyle

package org.apache.poi.ss.usermodel;

/**
 * The enumeration value indicating the line style of a border in a cell,
 * i.e., whether it is bordered dash dot, dash dot dot, dashed, dotted, double, hair, medium, 
 * medium dash dot, medium dash dot dot, medium dashed, none, slant dash dot, thick or thin.
 */
 public enum BorderStyle {

    /**
     * No border (default)
     */
    NONE(0x0),

    /**
     * Thin border
     */
    THIN(0x1),

    /**
     * Medium border
     */
    MEDIUM(0x2),

    /**
     * dash border
     */
    DASHED(0x3),

    /**
     * dot border
     */
    DOTTED(0x4),

    /**
     * Thick border
     */
    THICK(0x5),

    /**
     * double-line border
     */
    DOUBLE(0x6),

    /**
     * hair-line border
     */
    HAIR(0x7),

    /**
     * Medium dashed border
     */
    MEDIUM_DASHED(0x8),

    /**
     * dash-dot border
     */
    DASH_DOT(0x9),

    /**
     * medium dash-dot border
     */
    MEDIUM_DASH_DOT(0xA),

    /**
     * dash-dot-dot border
     */
    DASH_DOT_DOT(0xB),

    /**
     * medium dash-dot-dot border
     */
    MEDIUM_DASH_DOT_DOT(0xC),

    /**
     * slanted dash-dot border
     */
    SLANTED_DASH_DOT(0xD);

    private final short code;

    private BorderStyle(int code) {
        this.code = (short)code;
    }

    public short getCode() {
        return code;
    }

    private static final BorderStyle[] _table = new BorderStyle[0xD + 1];
    static {
        for (BorderStyle c : values()) {
            _table[c.getCode()] = c;
        }
    }

    public static BorderStyle valueOf(short code) {
        return _table[code];
    }
}

3. 单元格边框实例

package hssf.sheet.border;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.BorderExtent;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.PropertyTemplate;

public class ExportBorderStyle {
    public static void main(String[] args) throws Exception {
        File file = new File("C:\\Users\\Administrator\\Desktop\\test.xls");
        if (file.exists()) {
            file.delete();
        }
        BufferedOutputStream out = null;
        try {
            out = new BufferedOutputStream(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test.xls"));
            exportExcel(out);
        } finally {
            out.close();
        }
    }

    /**
     * @param out
     * @throws IOException
     */
    private static void exportExcel(BufferedOutputStream out) throws IOException {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        sheet.setColumnWidth(2, 20*256);

        Row row = sheet.createRow(2);
        row.setHeightInPoints(30);
        Cell cell = row.createCell(2);
        cell.setCellValue("单元格边框样式");

        CellStyle style = workbook.createCellStyle();
        style.setBorderBottom(BorderStyle.DOUBLE);
        style.setBottomBorderColor(IndexedColors.BLUE.getIndex());
        style.setBorderLeft(BorderStyle.THICK);
        style.setLeftBorderColor(IndexedColors.GREEN.getIndex());
        style.setBorderRight(BorderStyle.DASH_DOT);
        style.setRightBorderColor(IndexedColors.RED.getIndex());
        style.setBorderTop(BorderStyle.MEDIUM);
        style.setTopBorderColor(IndexedColors.ORANGE.getIndex());
        cell.setCellStyle(style);
        workbook.write(out);
    }
}

POI 边框样式BorderStyle_第1张图片

4. 合并单元格边框样式BorderStyle

设置合并单元格边框样式,POI提供了方法:
org.apache.poi.ss.util.CellUtil

    /**
     * 设置合并单元格边框 - 线条
     * */
    private static void setBorderStyle(Sheet sheet, CellRangeAddress region) {
        // 合并单元格左边框样式
        RegionUtil.setBorderLeft(BorderStyle.THICK, region, sheet);
        RegionUtil.setLeftBorderColor(IndexedColors.BLUE.getIndex(), region, sheet);

        // 合并单元格上边框样式
        RegionUtil.setBorderTop(BorderStyle.DASH_DOT_DOT, region, sheet);
        RegionUtil.setTopBorderColor(IndexedColors.LIGHT_ORANGE.getIndex(), region, sheet);

        // 合并单元格右边框样式
        RegionUtil.setBorderRight(BorderStyle.SLANTED_DASH_DOT, region, sheet);
        RegionUtil.setRightBorderColor(IndexedColors.RED.getIndex(), region, sheet);

        // 合并单元格下边框样式
        RegionUtil.setBorderBottom(BorderStyle.MEDIUM_DASHED, region, sheet);
        RegionUtil.setBottomBorderColor(IndexedColors.GREEN.getIndex(), region, sheet);
    }

5. 合并单元格边框样式实例

package hssf.sheet.border;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellUtil;
import org.apache.poi.ss.util.RegionUtil;
import org.apache.poi.ss.util.WorkbookUtil;

public class ExportMergeCellBorderStyle {

    public static void main(String[] args) throws Exception {
        File file = new File("C:\\Users\\Administrator\\Desktop\\test.xls");
        if (file.exists()) {
            file.delete();
        }
        BufferedOutputStream out = null;
        try {
            out = new BufferedOutputStream(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test.xls"));
            exportExcel(out);
        } finally {
            out.close();
        }
    }

    private static void exportExcel(BufferedOutputStream out) throws Exception {
        Workbook workbook = new HSSFWorkbook();
        String safeSheetName = WorkbookUtil.createSafeSheetName("合并单元格边框样式");
        Sheet sheet = workbook.createSheet(safeSheetName);

        // 1.创建一个合并单元格
//      CellRangeAddress region = new CellRangeAddress(1, 9, 1, 4);
        CellRangeAddress region = CellRangeAddress.valueOf("B2:E10");
        sheet.addMergedRegion(region);

        // 2.设置合并单元格内容
        Cell cell = sheet.createRow(1).createCell(1);
        cell.setCellValue("合并单元格边框样式");

        // 设置单元格内容水平垂直居中
        CellUtil.setAlignment(cell, HorizontalAlignment.CENTER);
        CellUtil.setVerticalAlignment(cell, VerticalAlignment.CENTER);

        // 3.设置合并单元格边框
        setBorderStyle(sheet, region);

        workbook.write(out);
    }

    /**
     * 设置合并单元格边框 - 线条
     * */
    private static void setBorderStyle(Sheet sheet, CellRangeAddress region) {
        // 合并单元格左边框样式
        RegionUtil.setBorderLeft(BorderStyle.THICK, region, sheet);
        RegionUtil.setLeftBorderColor(IndexedColors.BLUE.getIndex(), region, sheet);

        // 合并单元格上边框样式
        RegionUtil.setBorderTop(BorderStyle.DASH_DOT_DOT, region, sheet);
        RegionUtil.setTopBorderColor(IndexedColors.LIGHT_ORANGE.getIndex(), region, sheet);

        // 合并单元格右边框样式
        RegionUtil.setBorderRight(BorderStyle.SLANTED_DASH_DOT, region, sheet);
        RegionUtil.setRightBorderColor(IndexedColors.RED.getIndex(), region, sheet);

        // 合并单元格下边框样式
        RegionUtil.setBorderBottom(BorderStyle.MEDIUM_DASHED, region, sheet);
        RegionUtil.setBottomBorderColor(IndexedColors.GREEN.getIndex(), region, sheet);
    }

}

POI 边框样式BorderStyle_第2张图片

你可能感兴趣的:(POI)