Java ApachePOI:如何添加对角线边框
我想使用ApachePOI绘制对角线边框,但我找不到任何函数可以支持该功能 我试过了,但没用Java ApachePOI:如何添加对角线边框,java,excel,apache-poi,Java,Excel,Apache Poi,我想使用ApachePOI绘制对角线边框,但我找不到任何函数可以支持该功能 我试过了,但没用 XSSFSheetConditionalFormatting my_cond_format_layer = my_sheet.getSheetConditionalFormatting(); XSSFConditionalFormattingRule my_rule = my_cond_format_layer.createConditionalFormattingRule(ComparisonOpe
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);
我不知道为什么直到现在XSSFCellStyle中还没有一个新的样式。但如果我们看看另一个订单的代码。。。方法,然后我们就可以自己做了 例如:
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日编辑:
上述代码适用于ApachePOI的早期版本。以下代码在实际的最新稳定版本3.17中工作:
我不知道为什么直到现在XSSFCellStyle中还没有一个新的样式。但如果我们看看另一个订单的代码。。。方法,然后我们就可以自己做了 例如:
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日编辑:
上述代码适用于ApachePOI的早期版本。以下代码在实际的最新稳定版本3.17中工作:
这个问题是否仅与HSSF有关?如果不是,那么XSSF可能是一个解决方案。不适用于HSSF,至少不适用于我@吕克:作为3年的会员,你应该知道,如果有人显示出代码不起作用,这种恼怒是没有必要的。@AxelRichter:你说得对。我的错误。我已经更新了我的问题。如果有XSSF的解决方案,希望您能提供。@Luc,也可以对XSSF使用相同的解决方案,请检查XSSFBorderFormatting.SetBorderDiagonaln条件格式?否,Excel无法在条件格式中设置对角线边框。在Excel中自己尝试一下。这是不可能的,因为对角线边框在条件格式中是灰色的。@AxelRichter:正常情况下如何?当我有单元格时,我只想设置对角线边框。如果没有条件格式,这个问题是否仅与HSSF有关?如果不是,那么XSSF可能是一个解决方案。不适用于HSSF,至少不适用于我@吕克:作为3年的会员,你应该知道,如果有人显示出代码不起作用,这种恼怒是没有必要的。@AxelRichter:你说得对。我的错误。我已经更新了我的问题。如果有XSSF的解决方案,希望您能提供。@Luc,也可以对XSSF使用相同的解决方案,请检查XSSFBorderFormatting.SetBorderDiagonaln条件格式?否,Excel无法在条件格式中设置对角线边框。在Excel中自己尝试一下。这是不可能的,因为对角线边框在条件格式中是灰色的。@AxelRichter:正常情况下如何?当我有单元格时,我只想设置对角线边框。无条件格式化谢谢!太棒了。我不知道StylesTable、ThemesTable和CTXf。新体验!!!非常感谢。太棒了。我不知道StylesTable、ThemesTable和CTXf。新体验!!!