package demo; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellUtil; import org.apache.poi.xssf.usermodel.*; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Locale; public class Demo { public static void main(String[] args) throws IOException { final Workbook wb = WorkbookFactory.create(new File("Excel.xlsx")); final XSSFSheet sheet = (XSSFSheet) wb.getSheet("Tabelle1"); XSSFRow row = sheet.getRow(1); if (row == null) { row = sheet.createRow(1); } final XSSFCell cell = row.createCell(0); cell.setCellValue(2.0); final DataFormat dataFormat = wb.createDataFormat(); System.out.println(printCellStyle(cell)); CellUtil.setCellStyleProperty(cell, CellUtil.DATA_FORMAT, dataFormat.getFormat("0")); System.out.println(printCellStyle(cell)); wb.write(new FileOutputStream(new File("/Users/dirk/IdeaProjects/temp/poi-example/src/main/resources/Output.xlsx"))); } public static String printCellStyle(Cell cell) { final CellStyle style = cell.getCellStyle(); String result = ""; if (style instanceof XSSFCellStyle) { final XSSFCellStyle xStyle = (XSSFCellStyle) style; result = String.format("XSSFStyle (Index %d) {%n", xStyle.getIndex()); result += String.format(" fillPattern: %s%n", xStyle.getFillPatternEnum()); result += String.format(" fillForeground: %s%n", formatColor(xStyle.getFillForegroundColorColor())); result += String.format(" fillBackground: %s%n", formatColor(xStyle.getFillBackgroundColorColor())); result += String.format(" bottomBorder: %s (%s)%n", xStyle.getBorderBottomEnum(), formatColor(xStyle.getBottomBorderXSSFColor())); result += String.format(" borderLeft: %s (%s)%n", xStyle.getBorderLeftEnum(), formatColor(xStyle.getLeftBorderXSSFColor())); result += String.format(" borderTop: %s (%s)%n", xStyle.getBorderTopEnum(), formatColor(xStyle.getTopBorderXSSFColor())); result += String.format(" borderRight: %s (%s)%n", xStyle.getBorderRightEnum(), formatColor(xStyle.getRightBorderXSSFColor())); result += String.format(" horizontal alignment: %s%n", xStyle.getAlignmentEnum()); result += String.format(" vertical alignment: %s%n", xStyle.getVerticalAlignmentEnum()); result += String.format(" hidden: %s%n", xStyle.getHidden()); result += String.format(" locked: %s%n", xStyle.getLocked()); result += String.format(" rotation: %d°%n", xStyle.getRotation()); result += String.format(" indention: %d%n", xStyle.getIndention()); result += String.format(" wrap text: %s%n", xStyle.getWrapText()); result += String.format(" dataFormat: '%s' (%d)%n", xStyle.getDataFormatString(), xStyle.getDataFormat()); final XSSFFont xFont = xStyle.getFont(); result += String.format(" font (Index %d) {%n", xFont.getIndex()); result += String.format(" name: %s%n", xFont.getFontName()); result += String.format(" size: %d%n", xFont.getFontHeightInPoints()); result += String.format(" bold: %s%n", xFont.getBold()); result += String.format(" italics: %s%n", xFont.getItalic()); result += String.format(" strikeout: %s%n", xFont.getStrikeout()); result += String.format(" underline: %s%n", xFont.getUnderline()); result += String.format(" color: %s%n", IndexedColors.fromInt(xFont.getColor())); result += String.format(" typeoffset %s%n", convertTypeOffset(xFont.getTypeOffset())); result += String.format(" }%n"); result += String.format("}%n"); } return result; } private static String convertTypeOffset(short offset) { switch (offset) { case Font.SS_NONE: return "SS_NONE"; case Font.SS_SUB: return "SS_SUB"; case Font.SS_SUPER: return "SS_SUPER"; default: return String.format("undefiniert (%d)", offset); } } private static String formatColor(XSSFColor color) { if (color == null) { return "null"; } else if (color.isIndexed()) { return IndexedColors.fromInt(color.getIndex()).toString(); } else { final byte[] rgb = color.getRGB(); StringBuilder sb = new StringBuilder(); for (byte c : rgb) { int i = c & 0xff; String cs = Integer.toHexString(i); if (cs.length() == 1) { sb.append('0'); } sb.append(cs); } return sb.toString().toUpperCase(Locale.ROOT); } } }