我不知道爲什麼沒有在XSSFCellStyle
一個setBorderDiagonal
直到現在。但如果我們看看其他setBorder...
方法的代碼,那麼我們可以製作我們自己的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
:
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();
}
}
這個問題只關於HSSF嗎?如果不是,XSSF可能是一個解決方案。不是HSSF,至少不是我。 @Luc:作爲一名3年的會員,您應該知道,如果您顯示代碼「無法正常工作」,那麼這種煩惱就不必要了。 –
@AxelRichter:你說得對。我的錯。我更新了我的問題。如果有XSSF的解決方案,希望你能提供。 – Luc
@Luc,也可以使用相同的XSSF,檢查XSSFBorderFormatting.setBorderDiagonal – xSAVIKx