参考 : java读取xlsx文件的所有数据 - 轨迹yys - 博客园
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
/**
* @Auther: liyue
* @Date: 2019/4/11 10:37
* @Description: 读取xlxs工具类, 可以读取多个sheet
*/
public class ReadXlxsUtil {
public static void main(String[] args) {
String path = "/Users/leyili/Desktop/file_test/合金成分.xlsx";
List<List<List<String>>> listss = readXlxs(path);
for (List<List<String>> lists : listss) {
for (List<String> list : lists) {
System.err.println(list);
}
}
}
public static List<List<List<String>>> readXlxs(String path) {
File excelFile = new File(path); //替换你文档地址
XSSFWorkbook wb = null;
try {
wb = new XSSFWorkbook(new FileInputStream(excelFile));
} catch (IOException e) {
e.printStackTrace();
}
int numberOfSheets = wb.getNumberOfSheets();
String str = "";
List<List<List<String>>> listss = new LinkedList<>();
for (int x = 0; x < numberOfSheets; x++) {
XSSFSheet sheet = wb.getSheetAt(x);
int columnNum = 0;
if (sheet.getRow(0) != null) {
columnNum = sheet.getRow(0).getLastCellNum()
- sheet.getRow(0).getFirstCellNum();
}
List<List<String>> lists = new LinkedList<>();
if (columnNum > 0) {
for (Row row : sheet) {
List<String> list = new LinkedList<>();
String[] singleRow = new String[columnNum];
int n = 0;
for (int i = 0; i < columnNum; i++) {
Cell cell = row.getCell(i, Row.CREATE_NULL_AS_BLANK);
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BLANK:
singleRow[n] = " ";
if (cell == null || cell.equals("") || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
list.add(" ");
} else {
list.add(singleRow[n]);
}
break;
case Cell.CELL_TYPE_BOOLEAN:
singleRow[n] = Boolean.toString(cell
.getBooleanCellValue());
list.add(singleRow[n]);
break;
// 数值
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
SimpleDateFormat sdf = null;
if (cell.getCellStyle().getDataFormat() == HSSFDataFormat
.getBuiltinFormat("h:mm")) {
sdf = new SimpleDateFormat("HH:mm");
} else {// 日期
sdf = new SimpleDateFormat("yyyy-MM-dd");
}
Date date = cell.getDateCellValue();
list.add(sdf.format(date));
} else {
cell.setCellType(Cell.CELL_TYPE_STRING);
String temp = cell.getStringCellValue();
// 判断是否包含小数点,如果不含小数点,则以字符串读取,如果含小数点,则转换为Double类型的字符串
if (temp.indexOf(".") > -1) {
singleRow[n] = String.valueOf(new Double(temp))
.trim();
list.add(singleRow[n]);
} else {
singleRow[n] = temp.trim();
list.add(singleRow[n]);
}
}
break;
case Cell.CELL_TYPE_STRING:
singleRow[n] = cell.getStringCellValue().trim();
list.add(singleRow[n]);
break;
case Cell.CELL_TYPE_ERROR:
singleRow[n] = " ";
list.add(singleRow[n]);
break;
case Cell.CELL_TYPE_FORMULA:
cell.setCellType(Cell.CELL_TYPE_STRING);
String temp = cell.getStringCellValue();
// 判断是否包含小数点,如果不含小数点,则以字符串读取,如果含小数点,则转换为Double类型的字符串
if (temp.indexOf(".") > -1) {
temp = String.valueOf(new Double(temp))
.trim();
Double cny = Double.parseDouble(temp);//6.2041
DecimalFormat df = new DecimalFormat("0.00");
String CNY = df.format(cny);
list.add(CNY);
} else {
singleRow[n] = temp.trim();
list.add(singleRow[n]);
}
default:
singleRow[n] = " ";
break;
}
n++;
}
lists.add(list);
}
listss.add(lists);
}
}
return listss;
}
maven依赖
<!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.9</version>
</dependency>
END。