Jxl使用
Jxl是对excel操作的一组API,下面就通过两个例子介绍一下它:
read Excel:
1
public String readExcel()
{
2
try
{
3
//选取指定的excel
4
Workbook workbook = Workbook.getWorkbook(new File("text.xls"));
5
//选取制定的sheet
6
Sheet sheet = workbook.getSheet(0);
7
//选取指定的cell
8
//遍历循环得到所要的cell值
9
for(int j = 0 ;j<sheet.getRows() ; j++)
10
for(int i = 0 ;i<sheet.getColumns();i++)
{
11
Cell cell = sheet.getCell(i,j);
12
//获取该cell的值
13
String var1 = cell.getContents();
14
//打印输出该值
15
System.out.println(var1);
16
}
17
} catch (BiffException e)
{
18
e.printStackTrace();
19
} catch (IOException e)
{
20
e.printStackTrace();
21
}
22
return null;
23
}
注意:
所有的参数全部是(column,row)
workbook.getSheet(0);参数代表sheet的名,0代表第一个sheet,可以去该sheet的全名
用getRows()&getColumn()取得的列数和行数是系统自动为你取得,即excel中实际使用的行数和列数,它包括,人为自定义的空行和空列,它也会取到,请使用是注意。
write Excel
1
/** *//**
2
* 测试jxl写入excel
3
* @author wanggang
4
* @param null
5
* @throws WriteException
6
* @throws RowsExceededException
7
*/
8
public String writeExcel()throws IOException, RowsExceededException, WriteException
{
9
//创建Excel
10
WritableWorkbook workbook = Workbook.createWorkbook(new File("write.xls"));
11
//创建sheet
12
WritableSheet sheet1 = workbook.createSheet("测试用excel", 1);
13
WritableSheet sheet2 = workbook.createSheet("测试excel", 0);
14
/** *//**
15
* 总结:createSheet("sheetName",index);
16
* @param index表示该sheet是第几个sheet
17
*
18
*/
19
//开始创建cell
20
21
//WritableCell cell
22
for(int i = 0 ; i < 10 ;i++)
{
23
sheet1.addCell(new jxl.write.Label(1, i, "书目ID"));
24
/** *//**
25
* 总结:addCell(new Lable(column,row,"content"));
26
*
27
*/
28
}
29
workbook.write();
30
workbook.close();
31
32
//sheet.addCell(new jxl.write.Label(0, 1, "书目ID"));
33
return null;
34
35
}
注意:
WritableSheet要用接口定义,不能用实体类,不然不能进行写操作
总结:
1、Workbook.createWorkbook(new File("write.xls"));和Workbook.getWorkbook(new File("text.xls"));
中new File(“name”);都要包含相对路径。
以下转自:http://zhangzcz1999.javaeye.com/blog/254736感谢原创
1
import java.io.File;
2
import java.io.FileInputStream;
3
import java.io.InputStream;
4
5
import jxl.Cell;
6
import jxl.CellType;
7
import jxl.Sheet;
8
import jxl.Workbook;
9
import jxl.write.Label;
10
11
public class Test
{
12
13
public static void main(String[] args)
{
14
jxl.Workbook rwb = null;
15
try
{
16
// 构建Workbook对象 只读Workbook对象
17
// 直接从本地文件创建Workbook
18
// 从输入流创建Workbook
19
InputStream is = new FileInputStream("Book.xls");
20
rwb = Workbook.getWorkbook(is);
21
// Sheet(术语:工作表)就是Excel表格左下角的Sheet1,Sheet2,Sheet3但在程序中
22
// Sheet的下标是从0开始的
23
// 获取第一张Sheet表
24
Sheet rs = rwb.getSheet(0);
25
// 获取Sheet表中所包含的总列数
26
int rsColumns = rs.getColumns();
27
// 获取Sheet表中所包含的总行数
28
int rsRows = rs.getRows();
29
// 获取指这下单元格的对象引用
30
for (int i = 0; i < rsRows; i++)
{
31
for (int j = 0; j < rsColumns; j++)
{
32
Cell cell = rs.getCell(j, i);
33
System.out.print(cell.getContents() + " ");
34
}
35
System.out.println();
36
}
37
// 利用已经创建的Excel工作薄创建新的可写入的Excel工作薄
38
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File(
39
"Book2.xls"), rwb);
40
// 读取第一张工作表
41
jxl.write.WritableSheet ws = wwb.getSheet(0);
42
43
// 获取第一个单元格对象
44
jxl.write.WritableCell wc = ws.getWritableCell(0, 0);
45
// 决断单元格的类型,做出相应的转化
46
if (wc.getType() == CellType.LABEL)
{
47
Label l = (Label) wc;
48
l.setString("The value has been modified.");
49
}
50
// 写入Excel对象
51
wwb.write();
52
wwb.close();
53
54
} catch (Exception e)
{
55
e.printStackTrace();
56
} finally
{
57
// 操作完成时,关闭对象,翻译占用的内存空间
58
rwb.close();
59
}
60
61
}
62
63
64
}