Read / Write Excel file in Java using Apache POI
- By Viral Patel on November 28, 2012
Apache POI is a powerful Java library to work with different Microsoft Office file formats such as Excel, Power point, Visio, MS Word etc. The name POI was originally an acronym for Poor Obfuscation Implementation, referring humorously to the fact that the file formats seemed to be deliberately obfuscated, but poorly, since they were successfully reverse-engineered.
In this tutorial we will use Apache POI library to perform different functions on Microsoft Excel spreadsheet.
Let’s get started.
Tools & Technologies:
- Java JDK 1.5 or above
- Apache POI library v3.8 or above (download)
- Eclipse 3.2 above (optional)
1. Add Apache POI dependency
Make sure to include apache poi jar file to your project. If your project uses Maven as dependency management, add following in your Pom.xml file.
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.8</version> </dependency>
If you are not using Maven then you can directly add required JAR files in your classpath.
- Download
poi-2.5.1.jar(or in this case 3.8) jar file. - Include this file in your projects class path.
- Create new java project in eclipse with auto generated main function.
2. Read Excel File
To read an excel file, Apache POI provides certain easy-to-use APIs. In below sample code we use different classes from POI library to read content of cell from excel file. This is for quick reference.
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
//..
FileInputStream file = new FileInputStream(new File("C:\\test.xls"));
//Get the workbook instance for XLS file
HSSFWorkbook workbook = new HSSFWorkbook(file);
//Get first sheet from the workbook
HSSFSheet sheet = workbook.getSheetAt(0);
//Get iterator to all the rows in current sheet
Iterator<Row> rowIterator = sheet.iterator();
//Get iterator to all cells of current row
Iterator<Cell> cellIterator = row.cellIterator();
Notice how each class in POI library starts with HSSF prefix! e.g. HSSFWorkbook, HSSFSheet etc. HSSF stands for Horrible SpreadSheet Format! I’m not kidding.. It really is.
Similar to HSSF, POI has different prefix for other file formats too:
- HSSF (Horrible SpreadSheet Format) – reads and writes Microsoft Excel (XLS) format files.
- XSSF (XML SpreadSheet Format) – reads and writes Office Open XML (XLSX) format files.
- HPSF (Horrible Property Set Format) – reads “Document Summary” information from Microsoft Office files.
- HWPF (Horrible Word Processor Format) – aims to read and write Microsoft Word 97 (DOC) format files.
- HSLF (Horrible Slide Layout Format) – a pure Java implementation for Microsoft PowerPoint files.
- HDGF (Horrible DiaGram Format) – an initial pure Java implementation for Microsoft Visio binary files.
- HPBF (Horrible PuBlisher Format) – a pure Java implementation for Microsoft Publisher files.
- HSMF (Horrible Stupid Mail Format) – a pure Java implementation for Microsoft Outlook MSG files
- DDF (Dreadful Drawing Format) – a package for decoding the Microsoft Office Drawing format.
Consider a sample excel file:
test.xls

