Apache POI:如何添加对角线边框 [英] Apache POI: How to add Diagonal Border
问题描述
我想使用 Apache POI
来绘制对角线边框
但我找不到任何可以支持的功能该功能。
I would like to use Apache POI
to draw Diagonal Border
but I couldn't find any function can support that feature.
我试过这个 setBorderDiagonal 但它不工作。
I tried this setBorderDiagonal but it isn't working.
XSSFSheetConditionalFormatting my_cond_format_layer = my_sheet.getSheetConditionalFormatting();
XSSFConditionalFormattingRule my_rule = my_cond_format_layer.createConditionalFormattingRule(ComparisonOperator.BETWEEN, "14", "16");
XSSFConditionalFormattingRule my_rule_2 = my_cond_format_layer.createConditionalFormattingRule(ComparisonOperator.EQUAL, "33");
XSSFPatternFormatting fill_pattern_2 = my_rule_2.createPatternFormatting();
fill_pattern_2.setFillBackgroundColor(IndexedColors.YELLOW.getIndex());
XSSFBorderFormatting borderFormatting = my_rule_2.createBorderFormatting();
borderFormatting.setDiagonalBorderColor(IndexedColors.BLUE.getIndex());
borderFormatting.setBorderDiagonal(BorderFormatting.BORDER_THICK);
ConditionalFormattingRule[] multiple_rules = {my_rule, my_rule_2};
CellRangeAddress[] my_data_range = {CellRangeAddress.valueOf("A1:A4")};
my_cond_format_layer.addConditionalFormatting(my_data_range, multiple_rules);
推荐答案
我不知道为什么没有 setBorderDiagonal
在 XSSFCellStyle
中,直到现在。但是如果我们看一下其他 setBorder ...
方法的代码,那么我们就可以制作自己的 setBorderDiagonal
。
I don't know why there is not a setBorderDiagonal
in XSSFCellStyle
until now. But if we take a look at the code of the other setBorder...
methods, then we could make our own setBorderDiagonal
.
示例:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.model.ThemesTable;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
import java.io.FileOutputStream;
import java.io.IOException;
class CellDiagonalBorders {
private static CTBorder getCTBorder(StylesTable _stylesSource, CTXf _cellXf ){
CTBorder ct;
if(_cellXf.getApplyBorder()) {
int idx = (int)_cellXf.getBorderId();
XSSFCellBorder cf = _stylesSource.getBorderAt(idx);
ct = (CTBorder)cf.getCTBorder().copy();
} else {
ct = CTBorder.Factory.newInstance();
}
return ct;
}
public static void setBorderDiagonal(short border, StylesTable _stylesSource, CTXf _cellXf , ThemesTable _theme) {
CTBorder ct = getCTBorder(_stylesSource, _cellXf);
CTBorderPr pr = ct.isSetDiagonal() ? ct.getDiagonal() : ct.addNewDiagonal();
if(border == BorderFormatting.BORDER_NONE) {
ct.unsetDiagonal();
}
else {
ct.setDiagonalDown(true);
ct.setDiagonalUp(true);
pr.setStyle(STBorderStyle.Enum.forInt(border + 1));
}
int idx = _stylesSource.putBorder(new XSSFCellBorder(ct, _theme));
_cellXf.setBorderId(idx);
_cellXf.setApplyBorder(true);
}
public static void main(String[] args) {
try {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("Sheet1");
Cell cell = sheet.createRow(2).createCell(2);
CellStyle style = wb.createCellStyle();
StylesTable _stylesSource = ((XSSFWorkbook)wb).getStylesSource();
ThemesTable _theme = _stylesSource.getTheme();
CTXf _cellXf = ((XSSFCellStyle)style).getCoreXf();
setBorderDiagonal(BorderFormatting.BORDER_THICK, _stylesSource, _cellXf, _theme);
cell.setCellStyle(style);
FileOutputStream fileOut = new FileOutputStream("CellDiagonalBorders.xlsx");
wb.write(fileOut);
} catch (IOException ioex) {
}
}
}
编辑2018年2月23日:
以上代码适用于以前版本的 apache poi
。以下代码适用于实际最新稳定版本 3.17
:
Edit February 23 2018:
Above code had worked for previous versions of apache poi
. Following code works in actual latest stable version 3.17
:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.xssf.streaming.*;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.model.ThemesTable;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
import java.io.FileOutputStream;
import java.io.IOException;
class CellDiagonalBorders {
private static CTBorder getCTBorder(StylesTable _stylesSource, CTXf _cellXf ) {
CTBorder ct;
if(_cellXf.getApplyBorder()) {
int idx = (int)_cellXf.getBorderId();
XSSFCellBorder cf = _stylesSource.getBorderAt(idx);
ct = (CTBorder)cf.getCTBorder().copy();
} else {
ct = CTBorder.Factory.newInstance();
}
return ct;
}
public static void setBorderDiagonal(BorderStyle border, StylesTable _stylesSource, CTXf _cellXf, ThemesTable _theme) {
CTBorder ct = getCTBorder(_stylesSource, _cellXf);
CTBorderPr pr = ct.isSetDiagonal() ? ct.getDiagonal() : ct.addNewDiagonal();
if(border == BorderStyle.NONE) {
ct.unsetDiagonal();
}
else {
ct.setDiagonalDown(true);
ct.setDiagonalUp(true);
pr.setStyle(STBorderStyle.Enum.forInt(border.getCode() + 1));
}
int idx = _stylesSource.putBorder(new XSSFCellBorder(ct, _theme, _stylesSource.getIndexedColors()));
_cellXf.setBorderId(idx);
_cellXf.setApplyBorder(true);
}
public static void main(String[] args) throws Exception {
//Workbook wb = new XSSFWorkbook();
Workbook wb = new SXSSFWorkbook(SXSSFWorkbook.DEFAULT_WINDOW_SIZE);
CellStyle style = wb.createCellStyle();
style.setBorderTop(BorderStyle.THICK);
style.setBorderBottom(BorderStyle.THICK);
StylesTable _stylesSource = null;
if (wb instanceof SXSSFWorkbook) {
_stylesSource = ((SXSSFWorkbook)wb).getXSSFWorkbook().getStylesSource();
} else if (wb instanceof XSSFWorkbook) {
_stylesSource = ((XSSFWorkbook)wb).getStylesSource();
}
if (_stylesSource != null) {
ThemesTable _theme = _stylesSource.getTheme();
CTXf _cellXf = ((XSSFCellStyle)style).getCoreXf();
setBorderDiagonal(BorderStyle.DOUBLE, _stylesSource, _cellXf, _theme);
}
style.setBorderLeft(BorderStyle.THICK);
style.setBorderRight(BorderStyle.THICK);
Sheet sheet = wb.createSheet("Sheet1");
for (int r = 1; r < 1000; r++) {
Cell cell = sheet.createRow(r).createCell(2);
cell.setCellStyle(style);
}
FileOutputStream fileOut = new FileOutputStream("CellDiagonalBorders.xlsx");
wb.write(fileOut);
wb.close();
if (wb instanceof SXSSFWorkbook) ((SXSSFWorkbook)wb).dispose();
}
}
这篇关于Apache POI:如何添加对角线边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!