點擊這里使用RSS訂閱本Blog:
歡迎來訪張軍網(wǎng)站:連續(xù)登錄【5】天博主自動為您充值【5】積分哦
歡迎來訪張軍網(wǎng)站:連續(xù)登錄【5】天博主自動為您充值【5】積分哦
能夠操作excel的開源項目jexcelapi
系統(tǒng)
2336 0
<!-- Feedsky FEED發(fā)布代碼開始 --> <!-- FEED自動發(fā)現(xiàn)標記開始 --> 點擊這里使用RSS訂閱本Blog: <link rel="alternate" type="application/rss+xml" title="RSS 2.0"> <!-- FEED自動發(fā)現(xiàn)標記結束 --><script language="javascript"><!-- main_sub="c1s67"; more_subs=""; --> </script><script language="javascript" src="http://www.feedsky.com/jsout/publishlist_v2.js?burl=softwave&out_html=true"></script><!-- Feedsky FEED發(fā)布代碼結束 -->
jexcelapi是一個開源項目,主要用來操作excel.
主頁地址:
http://www.andykhan.com/jexcelapi/
現(xiàn)在做一個項目用到了它,根據(jù)不同的公司生成不同的文件夾,
在相應的文件夾下生成對應的xls. 這里只帖出生成xls部分核心代碼:
public
void
generateXls()
{
try
{
/***/
/**
**********創(chuàng)建工作簿************
*/
WritableWorkbookworkbook
=
Workbook.createWorkbook(
new
File(
"
d:/test.xls
"
));
/***/
/**
**********創(chuàng)建工作表************
*/
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
,
"
工作經(jīng)驗:
"
,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
,
"
詳細數(shù)據(jù)
"
,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內(nèi)容
//
這里用你的dao
TestDAOdao
=
new
TestDAO();
Listlist
=
dao.findBy(user.getUserId());
System.out.println(
"
此保單擁有防疫碼數(shù)量:
"
+
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));
}
/***/
/**
**********以上所寫的內(nèi)容都是寫在緩存中的,下一句將緩存的內(nèi)容寫到文件中********
*/
workbook.write();
/***/
/**
*********關閉文件*************
*/
workbook.close();
System.out.println(
"
導出成功
"
);
//
存放url地址
}
catch
(Exceptione)
{
System.out.println(
"
在輸出到EXCEL的過程中出現(xiàn)錯誤,錯誤原因:
"
+
e.toString());
}
}
完整源碼可以在
這里下載
摘自【http://www.blogjava.net/supercrsky/archive/2008/05/21/201810.html】
<!-- Google Reader shared發(fā)布代碼開始 --> <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發(fā)布代碼結束 -->
能夠操作excel的開源項目jexcelapi
更多文章、技術交流、商務合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
主站蜘蛛池模板:
昌都县|
新野县|
平舆县|
金乡县|
巴彦淖尔市|
潞城市|
扎鲁特旗|
灵台县|
西昌市|
蚌埠市|
云和县|
新源县|
宜君县|
高密市|
棋牌|
宁乡县|
大同市|
长沙市|
多伦县|
太保市|
太仓市|
海丰县|
阿拉尔市|
丰台区|
金秀|
象山县|
德安县|
措勤县|
靖远县|
星子县|
阿拉善右旗|
武定县|
马山县|
芒康县|
宁国市|
青龙|
社旗县|
黎城县|
安丘市|
东乌|
大宁县|