java读取excel
使用 jxl读取
依赖
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
注意: 读取的excel为 2003版本 ,新版本会报错 Unable to recognize OLE stream
public static void main(String[] args) {
File file = new File("C:" + File.separator+ "Users" + File.separator+ "fangbin" + File.separator + "Desktop" + File.separator + "20210527开发需求" + File.separator + "allhosts2003.xls");
try {
Workbook workbook = Workbook.getWorkbook(file);
Sheet sheet = workbook.getSheet(0);
for (int i = 0; i < sheet.getRows(); i++) {
System.out.println(i +" "+ sheet.getRow(i)[0].getContents() + " " + sheet.getRow(i)[1].getContents() );
if (StringUtils.isBlank(sheet.getRow(i)[0].getContents())) {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
HSSFWorkbook
package com.ij34.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
public class Test02 {
public static void main(String[] args) throws FileNotFoundException, IOException {
File excelFile = new File("table01.xls");
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(excelFile));
HSSFSheet sheet = wb.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getRichStringCellValue().getString());
System.out.print(" ");
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
System.out.print(String.valueOf(cell.getDateCellValue()));
} else {
System.out.print(cell.getNumericCellValue());
}
System.out.print(" ");
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue());
System.out.print(" ");
break;
default:
}
}
System.out.println();
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
2020-05-26 redis监控指标
2020-05-26 shell 判断变量类型的几种方法
2020-05-26 Win10无法使用小娜搜索本地应用问题的解决方案