<script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; /* 728x90, 大橫幅正文上方 */ google_ad_slot = "4725362798"; google_ad_width = 728; google_ad_height = 90; // --></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
<!--Google 728*90橫幅廣告結束-->
1
package
jxlTest;
2
3
import
java.io.FileOutputStream;
4
import
java.io.OutputStream;
5
import
java.text.SimpleDateFormat;
6
import
java.util.ArrayList;
7
import
java.util.Date;
8
import
java.util.List;
9
10
import
jxl.
*
;
11
import
jxl.format.Alignment;
12
import
jxl.format.Border;
13
import
jxl.format.BorderLineStyle;
14
import
jxl.format.CellFormat;
15
import
jxl.write.Boolean;
16
import
jxl.write.Label;
17
import
jxl.write.Number;
18
import
jxl.write.WritableCellFormat;
19
import
jxl.write.WritableFont;
20
import
jxl.write.WritableSheet;
21
import
jxl.write.WritableWorkbook;
22
23
public
class
JXLExample{
24
25
/**
26
*數據庫導出至Excel表格
27
*/
28
public
static
void
main(String[]args){
29
//
準備設置excel工作表的標題
30
String[]title
=
{
"
編號
"
,
"
產品名稱
"
,
"
產品價格
"
,
"
產品數量
"
,
"
生產日期
"
,
"
產地
"
,
"
是否出口
"
};
31
try
{
32
//
獲得開始時間
33
long
start
=
System.currentTimeMillis();
34
//
輸出的excel的路徑
35
StringfilePath
=
"
e://testJXL.xls
"
;
36
//
創建Excel工作薄
37
WritableWorkbookwwb;
38
//
新建立一個jxl文件,即在e盤下生成testJXL.xls
39
OutputStreamos
=
new
FileOutputStream(filePath);
40
wwb
=
Workbook.createWorkbook(os);
41
//
添加第一個工作表并設置第一個Sheet的名字
42
WritableSheetsheet
=
wwb.createSheet(
"
產品清單
"
,
0
);
43
Labellabel;
44
for
(
int
i
=
0
;i
<
title.length;i
++
){
45
//
Label(x,y,z)代表單元格的第x+1列,第y+1行,內容z
46
//
在Label對象的子對象中指明單元格的位置和內容
47
label
=
new
Label(i,
0
,title[i]);
48
//
將定義好的單元格添加到工作表中
49
sheet.addCell(label);
50
}
51
//
下面是填充數據
52
/*
53
*保存數字到單元格,需要使用jxl.write.Number
54
*必須使用其完整路徑,否則會出現錯誤
55
*
*/
56
//
填充產品編號
57
jxl.write.Numbernumber
=
new
jxl.write.Number(
0
,
1
,
20071001
);
58
sheet.addCell(number);
59
//
填充產品名稱
60
label
=
new
Label(
1
,
1
,
"
金鴿瓜子
"
);
61
sheet.addCell(label);
62
/*
63
*定義對于顯示金額的公共格式
64
*jxl會自動實現四舍五入
65
*例如2.456會被格式化為2.46,2.454會被格式化為2.45
66
*
*/
67
jxl.write.NumberFormatnf
=
new
jxl.write.NumberFormat(
"
#.##
"
);
68
jxl.write.WritableCellFormatwcf
=
new
jxl.write.WritableCellFormat(nf);
69
//
填充產品價格
70
jxl.write.Numbernb
=
new
jxl.write.Number(
2
,
1
,
2.45
,wcf);
71
sheet.addCell(nb);
72
//
填充產品數量
73
jxl.write.Numbernumb
=
new
jxl.write.Number(
3
,
1
,
200
);
74
sheet.addCell(numb);
75
/*
76
*定義顯示日期的公共格式
77
*如:yyyy-MM-ddhh:mm
78
*
*/
79
SimpleDateFormatsdf
=
new
SimpleDateFormat(
"
yyyy-MM-dd
"
);
80
Stringnewdate
=
sdf.format(
new
Date());
81
//
填充出產日期
82
label
=
new
Label(
4
,
1
,newdate);
83
sheet.addCell(label);
84
//
填充產地
85
label
=
new
Label(
5
,
1
,
"
陜西西安
"
);
86
sheet.addCell(label);
87
/*
88
*顯示布爾值
89
*
*/
90
jxl.write.Booleanbool
=
new
jxl.write.Boolean(
6
,
1
,
true
);
91
sheet.addCell(bool);
92
/*
93
*合并單元格
94
*通過writablesheet.mergeCells(intx,inty,intm,intn);來實現的
95
*表示將從第x+1列,y+1行到m+1列,n+1行合并
96
*
97
*
*/
98
sheet.mergeCells(
0
,
3
,
2
,
3
);
99
label
=
new
Label(
0
,
3
,
"
合并了三個單元格
"
);
100
sheet.addCell(label);
101
/*
102
*
103
*定義公共字體格式
104
*通過獲取一個字體的樣式來作為模板
105
*首先通過web.getSheet(0)獲得第一個sheet
106
*然后取得第一個sheet的第二列,第一行也就是"產品名稱"的字體
107
*
*/
108
CellFormatcf
=
wwb.getSheet(
0
).getCell(
1
,
0
).getCellFormat();
109
WritableCellFormatwc
=
new
WritableCellFormat();
110
//
設置居中
111
wc.setAlignment(Alignment.CENTRE);
112
//
設置邊框線
113
wc.setBorder(Border.ALL,BorderLineStyle.THIN);
114
//
設置單元格的背景顏色
115
wc.setBackground(jxl.format.Colour.RED);
116
label
=
new
Label(
1
,
5
,
"
字體
"
,wc);
117
sheet.addCell(label);
118
119
//
設置字體
120
jxl.write.WritableFontwfont
=
new
jxl.write.WritableFont(WritableFont.createFont(
"
隸書
"
),
20
);
121
WritableCellFormatfont
=
new
WritableCellFormat(wfont);
122
label
=
new
Label(
2
,
6
,
"
隸書
"
,font);
123
sheet.addCell(label);
124
125
//
寫入數據
126
wwb.write();
127
//
關閉文件
128
wwb.close();
129
long
end
=
System.currentTimeMillis();
130
System.out.println(
"
----完成該操作共用的時間是:
"
+
(end
-
start)
/
1000
);
131
}
catch
(Exceptione){
132
System.out.println(
"
---出現異常---
"
);
133
e.printStackTrace();
134
}
135
}
136
137
}
<script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; /* 728x90, 大橫幅正文下方 */ google_ad_slot = "4725362798"; google_ad_width = 728; google_ad_height = 90; // --></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
<!--Google 728*90橫幅廣告結束-->
<script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; /* 468x15 橫鏈接單元 */ google_ad_slot = "5785741422"; google_ad_width = 468; google_ad_height = 15; // --></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
<!--新Google 468x15 橫鏈接單元結束-->
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
