In this Java Excel tutorial, you will learn how to read and write from Excel files in Java. You will learn steps to read/write both XLS and XLSX file format by using the Apache POI library. In this example, we will particularly focus on reading and writing String and Date values into Excel file as writing dates are little bit tricky. In our earlier Java Excel tutorial, you have already learned how to read/write Numeric types from Excel in Java, but we haven't touched date values, which are also stored as numeric types, we will learn that in this tutorial. There are two parts of this tutorial, in first part we will write date and String values into the XLS file and in the second part we will read them from the XLS file.
You might aware that Excel file now comes with two formats, XLS file which is an OLE format, and XLSX format, which is also known as OpenXML format. Apache POI supports both format but you would need different JAR files to read/write XLS and XLSX files. You need poi-3.12.jar to read XLS file and poi-ooxml-3.12.jar to read XLSX file in Java.
You can write different OLE formats using poi-3.12.jar for example you can also use this JAR to read Microsoft Word files witch .DOC extension and Microsoft PowerPoint files with .PPT extension in Java. Similarly you can read other OpenXML formats e.g. DOCX and PPTX using poi-ooxml-3.12.jar file.
It's very important to understand which JAR files you need to read which kind of Excel files in Java, because classes used to read different Excel file format are different e.g. to read old Excel file format i.e. XLS files you need HSSFWorkbook class, which is inside poi-XX.jar, while class used to read current Excel file format i.e. XLSX file is XSSFWorkbook, which is inside poi-ooxml.jar library.
If you are using Maven then include following two dependencies to use Apache POI in your Java program :
Main advantage of using Maven is that it not only downloads direct dependency e.g. poi.jar and poi-ooxml.jar but also download transitive dependency e.g. JARS on which POI library is internally dependent. For example, I have just specified Apache POI JAR files but Maven will also download xmlbeans-2.6.0.jar, stax-api-1.0.1.jar, poi-ooxml-schemas-3.12.jar and commons-codec-1.9.jar.
POI is for reading OLE format e.g. XLS, DOC and .PPT format, while poi-ooxml.jar is to read XLSX, DOCX and .PPTX format. Don't download just POI jar, always include transitive dependency.
Apache POI JARs to Read/Write Excel File in Java
Though there are couple of open source library available to read and write from Excel file in Java e.g. JXL, the most feature rich and most popular one is Apache POI library. You can read both types of Excel file format using this library. In order to use this library either you need to download POI JAR files and add into your Eclipse's build path manually or you can use Maven to download dependency for you.If you are using Maven then include following two dependencies to use Apache POI in your Java program :
<dependencies> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.12</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.12</version> </dependency> </dependencies>
Main advantage of using Maven is that it not only downloads direct dependency e.g. poi.jar and poi-ooxml.jar but also download transitive dependency e.g. JARS on which POI library is internally dependent. For example, I have just specified Apache POI JAR files but Maven will also download xmlbeans-2.6.0.jar, stax-api-1.0.1.jar, poi-ooxml-schemas-3.12.jar and commons-codec-1.9.jar.
JAR Dependencies :
If you are more comfortable by downloading JAR files by yourself, you can download Apache POI JARS from here . This will download whole bundle so you don't need to worry, but make sure it contains following JAR files if your application is going to support both XLS and XLSX format.- poi-3.12.jar
- commons-codec-1.9.jar
- poi-ooxml-3.12.jar
- poi-ooxml-schemas-3.12.jar
- xmlbeans-2.6.0.jar
- stax-api-1.0.1.jar
POI is for reading OLE format e.g. XLS, DOC and .PPT format, while poi-ooxml.jar is to read XLSX, DOCX and .PPTX format. Don't download just POI jar, always include transitive dependency.
For example, if you include just poi-3.12.jar then your program will compile fine because you are not using transitive dependency e.g. xmlbeans directly but it will fail at runtime with error like java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject because of missing xmlbeans.jar dependency.
How to read from Excel File in Java? Example
Suppose you have a cell in your excel file which contains a date e.g. birthdate? how do you read it? Most of you will say that you will read that cell by first creating a Workbook, then getting a sheet from that workbook, then getting the cell from that sheet which is containing date value and finally getting cell value from that cell.Cool, these are the steps to read data from Excel file in Java, but you forgot one thing you need to find the cell type before getting cell value, otherwise you will be get error reading that cell. Reading date values are even more tricky. To your surprise, there is no date cell type in Excel (both XLS and XLSX), instead of Excel stores date as numeric type.
So you need to compare the cell type with HSSFCell.CELL_TYPE_NUMERIC if you are reading XLS file and XSSFCell.CELL_TYPE_NUMERIC if you reading XLSX file, but story doesn't end here, if you just print the cell value by using getNumericCellValue(), you will not get any error but you will see an arbitrary number.
In order to print the actual date value you need to use method getDateCellValue(), which will return an object of java.util.Date, if you want to display a formatted date, then you need to format date using SimpleDateFormat or by using Joda Date and Time library.
In our example, we will create an excel file which contains one row and two columns. The first column will contain a String type, where we will store name and the second column will be of date type, where we will date of birth. Later, we will read the same excel file in our Java program to display name and date values into the console.
In our example, we will create an excel file which contains one row and two columns. The first column will contain a String type, where we will store name and the second column will be of date type, where we will date of birth. Later, we will read the same excel file in our Java program to display name and date values into the console.
In order to read an excel file in Java, it must be in classpath. In order to avoid issues, I will use Eclipse IDE to write this program and it will create an excel file in Eclipse's project directly, which always remains in the classpath.
These steps are fine for writing String and Numeric values but in order to write date values into Excel file, you need to follow following more steps :
In this program, reading and writing logic are encapsulated into two static utility methods readFromExcel() and writeIntoExcel(), so you can also take a look at them for exact code for reading writing XLS file in Java.
In our program, we have first created an excel file with String and date columns and later read from the same file and displayed the values into the console. Now let's verify output of this program. It's correctly display the date value, though not formatted, which means the excel file was created successfully and later Java was able to read from it.
How to read/write from XLS file in Java
This is our first example to read String and date values from Excel files in Java. In this example, we are first creating an old Excel file format i.e. XLS file birthdays.xls and later we will read from the same file. Once we run our program, you can see this excel file created in your Eclipse project directory, as shown below.Steps to write Data into XLS file in Java
- Include poi-3.12.jar in your Java program's classpath
- Create an object of HSSFWorkBook
- Create a Sheet on that workbook by calling createSheet() method
- Create a Row on that sheet by calling createRow() method
- Create a Cell by calling createCell() method
- Set value to that cell by calling setCellValue() method.
- Write workbook content into File using FileOutputStream object.
- Close the workbook object by calling close() method
These steps are fine for writing String and Numeric values but in order to write date values into Excel file, you need to follow following more steps :
- Create a DataFormat
- Create a CellStyle
- Set format into CellStyle
- Set CellStyle into Cell
- Write java.util.Date into Cell
Step to read data from XLS file in Java
- Include poi-3.12.jar in your Java program's classpath
- Create an object of HSSFWorkBook by opening excel file using FileInputStream
- Get a Sheet from workbook by calling getSheet() method, you can pass name or sheet index
- Get a Row from that sheet by calling getRow() method, you can pass index
- Get a Cell by calling getCell() method
- Get the Cell type by calling getCellType() method.
- Depending upon Cell type, call getStringCellValue(), getNumericCellValue() or getDateCellValue() method to get value.
- Close the workbook object by calling close() method
In this program, reading and writing logic are encapsulated into two static utility methods readFromExcel() and writeIntoExcel(), so you can also take a look at them for exact code for reading writing XLS file in Java.
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.DataFormat; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; /** * Simple Java Program to read and write dates from Excel file in Java. * This example particularly read Excel file in OLE format i.e. * Excel file with extension .xls, also known as XLS files. * * @author WINDOWS 8 * */ public class ExcelDateReader { public static void main(String[] args) throws FileNotFoundException, IOException { writeIntoExcel("birthdays.xls"); readFromExcel("birthdays.xls"); } /** * Java method to read dates from Excel file in Java. * This method read value from .XLS file, which is an OLE * format. * * @param file * @throws IOException */ public static void readFromExcel(String file) throws IOException{ HSSFWorkbook myExcelBook = new HSSFWorkbook(new FileInputStream(file)); HSSFSheet myExcelSheet = myExcelBook.getSheet("Birthdays"); HSSFRow row = myExcelSheet.getRow(0); if(row.getCell(0).getCellType() == HSSFCell.CELL_TYPE_STRING){ String name = row.getCell(0).getStringCellValue(); System.out.println("name : " + name); } if(row.getCell(1).getCellType() == HSSFCell.CELL_TYPE_NUMERIC){ Date birthdate = row.getCell(1).getDateCellValue(); System.out.println("birthdate :" + birthdate); } myExcelBook.close(); } /** * Java method to write dates from Excel file in Java. * This method write value into .XLS file in Java. * @param file, name of excel file to write. * @throws IOException * @throws FileNotFoundException */ @SuppressWarnings("deprecation") public static void writeIntoExcel(String file) throws FileNotFoundException, IOException{ Workbook book = new HSSFWorkbook(); Sheet sheet = book.createSheet("Birthdays"); // first row start with zero Row row = sheet.createRow(0); // we will write name and birthdates in two columns // name will be String and birthday would be Date // formatted as dd.mm.yyyy Cell name = row.createCell(0); name.setCellValue("John"); Cell birthdate = row.createCell(1); // steps to format a cell to display date value in Excel // 1. Create a DataFormat // 2. Create a CellStyle // 3. Set format into CellStyle // 4. Set CellStyle into Cell // 5. Write java.util.Date into Cell DataFormat format = book.createDataFormat(); CellStyle dateStyle = book.createCellStyle(); dateStyle.setDataFormat(format.getFormat("dd.mm.yyyy")); birthdate.setCellStyle(dateStyle); // It's very trick method, deprecated, don't use // year is from 1900, month starts with zero birthdate.setCellValue(new Date(110, 10, 10)); // auto-resizing columns sheet.autoSizeColumn(1); // Now, its time to write content of Excel into File book.write(new FileOutputStream(file)); book.close(); } } Output name : John birthdate :Wed Nov 10 00:00:00 GMT+08:00 2010
In our program, we have first created an excel file with String and date columns and later read from the same file and displayed the values into the console. Now let's verify output of this program. It's correctly display the date value, though not formatted, which means the excel file was created successfully and later Java was able to read from it.
If you look at your Eclipse project directory, you will find birthdays.xls file created there, if you open that with Microsoft Excel or any Open Office editor, you will see the following output.
This is because I haven't included sheet.autoSizeColumn(1) method call in first run and since column width is not enough to display the date in requested format e.g. dd.mm.yyyy it just displays ######.
This is because I haven't included sheet.autoSizeColumn(1) method call in first run and since column width is not enough to display the date in requested format e.g. dd.mm.yyyy it just displays ######.
In order to solve this problem of date not displaying properly, all you need to do is enable autosizing of columns in Excel by calling sheet.autoSizeColumn(1) method, where column index is the column you want to resize automatically.
If you run the program again with that code, you can see the date values properly formatted and fitted in requested column, as shown below
Apache POI Example to read XLSX file in Java
Reading and writing into new excel file format XLSX is also the same, all you need to do is include poi-ooxml.jar and replace all HSFF classes with XSSF classes e.g. instead of using HSSFWorkbook, use XSSFWorkbook, instead of using HSFFSheet use XSSFSheet, instead of using HSSFRow use XSSFRow and instead of using HSSFCell just use XSSFCell class.Rest of the code and steps will be same. In following Java program, I will show you how to read XLSX file in Java. In this program also we are first creating an excel file and writing string and date values into it and later reading from the same excel file and displaying data into the console, only difference this time would be instead of creating an XLS file, our program will create an XLSX file.
Once you run this program in your Eclipse IDE, you can see the birthdays.xlsx file created in your Eclipse Project directory, as shown below :
here is our java program to read XLSX files using Apache POI library.
That's all about how to read and write from Excel file in Java. You have now learned how to read and write both String and Date from XLS as well as XLSX files in Java. You can do a lot more using the Apache POI library but this guide will help you learn and quickly use this library. Once again I suggest to use Maven for including POI dependency and if you are downloading JAR, make sure you download transitive dependency e.g. xmlbeans.
here is our java program to read XLSX files using Apache POI library.
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.DataFormat; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * This program read date values from XLSX file in Java using Apache POI. * * @author WINDOWS 8 * */ public class ExcelDateReader { public static void main(String[] args) throws FileNotFoundException, IOException { writeIntoExcel("birthdays.xlsx"); readFromExcel("birthdays.xlsx"); } public static void readFromExcel(String file) throws IOException{ XSSFWorkbook myExcelBook = new XSSFWorkbook(new FileInputStream(file)); XSSFSheet myExcelSheet = myExcelBook.getSheet("Birthdays"); XSSFRow row = myExcelSheet.getRow(0); if(row.getCell(0).getCellType() == HSSFCell.CELL_TYPE_STRING){ String name = row.getCell(0).getStringCellValue(); System.out.println("NAME : " + name); } if(row.getCell(1).getCellType() == HSSFCell.CELL_TYPE_NUMERIC){ Date birthdate = row.getCell(1).getDateCellValue(); System.out.println("DOB :" + birthdate); } myExcelBook.close(); } @SuppressWarnings("deprecation") public static void writeIntoExcel(String file) throws FileNotFoundException, IOException{ Workbook book = new XSSFWorkbook(); Sheet sheet = book.createSheet("Birthdays"); Row row = sheet.createRow(0); Cell name = row.createCell(0); name.setCellValue("Gokul"); Cell birthdate = row.createCell(1); DataFormat format = book.createDataFormat(); CellStyle dateStyle = book.createCellStyle(); dateStyle.setDataFormat(format.getFormat("dd.mm.yyyy")); birthdate.setCellStyle(dateStyle); birthdate.setCellValue(new Date(115, 10, 10)); sheet.autoSizeColumn(1); book.write(new FileOutputStream(file)); book.close(); } } NAME : Gokul DOB :Tue Nov 10 00:00:00 GMT+08:00 2015
That's all about how to read and write from Excel file in Java. You have now learned how to read and write both String and Date from XLS as well as XLSX files in Java. You can do a lot more using the Apache POI library but this guide will help you learn and quickly use this library. Once again I suggest to use Maven for including POI dependency and if you are downloading JAR, make sure you download transitive dependency e.g. xmlbeans.
What is difference between reading or writing character data e.g. String over numberi data e.g. numbers and date and time? Do we need to do anything special to format date and numbers in a particular format? does Apache APOI provides support for such formatting?
ReplyDeleteDoes POI allow you to open a password protected XLSX file in Java?
ReplyDeleteYes, you can open a password protected xlsx file in Java by using Apache POI API, it provides encryption decryption support out-of-box. You can further read and write content.
ReplyDeleteYou can use org.apache.poi.hssf.record.crypt to open password protected HSSF i.e. XLS file and org.apache.poi.poifs.crypt to open password protected XSSF or XLSX file.
don't forget to provide password by calling org.apache.poi.hssf.record.crypto.Biff8EncryptionKey.setCurrentUserPassword(password); method.
See here to learn more about how to open and read/write password protected xls and and xlsx file in Java using POI
I get "cannot find symbol, symbol: method createSheet(String)"
ReplyDeleteHi, I am trying to read number of rows from xls file with jxl api. when i open the excel file manually, i get the below error
ReplyDeleteThe file you are trying to open, 'file.xls', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?
How to handle the above error with java code ??
@Balu, it seems blogger eats your error due to HTML tags on it. Can you please post the error again after encoding HTML or XML tags e.g. < and >
ReplyDeletePaul, you are right. I have told the devopers the same. Thank you for the reply
ReplyDeleteHi,
ReplyDeleteIs there a way to read and write into excel real time. Eg. I pick an agency number from an excel, go a web page and extract some content and then need to paste that real-time into the excel in an adjacent cell and then iterate to the next agency id. Currently I am able to write the info into excel only once all info is extracted. Can this be done real time?
How to can i compare values values which are positive and negative and are present in 200+ rows and if the difference between those numbers are zero, then it should give me "matched" as output
ReplyDeleteunable to save as the xlsx file by using the apachi POI Objects and other java logic and please help on this that really great appreciation. Regards Rama
ReplyDeleteCan we use HSSFCELL to read the data from XLSV file, in above program you have used .
ReplyDelete@Balu regarding the "The file you are trying to open, 'file.xls', is in a different format than specified by the file extension.", this happens when your ".xls" file is written in some different formant, for example it could be XLSX, HTML, TAB, etc.
ReplyDeleteYou need to detect what format it is and read it accordingly, for example:
- If it's XLSX then it will have ZIP signature.
- If it's HTML then it will have "DOCTYPE", "HTML" or "BODY" element.
- If it's TAB then it will have tab characters.
- Etc...
So basically in that case you'll need to handle all spreadsheet formats, and for that I use the following:
https://www.gemboxsoftware.com/spreadsheet-java/examples/open-read-excel-file-in-java/401
That "ExcelFile.load" method can handle all kinds of spreadsheets in the same way.
Can we search in xlsx excel file through Apache POI?
ReplyDeleteIf yes please tell me!!