Support free tutorials

vogella training Training Books

Excel and Java - Read and Write Excel with Java - Tutorial

Lars Vogel

Version 1.3



This article demonstrate how to create and how to read Excel files with the Java Excel API.

Table of Contents

1. Installation
2. Create an Excel Spreadsheet
3. Read an existing Excel Spreadsheet
4. About this website
4.1. Donate to support free tutorials
4.2. Questions and discussion
4.3. License for this tutorial and its code
5. Links and Literature
5.1. Source Code
5.2. Java Excel Resources
5.3. vogella Resources

1. Installation

Download the Java Excel library from the webpage

To use this library in your Java program add the lib jxl.jar to your classpath in your project. See Changing classpath in Eclipse.

2. Create an Excel Spreadsheet

Create a new Java project called Create the package and the following class.

package writer;

import java.util.Locale;

import jxl.CellView;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.UnderlineStyle;
import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class WriteExcel {

  private WritableCellFormat timesBoldUnderline;
  private WritableCellFormat times;
  private String inputFile;
public void setOutputFile(String inputFile) {
  this.inputFile = inputFile;

  public void write() throws IOException, WriteException {
    File file = new File(inputFile);
    WorkbookSettings wbSettings = new WorkbookSettings();

    wbSettings.setLocale(new Locale("en", "EN"));

    WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
    workbook.createSheet("Report", 0);
    WritableSheet excelSheet = workbook.getSheet(0);


  private void createLabel(WritableSheet sheet)
      throws WriteException {
    // Lets create a times font
    WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
    // Define the cell format
    times = new WritableCellFormat(times10pt);
    // Lets automatically wrap the cells

    // create create a bold font with unterlines
    WritableFont times10ptBoldUnderline = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD, false,
    timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
    // Lets automatically wrap the cells

    CellView cv = new CellView();

    // Write a few headers
    addCaption(sheet, 0, 0, "Header 1");
    addCaption(sheet, 1, 0, "This is another header");


  private void createContent(WritableSheet sheet) throws WriteException,
      RowsExceededException {
    // Write a few number
    for (int i = 1; i < 10; i++) {
      // First column
      addNumber(sheet, 0, i, i + 10);
      // Second column
      addNumber(sheet, 1, i, i * i);
    // Lets calculate the sum of it
    StringBuffer buf = new StringBuffer();
    Formula f = new Formula(0, 10, buf.toString());
    buf = new StringBuffer();
    f = new Formula(1, 10, buf.toString());

    // now a bit of text
    for (int i = 12; i < 20; i++) {
      // First column
      addLabel(sheet, 0, i, "Boring text " + i);
      // Second column
      addLabel(sheet, 1, i, "Another text");

  private void addCaption(WritableSheet sheet, int column, int row, String s)
      throws RowsExceededException, WriteException {
    Label label;
    label = new Label(column, row, s, timesBoldUnderline);

  private void addNumber(WritableSheet sheet, int column, int row,
      Integer integer) throws WriteException, RowsExceededException {
    Number number;
    number = new Number(column, row, integer, times);

  private void addLabel(WritableSheet sheet, int column, int row, String s)
      throws WriteException, RowsExceededException {
    Label label;
    label = new Label(column, row, s, times);

  public static void main(String[] args) throws WriteException, IOException {
    WriteExcel test = new WriteExcel();
        .println("Please check the result file under c:/temp/lars.xls ");

I assume that the code is pretty much self-explaining. I tried to add lots of comments to make it easier to understand.For more complex examples have a look at the excellent documentation of the Java Excel API which is also part of the distribution.

3. Read an existing Excel Spreadsheet

Reuse the project "". Create a package "" and the following class "ReadExcel".

package reader;


import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;

public class ReadExcel {

  private String inputFile;

  public void setInputFile(String inputFile) {
    this.inputFile = inputFile;

  public void read() throws IOException  {
    File inputWorkbook = new File(inputFile);
    Workbook w;
    try {
      w = Workbook.getWorkbook(inputWorkbook);
      // Get the first sheet
      Sheet sheet = w.getSheet(0);
      // Loop over first 10 column and lines

      for (int j = 0; j < sheet.getColumns(); j++) {
        for (int i = 0; i < sheet.getRows(); i++) {
          Cell cell = sheet.getCell(j, i);
          CellType type = cell.getType();
          if (type == CellType.LABEL) {
            System.out.println("I got a label "
                + cell.getContents());

          if (type == CellType.NUMBER) {
            System.out.println("I got a number "
                + cell.getContents());

    } catch (BiffException e) {

  public static void main(String[] args) throws IOException {
    ReadExcel test = new ReadExcel();


Create an excel spreadsheet and save it somewhere, e.g. "c:/temp/lars.xls".

I assume that the code is pretty much self-explaining. I tried to add lots of comments to make it easier to understand.For more complex examples have a look at the excellent documentation of the Java Excel API which is also part of the distribution.

4. About this website

4.1. Donate to support free tutorials

Please consider a contribution Support free tutorials if this article helped you. It will help to maintain our content and our Open Source activities.

4.2. Questions and discussion

Writing and updating these tutorials is a lot of work. If this free community service was helpful, you can support the cause by giving a tip as well as reporting typos and factual errors.

If you find errors in this tutorial, please notify me (see the top of the page). Please note that due to the high volume of feedback I receive, I cannot answer questions to your implementation. Ensure you have read the vogella FAQ as I don't respond to questions already answered there.

4.3. License for this tutorial and its code

This tutorial is Open Content under the CC BY-NC-SA 3.0 DE license. Source code in this tutorial is distributed under the Eclipse Public License. See the vogella License page for details on the terms of reuse.

5. Links and Literature

5.1. Source Code

Source Code of Examples

5.2. Java Excel Resources Java Excel API - Homepage

5.3. vogella Resources

The vogella company provides comprehensive training and education services from experts in the areas of Eclipse RCP, Android, Git, Java, Gradle and Spring. We offer both public and inhouse training. Whichever course you decide to take, you are guaranteed to experience what many before you refer to as “The best IT class I have ever attended”. The vogella company offers expert consulting services, development support and coaching. Our customers range from Fortune 100 corporations to individual developers.