一、JExcelApi 可以从文件或者输入流进行读取操作。
基本步骤:
1.由文件或者输入流创建一个workbook;
2.由workbook的getSheet()方法创建一个工作表Sheet(两种方法,下标和名字,下标从0开始);
3.由Sheet的getCell(x,y)方法得到某个单元格,cell对象可以读取它的类型(getType)、内容(getContents)等。
读取excel表格内容的代码如下:
1 package com.test;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 import jxl.Cell;
7 import jxl.Sheet;
8 import jxl.Workbook;
9 import jxl.read.biff.BiffException;
10
11
12
13 public class ExcelRead {
14
15 /*
16 @param args
17 */
18 public static void main(String[] args) {
19 File f = new File(“F:/shar/test/test.xls”);
20 try {
21 Workbook book = Workbook.getWorkbook(f);//
22 Sheet sheet = book.getSheet(0); // 获得第一个工作表对象
23 for (int i = 0; i < sheet.getRows(); i++) {
24 for (int j = 0; j < sheet.getColumns(); j++) {
25 Cell cell = sheet.getCell(j, i); // 获得单元格
26 System.out.print(cell.getContents() + “ “);
27 //得到单元格的类型
28 //System.out.println(cell.getType());
29 }
30 System.out.print(“\n”);
31 }
32 } catch (BiffException e) {
33 // TODO Auto-generated catch block
34 e.printStackTrace();
35 } catch (IOException e) {
36 // TODO Auto-generated catch block
37 e.printStackTrace();
38 }
39 }
40
41 }
二、创建excel表格的基本步骤:
1.创建一个WritableWorkbook对象(用Workbook的createWorkbook方法创建),要指定创建一个文件;
2.创建一个工作表WritableSheet(用workbook对象的createSheet方法创建),注意要是WritableSheet,说明可以对其写;
3.创建单元格,再将单元格加入到sheet里;
4.执行workbook的write()方法进行写操作最后关闭workbook。
创建表格的具体代码如下:
1 package com.test;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 import jxl.Workbook;
7 import jxl.write.Label;
8 import jxl.write.Number;
9 import jxl.write.WritableSheet;
10 import jxl.write.WritableWorkbook;
11 import jxl.write.WriteException;
12 import jxl.write.biff.RowsExceededException;
13
14 public class ExcelWrite {
15
16 /*
17 @param args
18 @throws IOException
19 @throws WriteException
20 @throws RowsExceededException
21 /
22 public static void main(String[] args) throws IOException, RowsExceededException, WriteException {
23 // TODO Auto-generated method stub
24 WritableWorkbook workbook = Workbook.createWorkbook(new File(“F:/shar/test/write1.xls”));
25 //生成第一页的工作表,参数为0说明是第一页
26 WritableSheet sheet = workbook.createSheet(“第一页”, 0);
27 //指明单元格的位置是第一行第一列,第一个参数为列
28 Label type = new Label(0,0,”通话类型”);
29 Label poneNo = new Label(1,0,”对方号码”);
30 Label addr = new Label(2,0,”通话地”);
31 Label time = new Label(3,0,”通话时长”);
32 //将单元格加到工作表中
33 sheet.addCell(type);
34 sheet.addCell(poneNo);
35 sheet.addCell(addr);
36 sheet.addCell(time);
37 //数字类型
38 //jxl.write.Number number = new jxl.write.Number(0,1,789.123);
39 Label type1 = new Label(0,1,”主叫”);
40 Label poneNo1 = new Label(1,1,”18711370881”);
41 Label addr1 = new Label(2,1,”湖南株洲”);
42 Label time1 = new Label(3,1,”25”);
43 sheet.addCell(type1);
44 sheet.addCell(poneNo1);
45 sheet.addCell(addr1);
46 sheet.addCell(time1);
47 workbook.write();
48 workbook.close();
49 }
50
51 }
三、对原有的excel文件进行修改
基本步骤:
1.获得要修改的文件;
2.为要修改的文件创建一个副本;
3.对副本进行操作;
4.讲副本写到原有的文件中。
具体代码如下:
1 package com.test;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 import jxl.Workbook;
7 import jxl.read.biff.BiffException;
8 import jxl.write.Label;
9 import jxl.write.WritableSheet;
10 import jxl.write.WritableWorkbook;
11 import jxl.write.WriteException;
12 import jxl.write.biff.RowsExceededException;
13
14 public class ExcelUpdate {
15
16 /*
17 @param args
18 @throws IOException
19 @throws BiffException
20 @throws WriteException
21 @throws RowsExceededException
22 */
23 public static void main(String[] args) throws BiffException, IOException, RowsExceededException, WriteException {
24 // TODO Auto-generated method stub
25 //获得文件
26 Workbook wb = Workbook.getWorkbook(new File(“F:/shar/test/write.xls”));
27 //打开文件的一个副本,并且指定数据写回到原文件
28 WritableWorkbook workbook = Workbook.createWorkbook(new File(“F:/shar/test/write.xls”),wb);
29 //添加一个工作表
30 WritableSheet sheet = workbook.createSheet(“第二页”, 1);
31 //添加一个单元格
32 Label label = new Label(0,0,”第二页测试数据”);
33 sheet.addCell(label);
34 workbook.write();
35 workbook.close();
36 }
37
38 }
另可以设置单元格的字体以及对齐方式,代码如下:
1 package com.test;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 import jxl.Workbook;
7 import jxl.format.Alignment;
8 import jxl.format.VerticalAlignment;
9 import jxl.write.Label;
10 import jxl.write.Number;
11 import jxl.write.WritableCellFormat;
12 import jxl.write.WritableFont;
13 import jxl.write.WritableSheet;
14 import jxl.write.WritableWorkbook;
15 import jxl.write.WriteException;
16 import jxl.write.biff.RowsExceededException;
17
18 public class FontFormat {
19
20 /*
21 @param args
22 @throws IOException
23 @throws WriteException
24 @throws RowsExceededException
25 /
26 public static void main(String[] args) throws IOException, RowsExceededException, WriteException {
27 // TODO Auto-generated method stub
28 WritableWorkbook workbook = Workbook.createWorkbook(new File(“F:/shar/test/font.xls”));
29 //生成第一页的工作表,参数为0说明是第一页
30 WritableSheet sheet = workbook.createSheet(“第一页”, 0);
31
32 //设置字体格式(字体为TIMES,大小为16磅,加粗)
33 WritableFont font = new WritableFont(WritableFont.TIMES,16,WritableFont.BOLD);
34 WritableCellFormat format = new WritableCellFormat(font);
35 //设置数据的对齐方式
36 //水平居中
37 format.setAlignment(Alignment.CENTRE);
38 //垂直居中
39 format.setVerticalAlignment(VerticalAlignment.CENTRE);
40 //设置自动还行
41 format.setWrap(true);
42
43 //指明单元格的位置是第一行第一列,第一个参数为列
44 Label type = new Label(0,0,”通话类型”,format);
45 //将单元格加到工作表中
46 sheet.addCell(type);
47 //数字类型
48 //jxl.write.Number number = new jxl.write.Number(0,1,789.123);
49 Label type1 = new Label(0,1,”主叫”);
50 sheet.addCell(type1);
51 workbook.write();
52 workbook.close();
53 }
54
55 }