dear:
apache poi setDiagonal is effective in poi 4.0.1 but not effective in
4.1.0. i really want to know why.
package org.quartz.core;
import java.io.FileOutputStream;
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.Sheet;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.model.ThemesTable;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
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.setDiagonalUp(true);
pr.setStyle(STBorderStyle.Enum.forInt(border.getCode()+1));
}
int idx = _stylesSource.putBorder(new XSSFCellBorder(ct, _theme,
_stylesSource.getIndexedColors()));
System.out.println(idx);
_cellXf.setBorderId(idx);
_cellXf.setApplyBorder(true);
}
public static void main(String[] args) throws Exception {
SXSSFWorkbook wb = new SXSSFWorkbook(SXSSFWorkbook.DEFAULT_WINDOW_SIZE);
CellStyle style = wb.createCellStyle();
style.setBorderTop(BorderStyle.THICK);
style.setBorderBottom(BorderStyle.THICK);
StylesTable _stylesSource = ((SXSSFWorkbook)
wb).getXSSFWorkbook().getStylesSource();
if (_stylesSource != null) {
ThemesTable _theme = _stylesSource.getTheme();
CTXf _cellXf = ((XSSFCellStyle) style).getCoreXf();
setBorderDiagonal(BorderStyle.THICK, _stylesSource, _cellXf, _theme);
}
style.setBorderLeft(BorderStyle.THICK);
style.setBorderRight(BorderStyle.THICK);
Sheet sheet = wb.createSheet("Sheet1");
Cell cell = sheet.createRow(1).createCell(2);
cell.setCellStyle(style);
FileOutputStream fileOut = new FileOutputStream("CellDiagonalBorders.xlsx");
wb.write(fileOut);
wb.close();
if (wb instanceof SXSSFWorkbook)
((SXSSFWorkbook) wb).dispose();
}
}