java如何导入excel
使用Apache POI库导入Excel
Apache POI是Java处理Microsoft Office格式文件的流行库,支持Excel的.xls和.xlsx格式。
添加Maven依赖:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
读取.xlsx文件示例代码:
import org.apache.poi.ss.usermodel.*;
import java.io.FileInputStream;
public class ExcelReader {
public static void readExcel(String filePath) throws Exception {
Workbook workbook = WorkbookFactory.create(new FileInputStream(filePath));
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
System.out.print(cell.toString() + "\t");
}
System.out.println();
}
workbook.close();
}
}
使用EasyExcel处理大数据量
阿里巴巴的EasyExcel专门优化了大文件读取,内存消耗更低。
添加Maven依赖:

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.3.2</version>
</dependency>
使用监听器模式读取:
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.ReadListener;
public class ExcelReader {
public static void readLargeExcel(String filePath) {
EasyExcel.read(filePath, DemoData.class, new ReadListener<DemoData>() {
public void invoke(DemoData data, AnalysisContext context) {
System.out.println(data);
}
public void doAfterAllAnalysed(AnalysisContext context) {}
}).sheet().doRead();
}
}
使用JExcelAPI处理老格式文件
JExcelAPI适合处理.xls格式的老版本Excel文件。
添加Maven依赖:

<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
读取示例:
import jxl.Sheet;
import jxl.Workbook;
import java.io.File;
public class ExcelReader {
public static void readXls(String filePath) throws Exception {
Workbook workbook = Workbook.getWorkbook(new File(filePath));
Sheet sheet = workbook.getSheet(0);
for (int row = 0; row < sheet.getRows(); row++) {
for (int col = 0; col < sheet.getColumns(); col++) {
System.out.print(sheet.getCell(col, row).getContents() + "\t");
}
System.out.println();
}
workbook.close();
}
}
处理不同类型单元格数据
读取时需要区分不同单元格类型:
Cell cell = row.getCell(0);
switch (cell.getCellType()) {
case STRING:
System.out.println(cell.getStringCellValue());
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
System.out.println(cell.getDateCellValue());
} else {
System.out.println(cell.getNumericCellValue());
}
break;
case BOOLEAN:
System.out.println(cell.getBooleanCellValue());
break;
case FORMULA:
System.out.println(cell.getCellFormula());
break;
default:
System.out.println("");
}
性能优化建议
对于大文件处理,采用流式读取模式:
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelReader {
public static void streamRead(String filePath) throws Exception {
Workbook workbook = new XSSFWorkbook(new FileInputStream(filePath));
Sheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// 处理行数据
}
workbook.close();
}
}






