需要下载 POI jar包
POI 下载
下载后解压再导包即可。
package com.qzh.work;
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.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
public class text1 {
private static List readExcel(String str) throws FileNotFoundException, IOException {
File file = new File(str);
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(file));
Sheet sheet = workbook.getSheetAt(0);//读取第一个 sheet
//System.out.println(sheet);
List list = new ArrayList<>();
Row row = null;
int rowCount = sheet.getPhysicalNumberOfRows();//获得行总数
//逐行处理 excel 数据
for (int i = 0; i < rowCount; i++) {
row = sheet.getRow(i);
if (row != null) {
int coloumNum =sheet.getRow(0).getPhysicalNumberOfCells();//获得咧总数
Vector v=new Vector();
for(int j=0;j<coloumNum;j++){
Cell cell=row.getCell(j);
cell.setCellType(CellType.STRING);//被每一个单元格都设置为String类型
v.add(cell.getStringCellValue());//接入Vector容器
}
list.add(v);//再加入列表
}
}
workbook.close();
return list;//list存放文件内的全部文件
}
public static void main(String[] args) throws IOException {
readExcel("/*文件路径*/");
}
}