首先pom.xml中添加相关依赖:
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.0</version>
</dependency>
这里写了一个操作excel的测试类,有读取 excel文件 和输出 excel文件两个方法,这里没有进行字体、样式等的测试,只有最基础的读取和写入操作
package com.wyxh.bean;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
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;
public class ExcelUtils {
HSSFWorkbook wb = new HSSFWorkbook();
// create a new sheet
HSSFSheet s = wb.createSheet();
// declare a row object reference
HSSFRow r = null;
// declare a cell object reference
HSSFCell c = null;
public static void main(String[] args) throws IOException {
new ExcelUtils().getExcel();
}
/**
* 读取excel文件
*
* @throws IOException
* @throws IOException
*/
public void getExcel() throws IOException, IOException {
Workbook getwb = null;
//实例化一个工作薄,传入的参数为要读取的excel文件,可以改为参数动态传入
String file = "C:\\Users\\Administrator\\Documents\\WeChat Files\\Files\\test.xlsx";
//判断传入的文件是以 xls 还是 xlsx,湖区不同的Workbook实例
if(file.endsWith("xls")) {
getwb = new HSSFWorkbook(new FileInputStream(file));
}else if(file.endsWith("xlsx")) {
getwb = new XSSFWorkbook(new FileInputStream(file));
}
Iterator<Sheet> iterator = getwb.iterator();
while(iterator.hasNext()) {
//获取当前的工作表
Sheet next = iterator.next();
//获取工作表的一行,即:获取列名所在行
Row row = next.getRow(0);
//获取当前行的总列数
int cellnum = row.getPhysicalNumberOfCells();
//StringBuilder作为输出使用
StringBuilder str = new StringBuilder();
//遍历所有列
for(int i=0;i<cellnum;i++) {
//获取当前行每一列
Cell cell = row.getCell(i);
//获取当前列的值,追加到 StringBuilder 做输出用
str.append(cell.getStringCellValue());
str.append(" >> ");
}
System.out.println("列名:"+str.toString());
//以上部分是获取表中的第一行数据,即,每一列的名称
/*============================================================*/
//以下部分是获取每一行的数据,不包括第一行
//获取当前总行数
int rowNum = next.getLastRowNum();
//遍历所有行
for(int i = 1;i<rowNum;i++) {
//获取当前行
Row currentRow = next.getRow(i);
///获取当前行的 总列数
int cells = currentRow.getPhysicalNumberOfCells();
StringBuilder cellstr = new StringBuilder();
//遍历每一列
for(int cellNum = 0;cellNum<cells;cellNum++){
//获取当前行每一列
Cell cell = currentRow.getCell(cellNum);
//获取当前列的值,追加到 StringBuilder 做输出用
cellstr.append(cell.getStringCellValue());
cellstr.append(" >> ");
}
System.out.println(cellstr.toString());
}
}
}
/*
* 输出excel文件
*/
public void setExcel() {
//设置表第一行的字段,即:列名
HSSFRow r = s.createRow(0);
//测试数据,这个地方可以换成参数动态传入
List<String> list =new ArrayList<String>();
list.add("id");
list.add("name");
list.add("work");
list.add("age");
list.add("size");
//根据传入的列名集合,设置列名
for(int i=0;i<list.size();i++) {
//设置列数
HSSFCell c = r.createCell(i);
//设置每一列的值
c.setCellValue(list.get(i));
}
//准备测试数据
ArrayList<String[]> strs = new ArrayList<String[]> ();
for(int i=0;i<10;i++) {
String[] str = new String[] {i+"","haha"+i,"修仙"+i,"000"+i,i+15+""};
strs.add(str);
}
//设置数据
//遍历数据
for(int i =0;i<strs.size();i++) {
//根据数据大小,设置行数
HSSFRow rr = s.createRow(i+1);
for(int csize=0;csize<strs.get(0).length;csize++) {
//设置列数
HSSFCell c = rr.createCell(csize);
//设置每一列的值
c.setCellValue(strs.get(i)[csize]);
}
}
//输出为文件/保存文件
FileOutputStream out = null;
try {
out = new FileOutputStream("test.xls");
wb.write(out);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}