java读取execl表格中的数据
publicclasstest1{publicstaticvoidmain(Stringargs[])throwsBiffException,IOException,Wr...
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)
解决问题后给采纳 展开
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)
解决问题后给采纳 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询