Welcome to Apache POI Tutorial. Sometimes we need to read data from Microsoft Excel Files or we need to generate reports in Excel format, mostly for Business or Finance purposes. Java doesn’t provide built-in support for working with excel files, so we need to look for open source APIs for the job. When I started the hunt for Java APIs for excel, most of the people recommended JExcel or Apache POI.

 
After further research, I found that Apache POI is the way to go for following main reasons. There are some other reasons related to advanced features but let’s not go into that much detail.
 
Backing of Apache foundation.
JExcel doesn’t support xlsx format whereas POI supports both xls and xlsx formats.
Apache POI provides stream-based processing, that is suitable for large files and requires less memory.
Apache POI
 
Apache POI provides excellent support for working with Microsoft Excel documents. Apache POI is able to handle both XLS and XLSX formats of spreadsheets.
 
 
Some important points about Apache POI API are:
 
Apache POI contains HSSF implementation for Excel ’97(-2007) file format i.e XLS.
Apache POI XSSF implementation should be used for Excel 2007 OOXML (.xlsx) file format.
Apache POI HSSF and XSSF API provides mechanisms to read, write or modify excel spreadsheets.
Apache POI also provides SXSSF API that is an extension of XSSF to work with very large excel sheets. SXSSF API requires less memory and is suitable when working with very large spreadsheets and heap memory is limited.
There are two models to choose from – event model and user model. Event model requires less memory because the excel file is read in tokens and requires processing them. User model is more object oriented and easy to use and we will use this in our examples.
Apache POI provides excellent support for additional excel features such as working with Formulas, creating cell styles by filling colors and borders, fonts, headers and footers, data validations, images, hyperlinks etc.
Apache POI Maven Dependencies
 
If you are using maven, add below Apache POI dependencies.
 
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10-FINAL</version>
</dependency>
Current version of Apache POI is 3.10-FINAL. If you are having standalone java application, include jars from below image.
 
 
Apache POI Jars, Apache POI, Apache POI example, apache poi tutorial
 
Apache POI Example – Read Excel File
 
Let’s say we have an excel file “Sample.xlsx” with two sheets and having data like below image. We want to read the excel file and create the list of Countries. Sheet1 has some additional data, that we will ignore while parsing it.
 
Java Read Excel File, Apache POI, Apache POI Example, Apache POI tutorial
 
 
Java Read Excel File, Apache POI, Apache POI Example, Apache POI tutorial
 
Our Country java bean code is:
 
Country.java
 
package com.journaldev.excel.read;
 
public class Country {
 
private String name;
private String shortCode;
 
public Country(String n, String c){
this.name=n;
this.shortCode=c;
}
 
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getShortCode() {
return shortCode;
}
public void setShortCode(String shortCode) {
this.shortCode = shortCode;
}
 
@Override
public String toString(){
return name + "::" + shortCode;
}
 
}
Apache POI example program to read excel file to the list of countries looks like below.
 
ReadExcelFileToList.java
 
package com.journaldev.excel.read;
 
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.XSSFWorkbook;
 
