1.准备工作
导入apache-poi相关jar包:
gradle:
compile group: 'org.apache.poi', name: 'poi-ooxml', version: '3.15'
maven:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
成功导入的话应该是这个样子的(三个jar包都是3.15版本的):
这里如果用gradle导入jar包可能会有个问题,就是导入的apache-poi这个jar包可能是3.11版本
初步分析(瞎扯)可能是apache-poi-ooxml-3.15中引用了这个3.11的jar(通过查看依赖树发现它把我需要的3.15莫名其妙变成了3.11,可能是重复依赖,实际上如果直接用3.11的版本会报错,两个版本是不兼容的) 遇到这种问题就需要加一条了:
compile group: 'org.apache.poi', name: 'poi', version: '3.15'
compile group: 'org.apache.poi', name: 'poi-ooxml', version: '3.15'
然后就可以开心的写代码了~
2.基本操作简介
*以XSSF开头的类名适用于Microsoft Excel 2007及以后的文件(*.xlsx)
*以XSSF 开头的则适用于更早版本的Excel(*.xls)的文件
*对一个Excel文件进行操作使用的是*Workbook
*对Excel中Sheet操作使用的是Worksheet
* Cell表示Excel中的一个小格子
3.代码示例
写入操作:
package com.spring.framework.kowalski;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Created by Kowalski on 2017/4/27
* Updated by Kowalski on 2017/4/27
*/
public class WriteExcel {
public static void main(String[] args) {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("这是Sheet1");
Object[][] dataTypes = {
{"merchantId", "storeId", "orderNo"},
{1129, "1232", "68891704250000006278"},
{1130, "1121", "68891704250000006278"},
};
int rowNum = 0;
System.out.println("start======================");
for (Object[] dataType : dataTypes) {
Row row = sheet.createRow(rowNum++);
int colNum = 0;
for (Object field : dataType) {
Cell cell = row.createCell(colNum++);
if (field instanceof String) {
cell.setCellValue((String) field);
} else if (field instanceof Integer) {
cell.setCellValue((Integer) field);
}
}
}
try {
FileOutputStream outputStream = new FileOutputStream("D:/POIExcel.xlsx");
workbook.write(outputStream);
workbook.close();
System.out.println("end======================");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
不需要提前新建文件,但需要解除Excel文件的占用(比如正在被打开),运行成功后Excel中结果:
细心的同学应该能够发现除了merchantId其余的左上角都有个小三角形,这说明该数字在Excel中是以文本形式存储的,该方式能够避免Excel对数字类型做特殊处理~
读取操作:
package com.spring.framework.kowalski;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
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;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
/**
* Created by Kowalski on 2017/4/27
* Updated by Kowalski on 2017/4/27
*/
public class ReadExcel {
public static void main(String[] args) {
try {
Workbook workbook = new XSSFWorkbook("D:/POIExcel.xlsx");
Sheet dataTypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = dataTypeSheet.iterator();
while(iterator.hasNext()){
Row currentRow = iterator.next();
Iterator<Cell> cellIterator = currentRow.iterator();
while(cellIterator.hasNext()){
Cell currentCell = cellIterator.next();
if(currentCell.getCellTypeEnum() == CellType.STRING){
System.out.print(currentCell.getStringCellValue() + "===");
} else if(currentCell.getCellTypeEnum() == CellType.NUMERIC){
System.out.print(currentCell.getNumericCellValue() + "===");
}
}
System.out.println();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
读取结果:
读取操作Excel文件必须提前存在
这次学习了读写Excel文件,就是为了在做参数化单元测试的时候使用Excel去管理测试用例http://blog.csdn.net/qq_32193151/article/details/60135396,等着吧~就快写出来了~~~