Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能。该篇是介绍poi基本的读写数据功能。
本文操作的是xls格式的Excel,使用的jar包是:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
如果操作的是xlsx格式的Excel,引入该jar包:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>xxx</version>
</dependency>
- 数据写入Excel
package com.originexcel;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/**
* Created by dell on 2017/9/29.
*/
public class CreateSheet {
public static void main(String[] args) throws IOException {
/**
* 思路:
* 1、创建工作簿
* 2、创建sheet页(可设置sheet页名)
* 3、创建行
* 4、取数据:先取集合,再取对象,再取属性,并将数据写入到工作簿
* 5、创建文件,将工作簿写入文件
*/
XSSFWorkbook workbook = new XSSFWorkbook(); // 创建工作簿
XSSFSheet sheet = workbook.createSheet("mysheet");// sheet页名称
XSSFRow row ; // 行
Map<String,Object[]> object = new TreeMap<>();
object.put("1",new Object[]{"name","age","sex"});
object.put("2",new Object[]{"admin01","15","女"});
object.put("3",new Object[]{"admin02","15","女"});
object.put("4",new Object[]{"admin03","15","女"});
object.put("5",new Object[]{"admin04","15","女"});
object.put("6",new Object[]{"admin05","15","女"});
Set<String> keys = object.keySet();
int rowid = 0;
for (String key:keys) {
row = sheet.createRow(rowid++);
Object[] objects = object.get(key);
int cellid = 0;
for (Object obj : objects) {
Cell cell = row.createCell(cellid++); // 单元格
cell.setCellValue((String)obj);
}
}
FileOutputStream outputStream = new FileOutputStream(new File("test.xls"));
workbook.write(outputStream);
outputStream.close();
System.out.println("test.xlsx written successfully");
}
}
输出的效果是:
- 读取excel数据
package com.originexcel;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
/**
* 读取excel的数据
*
* Created by dell on 2017/9/29.
*/
public class ReadWorkBook {
static XSSFRow row;
public static void main(String[] args) throws IOException {
FileInputStream inputStream = new FileInputStream("test.xls");
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator<Row> rows = sheet.iterator();
while (rows.hasNext()){
row = (XSSFRow) rows.next();
Iterator <Cell> cellIterator = row.cellIterator();
Cell cell = cellIterator.next();
switch (cell.getCellType()){
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();
}
inputStream.close();
}
}
本文只是作为poi一个入门的demo(也是博主的处女作)。欢迎大家多多指教。