public class ReadExcelFileToList {
 
public static List<Country> readExcelData(String fileName) {
List<Country> countriesList = new ArrayList<Country>();
 
try {
//Create the input stream from the xlsx/xls file
FileInputStream fis = new FileInputStream(fileName);
 
//Create Workbook instance for xlsx/xls file input stream
Workbook workbook = null;
if(fileName.toLowerCase().endsWith("xlsx")){
workbook = new XSSFWorkbook(fis);
}else if(fileName.toLowerCase().endsWith("xls")){
workbook = new HSSFWorkbook(fis);
}
 
//Get the number of sheets in the xlsx file
int numberOfSheets = workbook.getNumberOfSheets();
 
//loop through each of the sheets
for(int i=0; i < numberOfSheets; i++){
 
//Get the nth sheet from the workbook
Sheet sheet = workbook.getSheetAt(i);
 
//every sheet has rows, iterate over them
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) 
        {
String name = "";
String shortCode = "";
 
//Get the row object
Row row = rowIterator.next();
 
//Every row has columns, get the column iterator and iterate over them
Iterator<Cell> cellIterator = row.cellIterator();
             
            while (cellIterator.hasNext()) 
            {
             //Get the Cell object
             Cell cell = cellIterator.next();
            
             //check the cell type and process accordingly
             switch(cell.getCellType()){
             case Cell.CELL_TYPE_STRING:
             if(shortCode.equalsIgnoreCase("")){
             shortCode = cell.getStringCellValue().trim();
             }else if(name.equalsIgnoreCase("")){
             //2nd column
             name = cell.getStringCellValue().trim();
             }else{
             //random data, leave it
             System.out.println("Random data::"+cell.getStringCellValue());
             }
             break;
             case Cell.CELL_TYPE_NUMERIC:
             System.out.println("Random data::"+cell.getNumericCellValue());
             }
            } //end of cell iterator
            Country c = new Country(name, shortCode);
            countriesList.add(c);
        } //end of rows iterator
 
 
} //end of sheets for loop
 
//close file input stream
fis.close();
 
} catch (IOException e) {
e.printStackTrace();
}
 
return countriesList;
}
 
public static void main(String args[]){
List<Country> list = readExcelData("Sample.xlsx");
System.out.println("Country List\n"+list);
}
 
}
The program is very easy to understand and contains following steps:
 
 
Create Workbook instance based on the file type. XSSFWorkbook for xlsx format and HSSFWorkbook for xls format. Notice that we could have created a wrapper class with factory pattern to get the workbook instance based on the file name.
Use workbook getNumberOfSheets() to get the number of sheets and then use for loop to parse each of the sheets. Get the Sheet instance using getSheetAt(int i) method.
Get Row iterator and then Cell iterator to get the Cell object. Apache POI is using iterator pattern here.
Use switch-case to read the type of Cell and the process it accordingly.
Now when we run above Apache POI example program, it produces following output on console.
 
Random data::1.0
Random data::2.0
Random data::3.0
Random data::4.0
Country List
[India::IND, Afghanistan::AFG, United States of America::USA, Anguilla::AIA, 
Denmark ::DNK, Dominican Republic ::DOM, Algeria ::DZA, Ecuador ::ECU]
Apache POI Example – Write Excel File
 
Writing excel file in apache POI is similar to reading, except that here we first create the workbook. Then set sheets, rows and cells values and use FileOutputStream to write it to file. Let’s write a simple apache POI example where we will use list of countries from the above method to save into another file in a single sheet.
 
WriteListToExcelFile.java
 
package com.journaldev.excel.read;
 
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.List;
 
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.XSSFWorkbook;
 
public class WriteListToExcelFile {
 
public static void writeCountryListToFile(String fileName, List<Country> countryList) throws Exception{
Workbook workbook = null;
 
if(fileName.endsWith("xlsx")){
workbook = new XSSFWorkbook();
}else if(fileName.endsWith("xls")){
workbook = new HSSFWorkbook();
}else{
throw new Exception("invalid file name, should be xls or xlsx");
}
 
Sheet sheet = workbook.createSheet("Countries");
 
Iterator<Country> iterator = countryList.iterator();
 
int rowIndex = 0;
while(iterator.hasNext()){
Country country = iterator.next();
Row row = sheet.createRow(rowIndex++);
Cell cell0 = row.createCell(0);
cell0.setCellValue(country.getName());
Cell cell1 = row.createCell(1);
cell1.setCellValue(country.getShortCode());
}
 
//lets write the excel data to file now
FileOutputStream fos = new FileOutputStream(fileName);
workbook.write(fos);
fos.close();
System.out.println(fileName + " written successfully");
}
 
public static void main(String args[]) throws Exception{
List<Country> list = ReadExcelFileToList.readExcelData("Sample.xlsx");
WriteListToExcelFile.writeCountryListToFile("Countries.xls", list);
}
}
When I execute above apache POI example program, the excel file generated looks like below image.
 
