如何用Apache POI操作Excel文件

如题所述

POI是Apache下的一个项目,是用Java编写的开源框架,提供API供开发者直接操作Microsoft Office(Excel,Word,PowerPoint...)

POI为我们带来了什么?
在很多的企业当中,储蓄数据是使用Excel文档的,因为Excel文档的格式方便,也能套用公式,而企业程序是存储在数据库当中,这样就需要一种两者之间互相转换的方法,当企业刚开始使用信息化的管理系统时,也需要将Excel的数据录入到程序当中,这种需求是非常普遍的.

POI使用:
首先增加Maven的依赖

<!-- POI核心依赖 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.8</version>
</dependency>
<!-- 为POI支持Office Open XML -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.8</version>
</dependency>
<!-- 支持Word文档的操作 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.8</version>
</dependency>

以下为操作Excel的测试类

package com.accentrix.ray;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
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.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Before;
import org.junit.Test;

public class TestExcel {

private Workbook workbook;

/*
* 由于Excel当中的单元格Cell存在类型,若获取类型错误 就会产生错误,
* 所以通过此方法将Cell内容全部转换为String类型
*/
private String getCellValue(Cell cell) {
String str = null;
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BLANK:
str = "";
break;
case Cell.CELL_TYPE_BOOLEAN:
str = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
str = String.valueOf(cell.getCellFormula());
break;
case Cell.CELL_TYPE_NUMERIC:
str = String.valueOf(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:
str = String.valueOf(cell.getStringCellValue());
break;
default:
str = null;
break;
}
return str;
}

@Before
public void setUp() throws InvalidFormatException, IOException {
// 加载excel文件,自动判断是HSSF还是XSSF
workbook = WorkbookFactory.create(new File("E:/aaa.xls"));
}

/*
* 读取一个已存在的Excel
*/
@Test
public void testReadExcel() throws InvalidFormatException, IOException {

// 获取第一个工作目录,下标从0开始
Sheet sheet = workbook.getSheetAt(0);

// 获取该工作目录最后一行的行数
int lastRowNum = sheet.getLastRowNum();

for (int i = 0; i < lastRowNum; i++) {

// 获取下标为i的行
Row row = sheet.getRow(i);

// 获取该行单元格个数
int lastCellNum = row.getLastCellNum();

for (int j = 0; j < lastCellNum; j++) {

// 获取下标为j的单元格
Cell cell = row.getCell(j);

// 调用获取方法
String cellValue = this.getCellValue(cell);
}
}
}

/*
* 使用Foreach方式读取Excel
*/
@Test
public void testForeachReadExcel() {
// 根据sheet的名字获取
Sheet sheet = workbook.getSheet("test");

// 处了上面testReadExcel的方式读取以外,还支持foreach的方式读取
for (Row row : sheet) {
for (Cell cell : row) {
String cellValue = this.getCellValue(cell);
System.out.println(cellValue);
}
}
}

/*
* 创建简单的Excel
*/
@Test
public void testWriteExcel() throws IOException {
// 创建一个XSSF的Excel文件
workbook = new XSSFWorkbook();
FileOutputStream fos = new FileOutputStream("E:/test.xlsx");

// 创建名称为test的工作目录
Sheet sheet = workbook.createSheet("test");

/*
* 创建1个10行x10列的工作目录
*/
for (int i = 0; i < 10; i++) {
// 创建一行
Row row = sheet.createRow(i);
for (int j = 0; j < 10; j++) {
// 创建一个单元格
Cell cell = row.createCell(j);
// 设置单元格value
cell.setCellValue("test");

// 此处为设置Excel的样式,设置单元格内容居中,
// 但这样设置方式并不常用,请留意下面的方法
CellStyle cs = workbook.createCellStyle();
cs.setAlignment(CellStyle.ALIGN_CENTER);
cell.setCellStyle(cs);

}
}

// 将Excel写出到文件流
workbook.write(fos);
}
温馨提示:答案为网友推荐,仅供参考
相似回答