直接可以运行
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;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class TestExcel2 {
public static void main(String[] args) throws Exception {
String filePath = "C:\\Users\\Administrator\\Desktop\\试导入.xlsx";
File file = new File(filePath);
if (!file.exists()) {
System.out.println(" File not exists ...");
return;
}
List list = dealExcel(new FileInputStream(file), "试导入.xlsx");
list.forEach(t->{
System.out.println(t.toString());
});
}
public static List dealExcel(InputStream in, String fileName) throws Exception {
List listResult = new ArrayList<>();
Workbook work = getWorkbook(in, fileName);
if (null == work) {
throw new Exception("创建Excel工作薄为空!");
}
Sheet sheet = null;
Row row = null;
Cell cell = null;
for (int i = 0; i < work.getNumberOfSheets(); i++) {
sheet = work.getSheetAt(i);
if (sheet == null) {
continue;
}
for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
row = sheet.getRow(j);
if (row == null) {
continue;
}
List<Object> list = new ArrayList<>();
for (int y = 0; y < row.getLastCellNum(); y++) {
if (isMergedRegion(sheet,j,y)) {
cell = getMergedRegionValue(sheet,j,y);
}else{
cell = row.getCell(y);
}
list.add(cell);
}
listResult.add(list);
}
}
work.close();
return listResult;
}
public static boolean isMergedRegion(Sheet sheet, int row, int column) {
int sheetMergeCount = sheet.getNumMergedRegions();
for (int i = 0; i < sheetMergeCount; i++) {
CellRangeAddress range = sheet.getMergedRegion(i);
int firstColumn = range.getFirstColumn();
int lastColumn = range.getLastColumn();
int firstRow = range.getFirstRow();
int lastRow = range.getLastRow();
if (row >= firstRow && row <= lastRow) {
if (column >= firstColumn && column <= lastColumn) {
Cell cell = sheet.getRow(firstRow).getCell(firstColumn);
System.out.println(cell.getStringCellValue());
System.out.println("++++++++++++++++++++++++++++");
return true;
}
}
}
return false;
}
public static Cell getMergedRegionValue(Sheet sheet, int row, int column) {
int sheetMergeCount = sheet.getNumMergedRegions();
for (int i = 0; i < sheetMergeCount; i++) {
CellRangeAddress range = sheet.getMergedRegion(i);
int firstColumn = range.getFirstColumn();
int lastColumn = range.getLastColumn();
int firstRow = range.getFirstRow();
int lastRow = range.getLastRow();
if (row >= firstRow && row <= lastRow) {
if (column >= firstColumn && column <= lastColumn) {
Cell cell = sheet.getRow(firstRow).getCell(firstColumn);
return cell;
}
}
}
return null;
}
public static Workbook getWorkbook(InputStream inputStream, String fileName) throws Exception {
Workbook workbook = null;
String fileType = fileName.substring(fileName.lastIndexOf("."));
if (".xls".equals(fileType)) {
workbook = new HSSFWorkbook(inputStream);
} else if (".xlsx".equals(fileType)) {
workbook = new XSSFWorkbook(inputStream);
} else {
throw new Exception("请上传excel文件!");
}
return workbook;
}
}