java读取execl表格中的数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java读取execl表格中的数据相关的知识,希望对你有一定的参考价值。
public class test1
public static void main(String args[]) throws BiffException, IOException, WriteException
try
//构建Workbook对象, 只读Workbook对象
//直接从本地文件创建Workbook
//从输入流创建Workbook
InputStream is = new FileInputStream("D:\\abc.xlsx");
jxl.Workbook rwb = Workbook.getWorkbook(is);
Sheet rs = (Sheet) rwb.getSheet(0);
//获取第一行,第一列的值
Cell c00 = ((jxl.Sheet) rs).getCell(0, 0);
String strc00 = c00.getContents();
//获取第一行,第二列的值
Cell c10 = ((jxl.Sheet) rs).getCell(1, 0);
String strc10 = c10.getContents();
//获取第二行,第二列的值
Cell c11 = ((jxl.Sheet) rs).getCell(1, 1);
String strc11 = c11.getContents();
System.out.println("Cell(0, 0)" + " value : " + strc00 + "; type : " + c00.getType());
System.out.println("Cell(1, 0)" + " value : " + strc10 + "; type : " + c10.getType());
System.out.println("Cell(1, 1)" + " value : " + strc11 + "; type : " + c11.getType());
String strcc00 = null;
double strcc10 = 0.00;
Date strcc11 = null;
Cell cc00 = ((jxl.Sheet) rs).getCell(0, 0);
Cell cc10 = ((jxl.Sheet) rs).getCell(1, 0);
Cell cc11 = ((jxl.Sheet) rs).getCell(1, 1);
if(c00.getType() == CellType.LABEL)
LabelCell labelc00 = (LabelCell)cc00;
strcc00 = labelc00.getString();
if(c10.getType() == CellType.NUMBER)
NumberCell numc10 = (NumberCell)cc10;
strcc10 = numc10.getValue();
/*if(c11.getType() == CellType.DATE)
DateCell datec11 = (DateCell)cc11;
strcc11 = datec11.getDate();
*/
System.out.println("Cell(0, 0)" + " value : " + strcc00 + "; type : " + cc00.getType());
System.out.println("Cell(1, 0)" + " value : " + strcc10 + "; type : " + cc10.getType());
System.out.println("Cell(1, 1)" + " value : " + strcc11 + "; type : " + cc11.getType());
rwb.close();
catch (Exception e)
e.printStackTrace();
运行代码报错
jxl.read.biff.BiffException: Unable to recognize OLE stream
at jxl.read.biff.CompoundFile.<init>(CompoundFile.java:116)
at jxl.read.biff.File.<init>(File.java:127)
at jxl.Workbook.getWorkbook(Workbook.java:268)
at jxl.Workbook.getWorkbook(Workbook.java:253)
at com.test.excel.test1.main(test1.java:32)
解决问题后给采纳
如果你要读写就用 apache POI ,差不多的用法支持 2003、2007 网上有很多实例。追问
我知道网上有人说用apache POI。。具体改哪里,用什么jar包
参考技术A 说明导入的excel不是jxl认定的标准的OLE file,可是打开的看起来是Excle,然后我把这个Excel重新另存为一个Excel,发现他默认的保存类型是“单个文件网页”(通过EditPlus打开Excle可以看到是网页文件),然后把他保类型修改为Microsoft Office Excel,保存导入,就可以正确导入了。以上是关于java读取execl表格中的数据的主要内容,如果未能解决你的问题,请参考以下文章