java读取excel表格

本文介绍了如何使用Java通过jxl API从.xls文件中读取单元格数据,包括工作簿处理、Sheet操作和单元格内容获取,适合Java开发者参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用java的时候,希望从excel中读取到一些单元格的数据,供大家参考,具体内容如下

1.Java读取的excel的api

这里用到了一个叫jxl的api如下:

<dependency>
  <groupId>net.sourceforge.jexcelapi</groupId>
  <artifactId>jxl</artifactId>
  <version>2.6.12</version>
</dependency>

在java中需要去导入一些类去读取excel

import jxl.Workbook; //java读取excel表使用的类 
import jxl.Cell;  //java读取表格里的单元格的类 
import jxl.Sheet; //java读取的工作铺的类

首先:

创建一个File 去读取文件(我以D盘redisInput文件下的GPSINFO.xls文件为例)  

注意:不能够读取xlsx后缀的excel文件,否则会报错: Unable to recognize OLE stream

File Inputfile = new File("D:\\redisInput\\GPSINFO.xls");

使用字符流去接File的数据

FileInputStream fileInputStream = new FileInputStream(Inputfile);

workbook去接fileInputStream

Workbook workbook = Workbook.getWorkbook(fileInputStream);

这样读取到了excel文件,但是需要去判断是哪一个工作簿,要用到Sheet类

Sheet readfirst = workbook.getSheet(0);

如果getsheet(0)那么就是去访问第一个工作簿里的数据,然后可以在sheet类中看有多少有效行和有效列

int rows = readfirst.getRows();
int clomns = readfirst.getColumns();
System.out.println("row:" + rows);
System.out.println("clomns:" + clomns);

2.读到的单元格进行打印

完整代码:

public class AnalysisExcel {
 
 Workbook workbook = null;
 File Inputfile = new File("D:\\redisInput\\GPSINFO.xls");
 File outputfile =new File("D:\\redisInput\\GPSINFO.txt");
 
 public void ToAnalysisExcel() {
 // Unable to recognize OLE stream 不支持xlsx格式 支持xls格式
 
 try {
 
  FileInputStream fileInputStream = new FileInputStream(Inputfile);
  workbook = Workbook.getWorkbook(fileInputStream);
  FileOutputStream fileOutputStream = new FileOutputStream(outputfile);
  BufferedOutputStream bw = new BufferedOutputStream(fileOutputStream); //输出语句
 
  Sheet readfirst = workbook.getSheet(0);
  int rows = readfirst.getRows();
  int clomns = readfirst.getColumns();
  System.out.println("row:" + rows);
  System.out.println("clomns:" + clomns);
 
  for(int i =1;i<rows;i++) {
  Cell[] cells = readfirst.getRow(i); //循环得到每一行的单元格对象
 
  //根据每一个单元格对象的到里面的值
  String brandNum= cells[0].getContents(); 
  String deviceCode = cells[1].getContents();
  String sim =cells[2].getContents();
  System.out.println("rand:"+brandNum+",vehicleNum:"+deviceCode+",sim:"+sim);
  
   //将得到的值放在一个我需要的格式的string对象中
 
  String output = "\n"+sim+"\n" +
   "{\n" +
   " \"brandColor\": 500000,\n" +
   " \"brandNumber\": \""+brandNum+"\",\n" +
   " \"deviceCode\": \""+deviceCode+"\",\n" +
   " \"simCard\": \""+sim+"\"\n" +
   "}"+
    "\n";
  //write and flush到 我需要的文件中去,flush后才能成功
  byte[] outputbyte = new String(output).getBytes();
  bw.write(outputbyte);
 
  bw.flush();
  }
 
 
 } catch (FileNotFoundException e) {
  e.printStackTrace();
 } catch (IOException e) {
  e.printStackTrace();
 } catch (BiffException e) {
  e.printStackTrace();
 }
 
 }
 
}

转载地址:https://www.jb51.net/article/203271.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值