代码改变世界

java读取excel

  方斌  阅读(110)  评论(0编辑  收藏  举报

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;
                }
                /*for (int j = 0 ; j < sheet.getColumns(); j++){
                    Cell cell = sheet.getCell(j,i);
                    System.out.println(cell.getContents() + );
                }*/
            }
        } 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://boolean类型
   System.out.print(cell.getBooleanCellValue());
   System.out.print(" ");
   break;
  default:
  }
  }
  System.out.println();
 }
}
}

编辑推荐:
· 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无法使用小娜搜索本地应用问题的解决方案
点击右上角即可分享
微信分享提示