use of org.apache.poi.ss.usermodel.CellStyle in project poi by apache.
the class CellStyleDetails method main.
public static void main(String[] args) throws Exception {
if (args.length == 0) {
throw new IllegalArgumentException("Filename must be given");
}
Workbook wb = WorkbookFactory.create(new File(args[0]));
DataFormatter formatter = new DataFormatter();
for (int sn = 0; sn < wb.getNumberOfSheets(); sn++) {
Sheet sheet = wb.getSheetAt(sn);
System.out.println("Sheet #" + sn + " : " + sheet.getSheetName());
for (Row row : sheet) {
System.out.println(" Row " + row.getRowNum());
for (Cell cell : row) {
CellReference ref = new CellReference(cell);
System.out.print(" " + ref.formatAsString());
System.out.print(" (" + cell.getColumnIndex() + ") ");
CellStyle style = cell.getCellStyle();
System.out.print("Format=" + style.getDataFormatString() + " ");
System.out.print("FG=" + renderColor(style.getFillForegroundColorColor()) + " ");
System.out.print("BG=" + renderColor(style.getFillBackgroundColorColor()) + " ");
Font font = wb.getFontAt(style.getFontIndex());
System.out.print("Font=" + font.getFontName() + " ");
System.out.print("FontColor=");
if (font instanceof HSSFFont) {
System.out.print(renderColor(((HSSFFont) font).getHSSFColor((HSSFWorkbook) wb)));
}
if (font instanceof XSSFFont) {
System.out.print(renderColor(((XSSFFont) font).getXSSFColor()));
}
System.out.println();
System.out.println(" " + formatter.formatCellValue(cell));
}
}
System.out.println();
}
wb.close();
}
use of org.apache.poi.ss.usermodel.CellStyle in project poi by apache.
the class AligningCells method createCell.
/**
* Creates a cell and aligns it a certain way.
*
* @param wb the workbook
* @param row the row to create the cell in
* @param column the column number to create the cell in
* @param halign the horizontal alignment for the cell.
*/
private static void createCell(XSSFWorkbook wb, XSSFRow row, int column, HorizontalAlignment halign, VerticalAlignment valign) {
CreationHelper ch = wb.getCreationHelper();
XSSFCell cell = row.createCell(column);
cell.setCellValue(ch.createRichTextString("Align It"));
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(halign);
cellStyle.setVerticalAlignment(valign);
cell.setCellStyle(cellStyle);
}
use of org.apache.poi.ss.usermodel.CellStyle in project poi by apache.
the class NewLinesInCells method main.
public static void main(String[] args) throws IOException {
//or new HSSFWorkbook();
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet();
Row row = sheet.createRow(2);
Cell cell = row.createCell(2);
cell.setCellValue("Use \n with word wrap on to create a new line");
//to enable newlines you need set a cell styles with wrap=true
CellStyle cs = wb.createCellStyle();
cs.setWrapText(true);
cell.setCellStyle(cs);
//increase row height to accomodate two lines of text
row.setHeightInPoints((2 * sheet.getDefaultRowHeightInPoints()));
//adjust column width to fit the content
sheet.autoSizeColumn(2);
FileOutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx");
wb.write(fileOut);
fileOut.close();
wb.close();
}
use of org.apache.poi.ss.usermodel.CellStyle in project poi by apache.
the class CreateUserDefinedDataFormats method main.
public static void main(String[] args) throws IOException {
//or new HSSFWorkbook();
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("format sheet");
CellStyle style;
DataFormat format = wb.createDataFormat();
Row row;
Cell cell;
short rowNum = 0;
short colNum = 0;
row = sheet.createRow(rowNum);
cell = row.createCell(colNum);
cell.setCellValue(11111.25);
style = wb.createCellStyle();
style.setDataFormat(format.getFormat("0.0"));
cell.setCellStyle(style);
row = sheet.createRow(++rowNum);
cell = row.createCell(colNum);
cell.setCellValue(11111.25);
style = wb.createCellStyle();
style.setDataFormat(format.getFormat("#,##0.0000"));
cell.setCellStyle(style);
FileOutputStream fileOut = new FileOutputStream("ooxml_dataFormat.xlsx");
wb.write(fileOut);
fileOut.close();
wb.close();
}
use of org.apache.poi.ss.usermodel.CellStyle in project poi by apache.
the class WorkingWithBorders method main.
public static void main(String[] args) throws IOException {
//or new HSSFWorkbook();
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("borders");
// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow((short) 1);
// Create a cell and put a value in it.
Cell cell = row.createCell((short) 1);
cell.setCellValue(4);
// Style the cell with borders all around.
CellStyle style = wb.createCellStyle();
style.setBorderBottom(BorderStyle.THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderLeft(BorderStyle.THIN);
style.setLeftBorderColor(IndexedColors.GREEN.getIndex());
style.setBorderRight(BorderStyle.THIN);
style.setRightBorderColor(IndexedColors.BLUE.getIndex());
style.setBorderTop(BorderStyle.MEDIUM_DASHED);
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
cell.setCellStyle(style);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("xssf-borders.xlsx");
wb.write(fileOut);
fileOut.close();
wb.close();
}
Aggregations