package test;
import jxl.*;
import jxl.Cell;
import java.io.*;
/**
* 读取excel
*/
public class TestExcel
{
public static void readExcel(String filePath)
{
try
{
InputStream is = new FileInputStream(filePath);
Workbook rwb = Workbook.getWorkbook(is);
//这里有两种方法获取sheet表:名字和下标(从0开始)
//Sheet st = rwb.getSheet("original");
// Sheet st = rwb.getSheet(0);
/**
//获得第一行第一列单元的值
Cell c00 = st.getCell(0,0);
//通用的获取cell值的方式,返回字符串
String strc00 = c00.getContents();
//获得cell具体类型值的方式
if(c00.getType() == CellType.LABEL)
{
LabelCell labelc00 = (LabelCell)c00;
strc00 = labelc00.getString();
}
//输出
System.out.println(strc00);*/
Sheet rst = rwb.getSheet(0);
//获取Sheet表中所包含的总列数
int rsColumns = rst.getColumns();
//获取Sheet表中所包含的总行数
int rsRows = rst.getRows();
//获取指定单元格的对象引用
/**
* 取第一行
*/
for (int i = 0; i < rsColumns; i++)
{
for (int j = 0; j < 1; j++)
{
Cell cell = rst.getCell(i, j);
System.out.print(cell.getContents() + " ");
}
}
/**
* 取每一行
*/
// for (int i = 0; i < rsRows; i++)
// {
// for (int j = 0; j < rsColumns; j++)
// {
// Cell cell = rst.getCell(j, i);
// System.out.print(cell.getContents() + " ");
// }
// System.out.println();
// }
/**
* 取每一行
*/
//第一行
// System.out.print(rst.getCell(0, 0).getContents() +
// " " +rst.getCell(1, 0).getContents()+
// " " +rst.getCell(2, 0).getContents()+
// " " +rst.getCell(3, 0).getContents()+
// " " +rst.getCell(4, 0).getContents()+
// " " +rst.getCell(5, 0).getContents()+
// " " +rst.getCell(6, 0).getContents())
// ;
// System.out.println();
// //第二行
// System.out.print(rst.getCell(0, 1).getContents() +
// " " +rst.getCell(1, 1).getContents());
// System.out.println();
// //第三行
// System.out.print(rst.getCell(0, 2).getContents() +
// " " +rst.getCell(1, 2).getContents());
//关闭
rwb.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
readExcel("d://test.xls");
}
}