Java Write Excel File, Apache POI, Apache POI Example, Apache POI tutorial
 
Apache POI Example – Read Excel Formula
 
Sometimes we need to handle complex excel files with formulas, let’s see a simple apache POI example to read the formula of a cell with it’s value.
 
Java Excel Read Formula, Apache POI, Apache POI Example, Apache POI tutorial
 
ReadExcelFormula.java
 
package com.journaldev.excel.read;
 
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
 
import org.apache.poi.ss.usermodel.Cell;
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.XSSFWorkbook;
 
public class ReadExcelFormula {
 
public static void readExcelFormula(String fileName) throws IOException{
 
FileInputStream fis = new FileInputStream(fileName);
 
//assuming xlsx file
Workbook workbook = new XSSFWorkbook(fis);
Sheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) 
        {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
            
            while (cellIterator.hasNext()) 
            {
             Cell cell = cellIterator.next();
             switch(cell.getCellType()){
             case Cell.CELL_TYPE_NUMERIC:
             System.out.println(cell.getNumericCellValue());
             break;
             case Cell.CELL_TYPE_FORMULA:
             System.out.println("Cell Formula="+cell.getCellFormula());
             System.out.println("Cell Formula Result Type="+cell.getCachedFormulaResultType());
             if(cell.getCachedFormulaResultType() == Cell.CELL_TYPE_NUMERIC){
             System.out.println("Formula Value="+cell.getNumericCellValue());
             }
             }
            }
        }
}
 
public static void main(String args[]) throws IOException {
readExcelFormula("FormulaMultiply.xlsx");
}
}
When we execute above apache poi example program, we get following output.
 
1.0
2.0
3.0
4.0
Cell Formula=A1*A2*A3*A4
Cell Formula Result Type=0
Formula Value=24.0
Apache POI Example – Excel Write Formula
 
Sometimes, we need to do some calculations and then write the cell values. We can use the excel formulas to do this calculation and that will make it more accurate because values will change if the cell values used in calculations are changed.
 
Let’s see a simple example to write excel file with formulas using apache poi api.
 
WriteExcelWithFormula.java
 
package com.journaldev.excel.read;
 
import java.io.FileOutputStream;
import java.io.IOException;
 
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.XSSFWorkbook;
 
public class WriteExcelWithFormula {
 
public static void writeExcelWithFormula(String fileName) throws IOException{
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Numbers");
Row row = sheet.createRow(0);
row.createCell(0).setCellValue(10);
row.createCell(1).setCellValue(20);
row.createCell(2).setCellValue(30);
//set formula cell
row.createCell(3).setCellFormula("A1*B1*C1");
 
//lets write to file
FileOutputStream fos = new FileOutputStream(fileName);
workbook.write(fos);
fos.close();
System.out.println(fileName + " written successfully");
}
 
public static void main(String[] args) throws IOException {
writeExcelWithFormula("Formulas.xlsx");
}
}
The excel file produced with above Apache POI API example program looks like below image.
 
java write excel formula, apache poi, apache poi tutorial, apache poi example
 
That’s all on Apache POI tutorial for working with excel files, look into Apache POI classes methods to learn more features of it.
 
References: Apache POI Developers Guide

