Shares

Java is a powerful programming language with an array of features. “Write once, run anywhere” is truly a testament to the JVM (Java Virtual Machine) which allows Java code to function cross platform. Without getting into the details, today, we are going to use Java to read and write Excel (.xls) files, using a small library called JExcelAPI. The JExcelAPI is an open source library which allows you to read, write and do a lot of cool stuff with Java and Excel. Excel Training provides you best Java training with proper knowledge about Java language and coding. Today, we are going to show you the code that you can use to read and write data in Excel format (.xls), using Java of course. Let’s get started.

Setting up on Eclipse

Before starting to code, we need to set up the working environment on the popular IDE Eclipse. You can download it by clicking the link here: Eclipse. Once you have installed the Eclipse onto your computer (most Java programmers already have), you need to create a new Java Project, as shown by the image below.

JavaExcelImage1

Once you have created a new Java Project, you need to download the JExcelAPI library package by clicking this link. Clicking this link will start the download process automatically. Unpack the .zip file and copy the file jxl.jar from there into your workspace. It helps to create a new folder in your workspace folder alongside src/. Name it lib/. Paste the jxl.jar in there. Now, open Eclipse and make sure jxl.jar is hooked to the build path. Eclipse lets you do this manually by right clicking the .jar file and following the options from there on. It should be a breeze from here on, as you’re ready to code.

Reading Excel (.xls) files

JExcelAPI can be used to read and write Excel files. The supported format is .xls. JExcelAPI can read an Excel spreadsheet from a file stored on the local filesystem. It even works from input streams. The code below is used to read an Excel file placed in a new folder of the Eclipse workspace folder i.e. in the same place as the src/ folder or root. It is better to keep your .xls files in a new folder called resources/, like we have in the example code as shown below.

import java.io.File;
import java.io.IOException;
import jxl.*;
import jxl.read.biff.BiffException;

public class ReadExcel {

	public static void main(String[] args) throws IOException, BiffException {

		Workbook workbook = Workbook.getWorkbook(new File("resources/sample.xls"));
		Sheet sheet = workbook.getSheet(0);

		Cell A = sheet.getCell(0,0);
		Cell B = sheet.getCell(1,0);
		Cell C = sheet.getCell(2,0);

		String stringA = A.getContents();
		String stringB = B.getContents();
		String stringC = C.getContents();

		System.out.println(stringA);
		System.out.println(stringB);
		System.out.println(stringC);

	}

}

Writing Excel (.xls) file

JExcelAPI is not just about reading data from Excel Spreadsheets. You can use it to create .xls or Excel files too. The code below demonstrates how you can use a few lines of Java code to create an .xls file, without any formatting (even though its possible but its beyond the scope of this tutorial).


import java.io.File;
import java.io.IOException;
import jxl.*;
import jxl.read.biff.BiffException;
import jxl.write.*;
import jxl.write.Number;
import jxl.write.biff.RowsExceededException;

public class WriteExcel {

	public static void main(String[] args) throws IOException, BiffException, RowsExceededException, WriteException {

		WritableWorkbook workbook = Workbook.createWorkbook(new File("output.xls"));
		WritableSheet sheet = workbook.createSheet("First Sheet", 0);

		Label label = new Label(0, 0, "Mr Geek Ltd");
		sheet.addCell(label);

		Number number = new Number(1, 0, 1.618);
		sheet.addCell(number);

		Label label1 = new Label(2, 0, "Ali Gajani");
		sheet.addCell(label1);

		workbook.write();
		workbook.close();

		System.out.println("File output complete");

	}

}

If you have any questions regarding this tutorial, do not hesitate to drop a comment below.


 

About Ali Gajani

Hi. I am Ali Gajani. I started Mr. Geek in early 2012 as a result of my growing enthusiasm and passion for technology. I love sharing my knowledge and helping out the community by creating useful, engaging and compelling content. If you want to write for Mr. Geek, just PM me on my Facebook profile.