jxl.jar下载地址:
http://sourceforge.net/project/showfiles.php?group_id=79926
1.
import jxl.Sheet;
import java.io.InputStream;
import java.util.Vector;
import java.io.FileInputStream;
import jxl.Workbook;
public class Test2 {
public static void main(String [] args){
ReadExcel("c://20060818104025_50101_Merchant.xls");
}
public Test2() {
}
/*
* 方法名:ReadExcel
* 参数:filePath为Execl文件的绝对路径
* 用途:读取Execl数据,放在数组中返回
*返回类型:Vector
*
* */
public static Vector ReadExcel(String filePath)
{
Vector v = new Vector();
Workbook rwb = null;
try
{
InputStream is = new FileInputStream(filePath);
rwb = Workbook.getWorkbook(is);
// 获取第一张Sheet表
Sheet rs = rwb.getSheet(0);
for (int k = 0; k < rs.getRows(); k++)
{
for (int j = 0; j < rs.getColumns(); j++)
{
System.out.println(rs.getCell(j, k).getContents());
v.add(rs.getCell(j, k).getContents());
}
}
rwb.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return v;
}
}
2.
public class Test1 {
public static void main(String[] args) {
jxl.Workbook rwb = null;
try{
//构建Workbook对象, 只读Workbook对象
//直接从本地文件创建Workbook
//从输入流创建Workbook
InputStream is = new FileInputStream("E:/project/test/jxlrwtest.xls");
rwb = Workbook.getWorkbook(is);
//Sheet(术语:工作表)就是Excel表格左下角的Sheet1,Sheet2,Sheet3但在程序中
//Sheet的下标是从0开始
//获取第一张Sheet表
Sheet rs = rwb.getSheet(0);
//获取Sheet表中所包含的总列数
int rsColumns = rs.getColumns();
//获取Sheet表中所包含的总行数
int rsRows = rs.getRows();
//获取指定单元格的对象引用
for(int i=0;i<rsRows;i++){
for(int j=0;j<rsColumns;j++){
Cell cell = rs.getCell(j,i);
System.out.print(cell.getContents()+" ");
}
System.out.println();
}
//利用已经创建的Excel工作薄创建新的可写入的Excel工作薄
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File("E:/project/test/test2.xls"),rwb);
//读取第一张工作表
jxl.write.WritableSheet ws = wwb.getSheet(0);
//获得第一个单元格对象
jxl.write.WritableCell wc = ws.getWritableCell(0, 0);
//判断单元格的类型, 做出相应的转化
if (wc.getType() == CellType.LABEL) {
Label l = (Label) wc;
l.setString("The value has been modified.");
}
//写入Excel对象
wwb.write();
wwb.close();
}catch(Exception e){
e.printStackTrace();
}
finally{
//操作完成时,关闭对象,释放占用的内存空间
rwb.close();
}
}
}