java读取Excel

代码:

import java.io.*;
import java.util.*;
import jxl.*;
import jxl.read.biff.BiffException;

public class ReadExcel{

public static void main(String[] args) {
try {
Workbook book = Workbook.getWorkbook(new File("1.xls"));
Sheet sheet = book.getSheet(0);
System.out.println("请输入要读取的单元格");
Scanner in = new Scanner(System.in);
int x = in.nextInt();
in = new Scanner(System.in);
int y = in.nextInt();

Cell cell1 = sheet.getCell(x,y);
if (cell1.getType() == CellType.NUMBER) {
NumberCell nc = (NumberCell) cell1;
System.out.println("value : " + nc.getValue());
System.out.println("type : " + cell1.getType());
System.out.println("Format : " + cell1.getCellFormat());
System.out.println("NumberFormat: " + nc.getNumberFormat());
System.out.println("Font" + cell1.getCellFeatures());
System.out.println("Colum "+ cell1.getColumn());
System.out.println("Class "+ nc.getClass());
System.out.println("");
}

else {
System.out.println("Cell(x, y)" + " value : " + cell1.getContents()

  • "; type : " + cell1.getType()+"Format"+cell1.getCellFormat() );

    }

    book.close();
    } catch (BiffException e) {
    // TODO 自动生成 catch 块
    System.out.println(e);
    } catch (IOException e) {
    // TODO 自动生成 catch 块
    System.out.println(e);;
    }

    }
    }

输出:

请输入要读取的单元格
3
3
value : 67.0
type : Number
Format : jxl.biff.XFRecord@1ac33c92
NumberFormat: java.text.DecimalFormat@7d1f1
Fontnull
Colum 3
Class class jxl.read.biff.NumberValue

问题:

为什么Format : jxl.biff.XFRecord@1ac33c92
NumberFormat: java.text.DecimalFormat@7d1f1
这两个输出这些啊?是什么?

这是因为这两处返回的都是对象,返回对象时会调用对象所在类的toString()方法,返回的格式一般为:类名@XXXX(对象在内存中所指向引用的地址),若想返回其他信息,需要重写toString()方法
例如[code="java"]
package my.util;
public class MyUtil {
//测试方法
public static void main(String[] args) {
Demo1 demo1 = new Demo1();
Demo2 demo2 = new Demo2();
System.out.println("当前打印的对象是:" + demo1);
System.out.println("当前打印的对象是:" + demo2);
}
}
//测试类1
class Demo1 {
}
//测试类2
class Demo2{
//重写toString方法
@Override
public String toString() {
return "我是Demo2的对象";
}
}[/code]
显示结果为(根据你的内存占用情况,第一行结果会不一样)
[code="java"]
当前打印的对象是:my.util.Demo1@1fb8ee3
当前打印的对象是:我是Demo2的对象

[/code]