項目結構:
http://www.cnblogs.com/hongten/gallery/image/112177.html
在項目中我們看到Reference Libraries中的jxl.jar包,它是我們自己外部引入的包。
運行結果:
http://www.cnblogs.com/hongten/gallery/image/112178.html
ExcelHandle.java
1 /**
2 *
3 */
4 package com.b510;
5
6 import java.io.File;
7
8 import jxl.Workbook;
9 import jxl.format.Border;
10 import jxl.format.BorderLineStyle;
11 import jxl.format.Colour;
12 import jxl.write.Label;
13 import jxl.write.WritableCellFormat;
14 import jxl.write.WritableFont;
15 import jxl.write.WritableSheet;
16 import jxl.write.WritableWorkbook;
17
18 /**
19 *
20 * @author XHW
21 *
22 * @date 2012-2-29
23 *
24 */
25 public class ExcelHandle {
26
27 /**
28 * @param args
29 */
30 public static void main(String[] args) {
31 ExcelHandle excelHandle = new ExcelHandle();
32 excelHandle.writeExcel();
33
34 }
35
36 /**
37 * 寫入Excel
38 *
39 */
40 public void writeExcel() {
41 try {
42 //寫入到那個Excel文件 如:c:\\hello.xls,或者hello.xls(這個是在項目的根目錄下)
43 WritableWorkbook wwb = Workbook
44 .createWorkbook(new File("hello.xls"));
45 // 創建Excel工作表 指定名稱和位置
46 WritableSheet ws = wwb.createSheet("Test Sheet 1", 0);
47 // 設置表格的列寬度
48 ws.setColumnView(0, 14);//第一列寬14
49 ws.setColumnView(1, 12);
50 ws.setColumnView(2, 25);
51 ws.setColumnView(3, 20);
52 ws.setColumnView(4, 12);
53 ws.setColumnView(5, 9);
54 ws.setColumnView(6, 12);//第7列寬12
55
56 // **************往工作表中添加數據*****************
57
58 //定義字體格式:字體為:微軟雅黑,24號子,加粗
59 WritableFont titleFont = new WritableFont(WritableFont
60 .createFont("微軟雅黑"), 24, WritableFont.NO_BOLD);
61 WritableFont contentFont = new WritableFont(WritableFont
62 .createFont("楷體 _GB2312"), 12, WritableFont.NO_BOLD);
63
64 WritableCellFormat titleFormat = new WritableCellFormat(titleFont);
65 WritableCellFormat contentFormat = new WritableCellFormat(
66 contentFont);
67 WritableCellFormat contentFormat2 = new WritableCellFormat(
68 contentFont);
69
70 contentFormat.setBorder(Border.ALL, BorderLineStyle.THIN,
71 Colour.BLACK);
72 //設置格式居中對齊
73 titleFormat.setAlignment(jxl.format.Alignment.CENTRE);
74 contentFormat2.setAlignment(jxl.format.Alignment.CENTRE);
75
76 // ***************將定義好的單元格添加到工作表中*****************
77 ws.mergeCells(0, 0, 6, 0);// 合並單元格A-G共7列
78 ws.addCell(new Label(0, 0, "廣州XXX大學2009級研究生課程考試成績冊", titleFormat));
79 ws.addCell(new Label(0, 1, "課程名稱", contentFormat2));
80 ws.mergeCells(1, 1, 6, 1);// 合並單元格B-G共6列
81 ws.addCell(new Label(1, 1, "大學數學", contentFormat2));
82 ws.addCell(new Label(0, 2, "院所教研室", contentFormat2));
83 ws.mergeCells(1, 2, 6, 2);// 合並單元格B-G共6列
84 ws.addCell(new Label(0, 3, "填表人", contentFormat2));
85 ws.addCell(new Label(2, 3, "教研室負責人", contentFormat2));
86
87 String th[] = { "學號", "姓名", "學院", "平時成績", "期末成績", "總成績", "補考成績" };
88 for (int i = 0; i < 7; i++) {
89 ws.addCell(new Label(i, 4, th[i], contentFormat2));
90 }
91 //這里我們可以從數據庫里面查詢數據,然后在這里獲取數據
92 int xh = 200901;
93 String xm = "王佳佳";
94 String xy = "XXX信息技術學院";
95 String space = " ";
96 int cj = 50;
97 String bk = "補 80";
98 //向Excel中插入數據
99 for (int j = 5; j < 10; j++) {
100 ws.addCell(new Label(0, j, "" + xh + j + "", contentFormat));
101 ws.addCell(new Label(1, j, xm+j , contentFormat));
102 ws.addCell(new Label(2, j, xy , contentFormat));
103 ws.addCell(new Label(3, j, space , contentFormat));
104 ws.addCell(new Label(4, j, space, contentFormat));
105 ws.addCell(new Label(5, j, "" + cj + j + "", contentFormat));
106 ws.addCell(new Label(6, j, "" + bk + "", contentFormat));
107 }
108 // 寫入工作表完畢,關閉流
109 wwb.write();
110 wwb.close();
111 } catch (Exception e) {
112 e.printStackTrace();
113 }
114 }
115
116
117
118 }
java的poi技術讀取和導入Excel:
http://www.cnblogs.com/hongten/archive/2012/02/22/java2poi.html
