简体   繁体   中英

Why is text formatting in Apache POI not working with XSSF but HSSF?

I want to create a workbook with some markup by using Apache POI.

I tried to do so with the more modern XSSF packages but ended up with them not working for even the simpliest purposes like changing colors.

I give you my Test-class, to try it yourself (just change the call of xssf to hssf in the main method).

import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

import com.cisamag.projects.basiclibrary.logical.file.FileHelper;

public class Test {

private static File path = new File("pathtofile");

    public static void main(String[] args) throws IOException{
        xssf();
    }


    public static void xssf() throws IOException {
        File f = new File(path);
        if(f.exists()){
            f.delete();
        }
        f.createNewFile();

        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet();
        XSSFCell c = sheet.createRow(1).createCell(1);
        XSSFCellStyle cellStyle = c.getCellStyle();

        Font font = workbook.createFont();
        font.setItalic(true);
        font.setColor(Font.COLOR_RED);
        cellStyle.setFont(font);
        c.setCellStyle(cellStyle);
        c.setCellValue("HELLO");

        workbook.write(new FileOutputStream(f));
        Desktop.getDesktop().open(f);
    }

    public static void hssf() throws IOException {
        File f = new File(path);
        if(f.exists()){
            f.delete();
        }
        f.createNewFile();

        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        HSSFCell c = sheet.createRow(1).createCell(1);
        HSSFCellStyle cellStyle = c.getCellStyle();

        Font font = workbook.createFont();
        font.setItalic(true);
        font.setColor(new HSSFColor.RED().getIndex());
        cellStyle.setFont(font);
        c.setCellStyle(cellStyle);
        c.setCellValue("HELLO");

        workbook.write(new FileOutputStream(f));
        Desktop.getDesktop().open(f);
    }
}

You need to create the CellStyle first - in your example c.getCellStyle() returns the default CellStyle (according to the api docs ), which apparently cannot be modified.

So, replace

    XSSFCellStyle cellStyle = c.getCellStyle();

in your example with

    XSSFCellStyle cellStyle = workbook.createCellStyle();

and it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM