
<!--Google 468*60橫幅廣告開(kāi)始--><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橫幅廣告結(jié)束-->
要往xls文件里面寫(xiě)入數(shù)據(jù)的時(shí)候需要注意的是第一要新建一個(gè)xls文件
OutputStream os=new FileOutputStream("c://excel2.xls");
再建完這個(gè)文件的時(shí)候再建立工作文件
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File(os));
如果這個(gè)文件已經(jīng)存在,那么我們可以在這個(gè)文件里面加入一個(gè)sheet為了和以前的數(shù)據(jù)進(jìn)行分開(kāi);
jxl.write.WritableSheet ws = wwb.createSheet("Test Sheet 1", 0);
在createSheet方法里前面的參數(shù)是sheet名,后面是要操作的sheet號(hào)
接下來(lái)就可以往這個(gè)文件里面寫(xiě)入數(shù)據(jù)了
寫(xiě)入數(shù)據(jù)的時(shí)候注意的格式
(1)添加的字體樣式
jxl.write.WritableFont wf = new jxl.write.WritableFont(WritableFont.TIMES, 18, WritableFont.BOLD, true);
WritableFont()方法里參數(shù)說(shuō)明:
這個(gè)方法算是一個(gè)容器,可以放進(jìn)去好多屬性
第一個(gè): TIMES是字體大小,他寫(xiě)的是18
第二個(gè): BOLD是判斷是否為斜體,選擇true時(shí)為斜體
第三個(gè): ARIAL
第四個(gè): UnderlineStyle.NO_UNDERLINE 下劃線
第五個(gè): jxl.format.Colour.RED 字體顏色是紅色的
jxl.write.WritableCellFormat wcfF = new jxl.write.WritableCellFormat(wf);
jxl.write.Label labelC = new jxl.write.Label(0, 0, "This is a Label cell",wcfF);
ws.addCell(labelC);
在Label()方法里面有三個(gè)參數(shù)
第一個(gè)是代表列數(shù),
第二是代表行數(shù),
第三個(gè)代表要寫(xiě)入的內(nèi)容
第四個(gè)是可選項(xiàng),是輸入這個(gè)label里面的樣式
然后通過(guò)寫(xiě)sheet的方法addCell()把內(nèi)容寫(xiě)進(jìn)sheet里面。
(2)添加帶有formatting的Number對(duì)象
jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");
(3)添加Number對(duì)象
(3.1)顯示number對(duì)象數(shù)據(jù)的格式
jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");
jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf);
jxl.write.Number labelNF = new jxl.write.Number(1, 1, 3.1415926, wcfN);
ws.addCell(labelNF);
Number()方法參數(shù)說(shuō)明:
前兩上表示輸入的位置
第三個(gè)表示輸入的內(nèi)容
(4)添加Boolean對(duì)象
jxl.write.Boolean labelB = new jxl.write.Boolean(0, 2, false);
ws.addCell(labelB);
(5)添加DateTime對(duì)象
jxl.write.DateTime labelDT = new jxl.write.DateTime(0, 3, new java.util.Date());
ws.addCell(labelDT);
DateTime()方法的參數(shù)說(shuō)明
前兩個(gè)表示輸入的位置
第三個(gè)表示輸入的當(dāng)前時(shí)間
(6)添加帶有formatting的DateFormat對(duì)象
這個(gè)顯示當(dāng)前時(shí)間的所有信息,包括年月日小時(shí)分秒
jxl.write.DateFormat df = new jxl.write.DateFormat("dd MM yyyy hh:mm:ss");
jxl.write.WritableCellFormat wcfDF = new jxl.write.WritableCellFormat(df);
jxl.write.DateTime labelDTF = new jxl.write.DateTime(1, 3, new java.util.Date(), wcfDF);
ws.addCell(labelDTF);
(7)添加帶有字體顏色Formatting的對(duì)象
jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false,UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);
jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc);
import="jxl.format.*
jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.ARIAL,20,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.GREEN);
(8)設(shè)置單元格樣式
jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc);
wcfFC.setBackGround(jxl.format.Colour.RED);//設(shè)置單元格的顏色為紅色
wcfFC = new jxl.write.Label(6,0,"i love china",wcfFC);
本文轉(zhuǎn)載自網(wǎng)上,原作者已不可考,在此小李謹(jǐn)向原作者致敬。
<!--新Google 468*60橫幅廣告開(kāi)始--><script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; /* 468x60, 創(chuàng)建于 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橫幅廣告結(jié)束-->
<!--新Google 468x15 橫鏈接單元開(kāi)始--><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 橫鏈接單元結(jié)束-->
<!-- Google Reader shared發(fā)布代碼開(kāi)始 --><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ā)布代碼結(jié)束 -->
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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