将所有单元格格式转化为文本格式读取,存储进一个二维数组,i代表行,j代表列
可以自带检索文件夹下存在的excel文件,不需要规定名字(但只能存在一个excel文件)
代码如下
package org.example;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TOOL {
// 将单元格内容转化为字符串
private static String convertCellValueToString(Cell cell) {
if (null == cell) {
return null;
}
String resStr = null;
CellType cellType = cell.getCellTypeEnum();
switch (cellType) {
case STRING:
resStr = cell.getStringCellValue();
break;
case NUMERIC:
if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
resStr = dateFormat.format(date);
} else {
double numericCellValue = cell.getNumericCellValue();
if (isInteger(numericCellValue)) {
java.text.DecimalFormat df = new java.text.DecimalFormat("0");
resStr = df.format(numericCellValue);
} else {
resStr = Double.toString(numericCellValue);
}
}
break;
case BOOLEAN:
boolean booleanCellValue = cell.getBooleanCellValue();
resStr = Boolean.toString(booleanCellValue);
break;
case BLANK:
break;
case FORMULA:
try {
resStr = String.valueOf(cell.getNumericCellValue());
} catch (IllegalStateException e) {
resStr = String.valueOf(cell.getRichStringCellValue());
}
break;
case ERROR:
break;
default:
break;
}
return resStr;
}
// 近似判断是否为整数,是返回true,否则返回false.
public static boolean isInteger(Double num) {
double eqs = 1e-10; //精度范围
return Math.abs(num - Math.floor(num)) < eqs;
}
public String [][] getOther() throws IOException {
// 获取当前文件夹路经 File(“.”)表示当前路径, File(“..”)表示上级目录,
File directory = new File(".");
// 获取当前目录的所有文件信息
File[] files = directory.listFiles();
// 获取当前文件夹绝对路径
String courseFile = directory.getCanonicalPath();
// 存储你要适配的文件名
String filename = "";
// 遍历所有文件
for (File file : files) {
// 判断是否匹配成功(通过文件名,string.matches , 正则表达式
boolean matches = file.getName().matches(".*xls.*");
// 如果成功则获取当前文件文件名
if (matches) {
filename = file.getName();
}
}
// 再加一个 \ 完成
filename = courseFile + "\\" + filename;
// 打印读取的文件绝对路径
System.out.println(filename);
// 根据路径读取文件内容
File file = new File(filename);
InputStream inputStream = Files.newInputStream(file.toPath());
// 根据格式选择对应的workbook
Workbook workbook;
if (filename.toLowerCase().endsWith(".xls")) {
workbook = new HSSFWorkbook(inputStream);
} else if (filename.toLowerCase().endsWith(".xlsx")) {
workbook = new XSSFWorkbook(inputStream);
} else {
throw new IllegalArgumentException("不支持的文件格式");
}
// 通常只有一张表
Sheet sheet = workbook.getSheetAt(0);
// 先遍历一遍,看看有多少条数据,方便定义数组
int n = 0;
for (Row row : sheet) {
n++;
}
System.out.println(n);
Row row1 = sheet.getRow(0);
int m = 0;
for (Cell cell:row1) {
m++;
}
System.out.println(n);
String[][] lists = new String[n][m];
for (int i = 0; i < sheet.getLastRowNum() +1; i++) {
Row row = sheet.getRow(i);
// 如果用row.getcell判断是否为null本身就会报错(空指针异常)
if (row == null) {
break;
}
for(int j =0;j < row.getLastCellNum();j++){
Cell cell = row.getCell(j);
String s = convertCellValueToString(cell);
lists[i][j] = s;
}
}
inputStream.close();
workbook.close();
return lists;
}
}