0

In an Apache POI XWPFDocument (Word) I use this code to assign the user defined format MyHeadline to a new paragraph. MyHeadline is contained in template.docx which was created with LibreOffice.

XWPFDocument document = new XWPFDocument(new FileInputStream("template.docx"));
XWPFParagraph paragraph = document.createParagraph();
paragraph.setStyle("MyHeadline");

My question: Is there a similar way to do this with an Apache POI XSSFWorkbook (Excel)?

Malfunctioning code!

XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream("template.xlsx"));
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellValue("Brennholzverleih");
cell.setCellStyle("MyCellStyle"); // This is what I want to do. "MyCellStyle" is an
                                  // user defined cell format contained in "template.xlsx".
                                  // Of course there is no setCellStyle(String) method!
Martin
  • 178
  • 10
  • See https://stackoverflow.com/questions/46422103/xssf-excel-named-styles/46489999#46489999 for how to set and get user defined named cell styles in `XSSF`. – Axel Richter Mar 25 '19 at 16:38
  • @Axel Sorry, I did not know the right terms to search for. Thanks a lot for pointing me there, got it to work. Unfortunately some properties of the named style from the template file are assigned (e.g. right alignment) while others were not (e.g. text color). But the style **is** present and assigned. If I double click the style in LibreOffice the missing properties were set, too. Maybe this problem is more about LibreOffice!? – Martin Mar 25 '19 at 17:44
  • Problem solved... and LibreOffice was **not** part of this. When you create a XLSX template to define formats you want to use in an XSSFWorkbook, you have to apply each of these formats to at least one cell in your template file. Otherwise the formats are not handled correctly. – Martin Mar 25 '19 at 20:05

0 Answers0