點擊這里使用RSS訂閱本Blog:
歡迎來訪張軍網站:連續登錄【5】天博主自動為您充值【5】積分哦
歡迎來訪張軍網站:連續登錄【5】天博主自動為您充值【5】積分哦
能夠操作excel的開源項目jexcelapi
系統
2190 0
<!-- Feedsky FEED發布代碼開始 --> <!-- FEED自動發現標記開始 --> 點擊這里使用RSS訂閱本Blog: <link rel="alternate" type="application/rss+xml" title="RSS 2.0"> <!-- FEED自動發現標記結束 --><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發布代碼結束 -->
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());
}
}
完整源碼可以在
這里下載
摘自【http://www.blogjava.net/supercrsky/archive/2008/05/21/201810.html】
<!-- 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發布代碼結束 -->
能夠操作excel的開源項目jexcelapi
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
主站蜘蛛池模板:
惠来县|
丰镇市|
成都市|
龙里县|
理塘县|
黄陵县|
东平县|
奉新县|
长治市|
呼伦贝尔市|
白玉县|
衡阳县|
方山县|
达日县|
沈丘县|
潢川县|
五峰|
上思县|
辽阳县|
广昌县|
墨玉县|
泗水县|
申扎县|
金山区|
万盛区|
遵义市|
凤山县|
绵竹市|
潜江市|
乌鲁木齐市|
辽阳县|
义乌市|
五华县|
菏泽市|
红桥区|
尉犁县|
合山市|
凌源市|
犍为县|
扬州市|
望都县|