Share this page 

Create XLS/XLSX file with Apache POI Tag(s): IO


You can use Apache POI HSSF to create an XLS file or Apache POI XSSF for an XLSX file.

Use the MAVEN POM below or download the required jars from https://poi.apache.org/download.html.

<!-- https://poi.apache.org/download.html  -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.0.1</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.0.1</version>
</dependency>

The first example will create a simple XLS file (to create a XSLX, use XSSFWorkbook instead).

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class TestSimpleXLSX {

    public static void main(String... args) throws IOException {
        createSimpleFile();
        System.out.println("Done.");
    }

    private static void createSimpleFile() throws IOException {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("How To");
        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0);
        cell.setCellValue("HowTo");
        try(OutputStream outputStream = new FileOutputStream("d:/simple.xls")) {
            workbook.write(outputStream);
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        finally {
            workbook.close();
        }
    }
}

The next example will create an XLS and a XLSX file.

First a DTO will hold our data. .

public class HowTo {
    private int id;
    private String name;
    private String type;
    private String link;

    private HowTo() {   }

    public static HowTo createHowTo(int id, String name, String type, String link) {
        HowTo howto = new HowTo();
        howto.setId(id);
        howto.setName(name);
        howto.setType(type);
        howto.setLink(link);
        return howto;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }
}
Next, we create our files.
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;


public class TestXLSX {

    public static void main(String... args) throws IOException {
        Workbook hssfWorkbook = new HSSFWorkbook();
        createXLSFile(hssfWorkbook, "d:/howto.xls");

        Workbook xssfWorkbook = new XSSFWorkbook();
        createXLSFile(xssfWorkbook, "d:/howto.xlsx");

        System.out.println("Done.");
    }

    private static void createXLSFile(Workbook workbook, String fileName) {
        Sheet sheet = workbook.createSheet("HowTo List");
        createHeaderRow(workbook, sheet);

        HowTo howto = HowTo.createHowTo(42,"Shorten a long path", "Java", "https://www.rgagnon.com/javadetails/java-0661.html");
        createHowToRow(workbook, sheet, howto, 1);

        howto = HowTo.createHowTo(43, "Hide a DIV", "Javascript", "https://www.rgagnon.com/jsdetails/js-0086.html");
        createHowToRow(workbook, sheet, howto, 2);

        howto = HowTo.createHowTo(44, "Create an Internet shortcut", "Windows batch", "https://www.rgagnon.com/gp/gp-0018.html");
        createHowToRow(workbook, sheet, howto, 3);
        sheet.autoSizeColumn(0);
        sheet.autoSizeColumn(1);
        sheet.autoSizeColumn(2);
        sheet.autoSizeColumn(3);

        try(OutputStream outputStream = new FileOutputStream(fileName)) {
            workbook.write(outputStream);
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private static void createHeaderRow(Workbook workbook, Sheet sheet) {
        Row row = sheet.createRow(0);
        CellStyle headerCellStyle = workbook.createCellStyle();
        headerCellStyle.setFillForegroundColor(IndexedColors.AQUA.getIndex());
        headerCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        Cell cell = row.createCell(0);
        cell.setCellValue("ID");
        cell.setCellStyle(headerCellStyle);

        cell = row.createCell(1);
        cell.setCellValue("Name");
        cell.setCellStyle(headerCellStyle);

        cell = row.createCell(2);
        cell.setCellValue("Type");
        cell.setCellStyle(headerCellStyle);

        cell = row.createCell(3);
        cell.setCellValue("Link");
        cell.setCellStyle(headerCellStyle);
    }

    private static void createHowToRow(Workbook workbook, Sheet sheet, HowTo howTo, int rowIndex) {
        Row row = sheet.createRow(rowIndex);
        Cell cell = row.createCell(0);
        cell.setCellType(CellType.NUMERIC);
        cell.setCellValue(howTo.getId());

        cell = row.createCell(1);
        cell.setCellValue(howTo.getName());

        cell = row.createCell(2);
        cell.setCellValue(howTo.getType());

        cell = row.createCell(3);
        Hyperlink link = workbook.getCreationHelper().createHyperlink(HyperlinkType.URL);
        link.setAddress(howTo.getLink());
        cell.setHyperlink(link);
        cell.setCellValue(howTo.getLink());
    }
}

mail_outline
Send comment, question or suggestion to howto@rgagnon.com