在使用java的时候,希望从excel中读取到一些单元格的数据,供大家参考,具体内容如下
1.Java读取的excel的api这里用到了一个叫jxl的api如下:
net.sourceforge.jexcelapi jxl2.6.12
在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);
如果想看每个单元格的数据可以使用一个双重循环去读取每一个有效单元格里数据
for(int i =1;i这样就把所有的有效单元格输出了。
2.读到的单元格进行打印但是我想让它按照我想要的格式进行输出,比如 excel表中有sim卡 ,车牌号,终端号,我希望让它能够生成一个特有的格式让我使用比如:
转成-》这种格式的数据
所以一个完整过程如下:
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以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



