Apache POI Excel 绘制边框
Apache POI 允许我们在工作簿表中应用一组边框。PropertyTemplate对象模拟了这种用方法和定义为允许绘图的顶部,底部常量,左,右,水平,垂直,里面,单元格外边界。
让我们看一个在单元格周围绘制边框的示例。
Apache POI 绘图边框示例
package com.yiidian;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.PropertyTemplate;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class DrawingBorderExample {
public static void main(String[] args) {
try (OutputStream os = new FileOutputStream("yiidian.xls")) {
PropertyTemplate pt = new PropertyTemplate();
pt.drawBorders(new CellRangeAddress(1, 2, 1, 2),
BorderStyle.MEDIUM, BorderExtent.ALL);
pt.drawBorders(new CellRangeAddress(5, 6, 1, 2),
BorderStyle.MEDIUM, BorderExtent.OUTSIDE);
pt.drawBorders(new CellRangeAddress(5, 6, 1, 2), BorderStyle.THIN,
BorderExtent.INSIDE);
pt.drawBorders(new CellRangeAddress(9, 10, 1, 3),
BorderStyle.MEDIUM, IndexedColors.GREEN.getIndex(),
BorderExtent.OUTSIDE);
pt.drawBorders(new CellRangeAddress(9, 10, 1, 3),
BorderStyle.MEDIUM, IndexedColors.BLUE.getIndex(),
BorderExtent.INSIDE_VERTICAL);
pt.drawBorders(new CellRangeAddress(9, 10, 1, 3),
BorderStyle.MEDIUM, IndexedColors.RED.getIndex(),
BorderExtent.INSIDE_HORIZONTAL);
pt.drawBorders(new CellRangeAddress(10, 10, 2, 2),
BorderStyle.NONE,
BorderExtent.ALL);
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("Sheet");
pt.applyBorders(sheet);
wb.write(os);
}catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
输出结果为:
热门文章
优秀文章