转:Apache POI Tutorial的更多相关文章

  1. Read / Write Excel file in Java using Apache POI

    Read / Write Excel file in Java using Apache POI 2014-04-18 BY DINESH LEAVE A COMMENT About a year o ...

  2. 简单使用Apache POI

    Apache POI是一个纯Java编写用来操作Microsoft Office的框架,最常见的应用是让服务器后台按照特定的数据生成Excel表格提供给用户实用.前段时间因为项目的需要被大量使用,使用 ...

  3. 使用maven引入Apache poi jar包

    maven构建的项目-->pom.xml文件 eclipse提供Dependencies直接添加依赖jar包的工具:直接搜索poi以及poi-ooxml即可,maven会自动依赖需要的jar包: ...

  4. apache poi导出excel报表

    Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能.POI为"P ...

  5. Apache POI 实现对 Excel 文件读写

    1. Apache POI 简介 Apache POI是Apache软件基金会的开放源码函式库. 提供API给Java应用程序对Microsoft Office格式档案读和写的功能. 老外起名字总是很 ...

  6. APACHE POI教程 --java应用程序用POI与Excel交互

    POI报表 --用POI与Excel交互 AURISOFT 第一章 POI简介 --Jakata Poi HSSF:纯java的Excel解决方案 在我们实际的开发中,表现层的解决方案虽然有多样,但是 ...

  7. apache poi 生成excel

    ExcelBuilder.Java package com.coracle.yk.xmanager.util.poi; import com.coracle.yk.xframework.util.Bl ...

  8. weblogic 12c下jxls导出excel报错Could not initialize class org.apache.poi.xssf.usermodel.XSSFVMLDrawing

    周一,开发反馈weblogic 12c下jxls导出excel报错,公司环境和UAT环境均报错,看日志如下: 2016-06-08 09:16:55,825 ERROR org.jxls.util.T ...

  9. [转]How to insert a row between two rows in an existing excel with HSSF (Apache POI)

    本文转自:http://stackoverflow.com/questions/5785724/how-to-insert-a-row-between-two-rows-in-an-existing- ...

随机推荐

  1. page fault rate

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION A program computes th ...

  2. 设置session失效时间

    以X5部署在Tomcat上为例,说明如何设置session失效时间. 可以设置session失效时间的地点有三处,分别是 1.BusinessServer的session设置 \runtime\Bus ...

  3. 回退(pop&present)到根页面(根控制器)的方法,很不错~

    http://blog.csdn.net/assholeu/article/details/45897035

  4. MYSQL 中常用日期时间函数使用

    MySQL Date 函数 下面的表格列出了 MySQL 中最重要的内建日期函数: 函数 描述 NOW() 返回当前的日期和时间 CURDATE() 返回当前的日期 CURTIME() 返回当前的时间 ...

  5. SIP学习(实例详解)

    本文摘自:http://blog.chinaunix.net/uid-20655530-id-1589483.html 学习 SIP 协议最快捷的方法是通过范例来学习, 找到了一个完整的呼叫流程,le ...

  6. android游戏动画特效的一些处理

    游戏中避免不了需要一些动画特效的处理,有些是不方便用美术或者美工来处理的,那么就由我们程序猿来搞了.直接进入正题. 首先是Animation,Animation针对view,可以控制view的位移.缩 ...

  7. ArcGIS API for Silverlight 点沿着线流动

    原文:ArcGIS API for Silverlight 点沿着线流动 概述 前段时间做了一个项目,要求是有一些电力输送线,电力输送线或者石油管道都是有流动方向的,用户想做一个动态效果来模拟电力的输 ...

  8. sublime text2 操作及插件

    sublime text2 1. 文件快速导航: 这是sublime上面很好用的功能之一,ctrl+p可以调出窗口,菜单上的解释是gotoanythings ,确实如其所言,调出窗口后,直接输入关键字 ...

  9. Selenium2学习-015-WebUI自动化实战实例-013-通过 URL 关闭多余的已开浏览器窗口

    在日常的 WebUI 自动化测试脚本执行的过程中,经常会打开不同的网页,进行相应的操作,此时可能会打开很多的网页,当打开的网页过多时,无效的网页资源对运行脚本的机器造成了过多无效的资源浪费,因而在日常 ...

  10. jquery 点击空白处隐藏div元素

    <style type="text/css">.pop {display:none;width: 200px;height: 130px;background: #08 ...