歡迎點擊此處訂閱本Blog" />
歡迎來訪張軍網站:連續登錄【5】天博主自動為您充值【5】積分哦
歡迎來訪張軍網站:連續登錄【5】天博主自動為您充值【5】積分哦
使用開源項目JExcelApi在Java環境中生成MS Exce
系統
1812 0
<!-- Feedsky FEED發布代碼開始 -->
歡迎點擊此處訂閱本Blog
<!-- FEED自動發現標記開始 --> <link title="RSS 2.0" type="application/rss+xml" rel="alternate"> <!-- FEED自動發現標記結束 -->
<!--Google 468*60橫幅廣告開始--><script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; google_ad_width = 468; google_ad_height = 60; google_ad_format = "468x60_as"; google_ad_type = "image"; //2007-07-26: CSDN google_ad_channel = "6063905817"; google_color_border = "6699CC"; google_color_bg = "E6E6E6"; google_color_link = "FFFFFF"; google_color_text = "333333"; google_color_url = "AECCEB"; google_ui_features = "rc:6"; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script><!--Google 468*60橫幅廣告結束-->
本文轉載自:http://www.blogjava.net/supercrsky/archive/2008/05/21/201810.html
jexcelapi是一個開源項目,主要用來操作excel.
主頁地址:
http://www.andykhan.com/jexcelapi/
現在做一個項目用到了它,根據不同的公司生成不同的文件夾,
在相應的文件夾下生成對應的xls. 這里只帖出生成xls部分核心代碼:
public
void
generateXls()
{
try
{
/***/
/**
**********創建工作簿************
*/
WritableWorkbookworkbook
=
Workbook.createWorkbook(
new
File(
"
d:/test.xls
"
));
/***/
/**
**********創建工作表************
*/
WritableSheetsheet
=
workbook.createSheet(
"
工作表名稱
"
,
0
);
/***/
/**
*********設置列寬****************
*/
sheet.setColumnView(
0
,
15
);
//
第1列
sheet.setColumnView(
1
,
18
);
//
第2列
sheet.setColumnView(
2
,
13
);
sheet.setColumnView(
3
,
13
);
sheet.setColumnView(
4
,
15
);
sheet.setColumnView(
5
,
15
);
//
設置行高
sheet.setRowView(
0
,
600
,
false
);
sheet.setRowView(
1
,
400
,
false
);
sheet.setRowView(
7
,
400
,
false
);
//
設置頁邊距
sheet.getSettings().setRightMargin(
0.5
);
//
設置頁腳
sheet.setFooter(
""
,
""
,
"
測試頁腳
"
);
/***/
/**
************設置單元格字體**************
*/
//
字體
WritableFontNormalFont
=
new
WritableFont(WritableFont.ARIAL,
10
);
WritableFontBoldFont
=
new
WritableFont(WritableFont.ARIAL,
14
,
WritableFont.BOLD);
WritableFonttableFont
=
new
WritableFont(WritableFont.ARIAL,
12
,
WritableFont.NO_BOLD);
WritableFontbaodanFont
=
new
WritableFont(WritableFont.ARIAL,
10
,
WritableFont.BOLD);
/***/
/**
************以下設置幾種格式的單元格************
*/
//
用于標題
WritableCellFormatwcf_title
=
new
WritableCellFormat(BoldFont);
wcf_title.setBorder(Border.NONE,BorderLineStyle.THIN);
//
線條
wcf_title.setVerticalAlignment(VerticalAlignment.CENTRE);
//
垂直對齊
wcf_title.setAlignment(Alignment.CENTRE);
//
水平對齊
wcf_title.setWrap(
true
);
//
是否換行
//
用于表格標題
WritableCellFormatwcf_tabletitle
=
new
WritableCellFormat(
tableFont);
wcf_tabletitle.setBorder(Border.NONE,BorderLineStyle.THIN);
//
線條
wcf_tabletitle.setVerticalAlignment(VerticalAlignment.CENTRE);
//
垂直對齊
wcf_tabletitle.setAlignment(Alignment.CENTRE);
//
水平對齊
wcf_tabletitle.setWrap(
true
);
//
是否換行
//
用于正文左
WritableCellFormatwcf_left
=
new
WritableCellFormat(NormalFont);
wcf_left.setBorder(Border.ALL,BorderLineStyle.THIN);
//
線條
wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE);
//
垂直對齊
wcf_left.setAlignment(Alignment.LEFT);
wcf_left.setWrap(
true
);
//
是否換行
//
用于正文左
WritableCellFormatwcf_center
=
new
WritableCellFormat(NormalFont);
wcf_center.setBorder(Border.ALL,BorderLineStyle.THIN);
//
線條
wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE);
//
垂直對齊
wcf_center.setAlignment(Alignment.CENTRE);
wcf_center.setWrap(
true
);
//
是否換行
//
用于正文右
WritableCellFormatwcf_right
=
new
WritableCellFormat(NormalFont);
wcf_right.setBorder(Border.ALL,BorderLineStyle.THIN);
//
線條
wcf_right.setVerticalAlignment(VerticalAlignment.CENTRE);
//
垂直對齊
wcf_right.setAlignment(Alignment.RIGHT);
wcf_right.setWrap(
false
);
//
是否換行
//
用于跨行
WritableCellFormatwcf_merge
=
new
WritableCellFormat(NormalFont);
wcf_merge.setBorder(Border.ALL,BorderLineStyle.THIN);
//
線條
wcf_merge.setVerticalAlignment(VerticalAlignment.TOP);
//
垂直對齊
wcf_merge.setAlignment(Alignment.LEFT);
wcf_merge.setWrap(
true
);
//
是否換行
WritableCellFormatwcf_table
=
new
WritableCellFormat(NormalFont);
wcf_table.setBorder(Border.ALL,BorderLineStyle.THIN);
//
線條
wcf_table.setVerticalAlignment(VerticalAlignment.CENTRE);
//
垂直對齊
wcf_table.setAlignment(Alignment.CENTRE);
wcf_table.setBackground(Colour.GRAY_25);
wcf_table.setWrap(
true
);
//
是否換行
/***/
/**
************單元格格式設置完成******************
*/
//
合并單元格,注意mergeCells(col0,row0,col1,row1)--列從0開始,col1為你要合并到第幾列,行也一樣
sheet.mergeCells(
0
,
0
,
5
,
0
);
sheet.addCell(
new
Label(
0
,
0
,
"
這里是大標題,自定義格式
"
,
wcf_title));
sheet.mergeCells(
0
,
1
,
1
,
1
);
sheet.mergeCells(
2
,
1
,
5
,
1
);
sheet.addCell(
new
Label(
0
,
1
,
""
,wcf_center));
sheet.addCell(
new
Label(
2
,
1
,
"
姓名:
"
+
"
supercrsky
"
,
wcf_center));
sheet.mergeCells(
0
,
2
,
1
,
2
);
sheet.mergeCells(
2
,
2
,
3
,
2
);
sheet.addCell(
new
Label(
0
,
2
,
"
單位:
"
,wcf_center));
sheet.addCell(
new
Label(
2
,
2
,
"
ChinaLong
"
,wcf_center));
sheet.addCell(
new
Label(
4
,
2
,
"
薪水
"
,wcf_center));
sheet.addCell(
new
Label(
5
,
2
,
"
5000
"
,wcf_center));
sheet.mergeCells(
0
,
3
,
1
,
3
);
sheet.mergeCells(
2
,
3
,
3
,
3
);
sheet.addCell(
new
Label(
0
,
3
,
"
性別:
"
,wcf_center));
sheet.addCell(
new
Label(
2
,
3
,
"
男
"
,wcf_center));
sheet.addCell(
new
Label(
4
,
3
,
"
婚否:
"
,wcf_center));
sheet.addCell(
new
Label(
5
,
3
,
"
否
"
,wcf_center));
sheet.mergeCells(
0
,
4
,
1
,
4
);
sheet.mergeCells(
2
,
4
,
3
,
4
);
sheet.addCell(
new
Label(
0
,
4
,
"
是否在職:
"
,wcf_center));
sheet.addCell(
new
Label(
2
,
4
,
"
是
"
,
wcf_center));
sheet.addCell(
new
Label(
4
,
4
,
"
工作經驗:
"
,wcf_center));
sheet.addCell(
new
Label(
5
,
4
,
"
4
"
,wcf_center));
sheet.mergeCells(
0
,
5
,
1
,
5
);
sheet.mergeCells(
2
,
5
,
3
,
5
);
sheet.addCell(
new
Label(
0
,
5
,
"
保險費:
"
,wcf_center));
sheet.addCell(
new
Label(
2
,
5
,
"
50
"
,
wcf_center));
sheet.addCell(
new
Label(
4
,
5
,
"
保險金額:
"
,wcf_center));
sheet.addCell(
new
Label(
5
,
5
,
"
50000
"
,
wcf_center));
sheet.mergeCells(
0
,
6
,
1
,
6
);
sheet.mergeCells(
2
,
6
,
3
,
6
);
sheet.addCell(
new
Label(
0
,
6
,
"
工作地點:
"
,wcf_center));
sheet.addCell(
new
Label(
2
,
6
,
"
北京
"
,wcf_center));
sheet.addCell(
new
Label(
4
,
6
,
"
開心度:
"
,wcf_center));
sheet.addCell(
new
Label(
5
,
6
,
"
一般
"
,wcf_center));
//
另起一table
sheet.mergeCells(
0
,
7
,
5
,
7
);
sheet.addCell(
new
Label(
0
,
7
,
"
詳細數據
"
,wcf_tabletitle));
//
table標題
sheet.addCell(
new
Label(
0
,
8
,
"
序號
"
,wcf_table));
sheet.addCell(
new
Label(
1
,
8
,
"
姓名
"
,wcf_table));
sheet.addCell(
new
Label(
2
,
8
,
"
年齡
"
,wcf_table));
sheet.addCell(
new
Label(
3
,
8
,
"
性別
"
,wcf_table));
sheet.addCell(
new
Label(
4
,
8
,
"
婚否
"
,wcf_table));
sheet.addCell(
new
Label(
5
,
8
,
"
在職
"
,wcf_table));
//
table內容
//
這里用你的dao
TestDAOdao
=
new
TestDAO();
Listlist
=
dao.findBy(user.getUserId());
System.out.println(
"
此保單擁有防疫碼數量:
"
+
list.size());
for
(
int
i
=
0
;i
<
list.size();i
++
)
{
//
對應你的vo類
Userdata
=
(User)list.get(i);
sheet.addCell(
new
Label(
0
,
9
+
i,String.valueOf(i
+
1
),
wcf_center));
sheet.addCell(
new
Label(
1
,
9
+
i,data.getDlEPCode(),
wcf_center));
sheet
.addCell(
new
Label(
2
,
9
+
i,data.getDlType(),
wcf_center));
sheet.addCell(
new
Label(
3
,
9
+
i,String.valueOf(data
.getDlPigAge()),wcf_center));
sheet.addCell(
new
Label(
4
,
9
+
i,
""
,wcf_center));
sheet.addCell(
new
Label(
5
,
9
+
i,
""
,wcf_center));
}
/***/
/**
**********以上所寫的內容都是寫在緩存中的,下一句將緩存的內容寫到文件中********
*/
workbook.write();
/***/
/**
*********關閉文件*************
*/
workbook.close();
System.out.println(
"
導出成功
"
);
//
存放url地址
}
catch
(Exceptione)
{
System.out.println(
"
在輸出到EXCEL的過程中出現錯誤,錯誤原因:
"
+
e.toString());
}
}
完整源碼可以在
這里下載
(源碼空間由文章原作者
々上善若水々
提供)
<!--新Google 468*60橫幅廣告開始--><script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; /* 468x60, 創建于 08-8-6 */ google_ad_slot = "7368701459"; google_ad_width = 468; google_ad_height = 60; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script><!--新Google 468*60橫幅廣告結束-->
<!--新Google 468x15 橫鏈接單元開始--><script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; /* 468x15 橫鏈接單元 */ google_ad_slot = "5785741422"; google_ad_width = 468; google_ad_height = 15; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script><!--新Google 468x15 橫鏈接單元結束-->
<!-- Google Reader shared發布代碼開始 --><script type="text/javascript" src="http://www.google.com/reader/ui/publisher.js"></script><script type="text/javascript" src="http://www.google.com/reader/public/javascript/user/00697638153916680411/state/com.google/broadcast?n=5&callback=GRC_p(%7Bc%3A%22green%22%2Ct%3A%22%5Cu8FD9%5Cu4E9B%5Cu6587%5Cu7AE0%5Cu4E5F%5Cu503C%5Cu5F97%5Cu4E00%5Cu770B%22%2Cs%3A%22false%22%7D)%3Bnew%20GRC"></script><!-- Google Reader shared發布代碼結束 -->
使用開源項目JExcelApi在Java環境中生成MS Excel文檔
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
主站蜘蛛池模板:
国产在线精品香蕉综合网一区
|
5x性区m免费毛片视频看看
|
四虎免费久久影院
|
中文字幕在线观看日韩
|
国产精品免费一级在线观看
|
四虎在线观看网址
|
很很鲁在线视频播放影院
|
免费区一级欧美毛片
|
日韩毛片欧美一级国产毛片
|
国产精品18久久久久网站
|
亚州综合激情另类久久久
|
国产999在线
|
国产久爱青草视频在线观看
|
日本中文字幕二区三区
|
素人259luxu在线观看暴露
|
久久久久久综合成人精品
|
天天操夜夜添
|
国产精品免费看久久久
|
国产精品视频第一区二区
|
99精彩免费观看
|
亚洲精品一区二区三区www
|
色的综合
|
全午夜免费一级毛片
|
久久五月婷
|
人做人爱视频欧美在线观看
|
国产精品a人片在线观看
|
亚洲欧美日韩综合在线
|
91成人免费在线视频
|
日本精品区
|
一级特级aaaa毛片免费观看
|
中文字幕欧美日韩va免费视频
|
色综合精品久久久久久久
|
亚洲一区日韩
|
在线综合+亚洲+欧美中文字幕
|
久久视频精品36线视频在线观看
|
97看片吧|
九天玄帝诀在线观看
|
婷婷亚洲视频
|
亚欧洲精品bb
|
天天天操天天天干
|
九九精品视频一区二区三区
|