需要下载jxl.jar,去官网下载
代码如下:
//import java.io.*;
//import java.util.Scanner;
//
///**
// * @author Administrator
// * @version 1.0
// * @Date 2011-06-07 java练习
// */
//public class Test7 {
//
// /**
// * 主函数
// *
// * @param args
// */
// public static void main(String[] args) throws Exception {
//
// Test7 test7 = new Test7();
// test7.print();
// test7.print1();
// }
//
// /**
// * 计算并输出1-1000中所有偶数的和
// *
// * @throws IOException
// */
// public void print() throws IOException {
// int cn = 0;
// for (int i = 1; i < 1000; i++) {
// if (i % 2 == 0) {
// cn += i;
// }
// }
// System.out.println(cn);
// }
//
// /**
// * 输出杨辉三角
// */
// public void print1() {
// Scanner sc = new Scanner(System.in);
// System.out.println("请输入行数:");
// int n = sc.nextInt();
// int a[][] = new int[n][n];
// int i, j;
// for (i = 0; i < n; i++) {
// a[i][i] = new Integer(1);
// a[i][0] = new Integer(1);
// }
// for (i = 2; i < n; i++) {
// for (j = 1; j <= i - 1; j++)
// a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
// }
// for (i = 0; i < n; i++) {
// for (j = 0; j <= i; j++)
// System.out.printf("%6d", a[i][j]);
// System.out.println();
// }
// }
//}
import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class Test7 {
/**
* @param args
*/
public static void main(String[] args) {
try {
//得到工作薄
Workbook workbook = Workbook.getWorkbook(new File("D:/日常开销.xls"));
//得到工作区
Sheet sheet = workbook.getSheet(0);
//得到单元格
Cell cell00 = sheet.getCell(0,0);
Cell cell01 = sheet.getCell(0,1);
Cell cell02 = sheet.getCell(0,2);
Cell cell03 = sheet.getCell(1,0);
System.out.println("第1个" + cell00.getContents());
System.out.println("第2个" + cell01.getContents());
System.out.println("第3个" + cell02.getContents());
System.out.println("第4个" + cell03.getContents());
//得到列数
int columCount = sheet.getColumns();
//得到行数
int rowCount = sheet.getRows();
System.out.println("行数"+columCount);
System.out.println("列数"+rowCount);
for(int i=0;i<rowCount;i++){
System.out.print("第"+(i+1)+"行的内容是:");
for(int j=0;j<columCount;j++){
Cell cell = sheet.getCell(j,i);
System.out.print(cell.getContents()+"\t");
}
System.out.println("");
}
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}