歡迎點擊此處訂閱本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元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
主站蜘蛛池模板:
不卡一区二区在线观看
|
精品牛牛影视久久精品
|
四虎影院新网址
|
91手机在线视频观看
|
欧美日韩亚洲无线码在线观看
|
国产精品日韩在线观看
|
国产日韩欧美综合一区
|
国产国拍亚洲精品福利
|
亚洲qingse中文久久网
|
久久伊人一区二区三区四区
|
日韩中文字幕在线亚洲一区
|
夜色91|
国产亚洲精品sese在线播放
|
欧美日韩北条麻妃一区二区
|
亚洲狠狠|
国产精品国产三级国产无毒
|
免费高清一级欧美片在线观看
|
99久久精品国产交换
|
免费看欧美毛片大片免费看
|
在线观看一级毛片免费
|
xxxx免费观看
|
91麻豆精品国产91久久久久
|
午夜操操
|
视频一区国产精戏刘婷
|
在线观看精品国产
|
一级特级aa欧美毛片
|
亚洲欧美bt|
国产精品久久久视频
|
国产精品久久久久久久久久98
|
九九久久久2
|
日韩视频 中文字幕 视频一区
|
纯欧美一级毛片免费
|
欧美一级精品高清在线观看
|
国产精品免费大片
|
欧美在线综合
|
久久久精品影院
|
12345国产精品高清在线
|
色综合久久久久久久久久久
|
老司机午夜免费影院
|
日韩一区二区天海翼
|
欧美午夜寂寞影院安卓列表
|