Created
September 17, 2016 01:48
-
-
Save lucduong/4e292d17577daa2335d637c5dcd09839 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2016 DOU Networks Co., Ltd - All Rights Reserved | |
* Unauthorized copying of this file, via any medium is strictly prohibited | |
* Proprietary and confidential | |
* Written by luc <luc@ltv.vn>, September 17, 2016 | |
*/ | |
package vn.ltv.demo.poi; | |
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_THIN, _stylesSource, _cellXf, _theme); | |
cell.setCellStyle(style); | |
FileOutputStream fileOut = new FileOutputStream("/Users/luc/Desktop/CellDiagonalBorders.xlsx"); | |
wb.write(fileOut); | |
} catch (IOException ioex) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment