<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-examples</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-excelant</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.0</version>
</dependency>
package com.example.demo.util;
import java.io.File;
import java.io.FileInputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
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.usermodel.WorkbookFactory;
public class OperateExcel {
public static void main(String[] args) {
List<String[]> value = readExcel(0, "D:\\1.xls");
for (int i = 0; i < value.size(); i++) {
System.out.println("第" + i + "行");
for (String v : value.get(i)) {
System.out.println(v);
}
}
System.out.println("============================================");
List<String[]> value_1 = readExcel(0, "D:\\2.xlsx");
for (int i = 0; i < value_1.size(); i++) {
System.out.println("第" + i + "行");
for (String v : value_1.get(i)) {
System.out.println(v);
}
}
}
public static List<String[]> readExcel(int read_line_num, String path) {
try {
File file = new File(path);
if (!file.exists()) {
return null;
}
List<String[]> result = new ArrayList<String[]>();
FileInputStream fis = new FileInputStream(file);
Workbook workBook = WorkbookFactory.create(fis);
Sheet sheet = workBook.getSheetAt(0);
int column_num = 0;
for (int start_row = read_line_num, num_row = sheet.getLastRowNum(); start_row <= num_row; start_row++) {
Row row = sheet.getRow(start_row);
if (row == null) {
continue;
}
if (column_num < row.getLastCellNum()) {
column_num = row.getLastCellNum();
}
String[] row_value = new String[column_num];
for (int start_cell = 0, sum_cell = row.getLastCellNum(); start_cell < sum_cell; start_cell++) {
Cell cell = row.getCell(start_cell);
if (cell == null) {
continue;
}
String cell_value = "";
switch (cell.getCellType()) {
case NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
if (date != null) {
cell_value = new SimpleDateFormat("yyyy-MM-dd").format(date);
} else {
cell_value = "";
}
} else {
cell_value = new DecimalFormat("#").format(cell.getNumericCellValue());
}
break;
case STRING:
cell_value = cell.getStringCellValue();
break;
case BOOLEAN:
cell_value = cell.getStringCellValue();
break;
case FORMULA:
if (!cell.getStringCellValue().equals("")) {
cell_value = cell.getStringCellValue();
} else {
cell_value = cell.getNumericCellValue() + "";
}
break;
case BLANK:
cell_value = "";
break;
case ERROR:
cell_value = "";
break;
default:
cell_value = "";
break;
}
row_value[start_cell] = String.valueOf(cell_value).trim();
}
result.add(row_value);
}
System.out.println(3);
workBook.close();
return result;
} catch (Exception e) {
return null;
}
}
}