We will read above xls file using Apache POI and prints the data.
try {
FileInputStream file = new FileInputStream(new File("C:\\test.xls"));
//Get the workbook instance for XLS file
HSSFWorkbook workbook = new HSSFWorkbook(file);
//Get first sheet from the workbook
HSSFSheet sheet = workbook.getSheetAt(0);
//Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while(rowIterator.hasNext()) {
Row row = rowIterator.next();
//For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch(cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t\t");
break;
}
}
System.out.println("");
}
file.close();
FileOutputStream out =
new FileOutputStream(new File("C:\\test.xls"));
workbook.write(out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
The above code is self explanatory. It read the sheet from workbook and iterate through each row and cell to print its values. Just note how we use different methods like getBooleanCellValue, getNumericCellValue etc to read cell value. Before reading a cell content, we need to first determine its type using method cell.getCellType() and then call appropriate method to read content.
Output:
Emp Id Name Salary 1.0 John 2000000.0 2.0 Dean 420000.0 3.0 Sam 280000.0 4.0 Cass 6000000.0
3. Create New Excel File
Let us create a new excel file and write data in it. Following is the API which we will use for this purpose.
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
//..
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sample sheet");
//Create a new row in current sheet
Row row = sheet.createRow(0);
//Create a new cell in current row
Cell cell = row.createCell(0);
//Set value to new value
cell.setCellValue("Blahblah");
Below is the complete code that writes a new excel with dummy data:
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sample sheet");
Map<String, Object[]> data = new HashMap<String, Object[]>();
data.put("1", new Object[] {"Emp No.", "Name", "Salary"});
data.put("2", new Object[] {1d, "John", 1500000d});
data.put("3", new Object[] {2d, "Sam", 800000d});
data.put("4", new Object[] {3d, "Dean", 700000d});
Set<String> keyset = data.keySet();
int rownum = 0;
for (String key : keyset) {
Row row = sheet.createRow(rownum++);
Object [] objArr = data.get(key);
int cellnum = 0;
for (Object obj : objArr) {
Cell cell = row.createCell(cellnum++);
if(obj instanceof Date)
cell.setCellValue((Date)obj);
else if(obj instanceof Boolean)
cell.setCellValue((Boolean)obj);
else if(obj instanceof String)
cell.setCellValue((String)obj);
else if(obj instanceof Double)
cell.setCellValue((Double)obj);
}
}
try {
FileOutputStream out =
new FileOutputStream(new File("C:\\new.xls"));
workbook.write(out);
out.close();
System.out.println("Excel written successfully..");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Output: new.xls

4. Update Existing Excel File
Updating an existing excel file is straight forward. Open the excel using different API that we discussed above and set the cell’s value. One thing we need to note here is that we can update the excel file only when we close it first.
update.xls

Following Java code read the above excel file and doubles the salary of each employee:
try {
FileInputStream file = new FileInputStream(new File("C:\\update.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheetAt(0);
Cell cell = null;
//Update the value of cell
cell = sheet.getRow(1).getCell(2);
cell.setCellValue(cell.getNumericCellValue() * 2);
cell = sheet.getRow(2).getCell(2);
cell.setCellValue(cell.getNumericCellValue() * 2);
cell = sheet.getRow(3).getCell(2);
cell.setCellValue(cell.getNumericCellValue() * 2);
file.close();
FileOutputStream outFile =new FileOutputStream(new File("C:\\update.xls"));
workbook.write(outFile);
outFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Steps to update excel file will be:
- Open excel file in input mode (inputstream)
- Use POI API and read the excel content
- Update cell’s value using different
setCellValuemethods. - Close the excel input file (inputstream)
- Open same excel file in output mode (outputstream)
- Write content of updated workbook in output file
- Close output excel file
Output: update.xls

5. Adding Formulas
Apache POI provides API to add excel formulas to cell programmatically. Following method that comes handy for this:
cell.setCellFormula("someformula")
For example:
cell.setCellFormula("A2*B2*C5")
//or
cell.setCellFormula("SUM(A1:A7)")
Note: Formula string should not start with equal sign (=)
Thus, following is incorrect way of adding formula:
cell.setCellFormula("=A2*B2*C5") //Ops! Won't work
The above code will throw:
org.apache.poi.ss.formula.FormulaParseException: The specified formula '=A2*B2*C5' starts with an equals sign which is not allowed.
Following Java code creates a new excel sheet which calculates Simple Interest. It defines Principal amount, Rate of Interest and Tenure. We add an excel formula to calculate interest.
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Calculate Simple Interest");
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("Pricipal Amount (P)");
header.createCell(1).setCellValue("Rate of Interest (r)");
header.createCell(2).setCellValue("Tenure (t)");
header.createCell(3).setCellValue("Interest (P r t)");
Row dataRow = sheet.createRow(1);
dataRow.createCell(0).setCellValue(14500d);
dataRow.createCell(1).setCellValue(9.25);
dataRow.createCell(2).setCellValue(3d);
dataRow.createCell(3).setCellFormula("A2*B2*C2");
try {
FileOutputStream out =
new FileOutputStream(new File("C:\\formula.xls"));
workbook.write(out);
out.close();
System.out.println("Excel written successfully..");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Output: formula.xls

Triggering Existing Excel Formulas
In certain cases your excel file might have formula defined and you may want to trigger those formulas since you updated it using POI. Following code snippet will do the trick.
FileInputStream fis = new FileInputStream("/somepath/test.xls");
Workbook wb = new HSSFWorkbook(fis); //or new XSSFWorkbook("C:\\test.xls")
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
for(int sheetNum = 0; sheetNum < wb.getNumberOfSheets(); sheetNum++) {
Sheet sheet = wb.getSheetAt(sheetNum);
for(Row r : sheet) {
for(Cell c : r) {
if(c.getCellType() == Cell.CELL_TYPE_FORMULA) {
evaluator.evaluateFormulaCell(c);
}
}
}
}
We use FormulaEvaluator class to evaluate formula defined in each of the cell.
6. Adding Styles to Cell
Adding style to a cell is also piece of cake. Check following example which creates two new cell one with bold font and another with italic and add text to it.
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Style example");
HSSFFont font = workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle style = workbook.createCellStyle();
style.setFont(font);
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("This is bold");
cell.setCellStyle(style);
font = workbook.createFont();
font.setItalic(true);
style = workbook.createCellStyle();
style.setFont(font);
row = sheet.createRow(1);
cell = row.createCell(0);
cell.setCellValue("This is italic");
cell.setCellStyle(style);
try {
FileOutputStream out = new FileOutputStream(new File("C:\\style.xls"));
workbook.write(out);
out.close();
System.out.println("Excel written successfully..");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Output: style.xls

I hope this article is useful.
Get our Articles via Email. Enter your email address.
hi patel i just copied your Update Existing Excel File and run it in netbeans ide.when i run it shows the following error,
Exception in thread “main” java.lang.IllegalArgumentException: Sheet index (0) is out of range (0..-1)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.validateSheetIndex(HSSFWorkbook.java:429)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.getSheetAt(HSSFWorkbook.java:825)
at com.infomindz.io.ReadXlFile.updateExistingFile(ReadXlFile.java:277)
at com.infomindz.io.ReadXlFile.main(ReadXlFile.java:308)
and the code is
String path = separator + "home" + separator + "santhanam" + separator+ "Contact list.xls"; FileInputStream inStream = new FileInputStream(path); HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.getSheetAt(0); Cell cell = null; //going to update existing excel file cell = sheet.getRow(1).getCell(2); cell.setCellValue(cell.getNumericCellValue() * 2); cell = sheet.getRow(2).getCell(2); cell.setCellValue(cell.getNumericCellValue() * 2); cell = sheet.getRow(3).getCell(2); cell.setCellValue(cell.getNumericCellValue() * 2); inStream.close(); FileOutputStream out = new FileOutputStream(path); workbook.write(out); System.out.println("Updating Excell file successful");and also i checked xl file values.it is contain same value as what you gave.
if i did anything wrong pls advice me.
thanks in advance
Sir ,where i add my downloaded poi in project .can u plz tell the complete structure of the project after adding the POI……Thanks
In eclipse u can download and add in build path
like Go to Project > Properties > Java Build Path
Click and browse external jar where it is saved
If u have created a shared path for all external jars with environment variable M2_REPO then you can browse that path
C:\Users\<>\.m2\repository\
nice, u helped me. u explained in a better way.
thank u.
One should use TreeMap instead of HashMap. As there is no ordering, one can get the heading row not as the top row always.
excellent information.
I will download this api now and work on it. I wasted lot of time searching online about other alternatives but this api is awesome and easy to use.
thanks for sharing this tutorial with all of us..
Solve this error ::
The supplied spreadsheet seems to be Excel 5.0/7.0 (BIFF5) format. POI only supports BIFF8 format (from Excel versions 97/2000/XP/2003)
Hi Patel, am in a serious problem, i have a folder receiving data in excel format every day in every second. i need to read these excel files and parse to MySQL database(Apache POI am using). My program needs to go back to the same data source to update only new data in those existing excel files. I also need to generate real time graphs from this data fro the database.
Please help me.
Hi Patel,
It might be helpful if you can explain how to setup this library to some beginners
let me help you here.
You need to follow following steps to get started with above examples
1. you need to download poi-2.5.1.jar(or in this case 3.8) jar file
2. You need to include this file in your projects class path
3. Create new java project in eclipse with auto generated main function
4. just copy and paste above code or and press ctrl+Shift+o and your ready to go
Hope this helps
Amol Patil
findnetinfo.com
@Amol: I am using Maven in this tutorial as specified in Step 1. That should add required dependencies. But thanks for your suggestion. I have added these steps to above tutorial so that users who do not wish to use Maven can run the examples without getting into classpath errors. Thanks
and yes this is most helpful resource I found on internet about using these libraries thanks a ton..
for my previous comment , I was a complete newbie in reading xls and I faced problems while setting up all of it thats why explained this stuff but I found most of your readers expert here .
Your example: Read Excel File using Eclipse worked fine on windows 7 for me. The only difficulty that I’m having is getting the docs to be accessed by Eclipse. I get “Note: This element neither has attached source nor attached Javadoc and hence no Javadoc could be found”. I added the source docs in as part of the Referenced Libraries along with the class .jar file. I can access the docs separately just fine, but haven’t been able to use them in code assist.
hi your examples are simply good…
my requirement is i want to read data from one excel and i want to append it in another excel.
can you please guide me.
Im getting the following error:
org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
How do i resolve this?
The file was in .xlsx format. Converted it to .xls and now its working fine. Thank you.
How to add an image(logo) into an excel file using poi api?
Hi, I am reading table from db and storing it in excel I have more than one million records in the table. I want to store the records 1 lakh each in different excel file tabs not in the single excel file. Please let me know the procedure to get it stored in the excel with best efficient way.
Hi,
Doesn’t Apache POI read .xlsx files?
This is the error that im getting when i try to read an .xlsx file:
org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)