Java Blog for Alex Wan

          Let life be beautiful like summer flowers and death like autumn leaves.

          統(tǒng)計(jì)

          留言簿(10)

          BlogJava

          Blogs

          DIV+CSS

          JQuery相關(guān)

          友情鏈接

          常去的地方

          數(shù)據(jù)供應(yīng)

          閱讀排行榜

          評(píng)論排行榜

          jxl讀寫excel的簡(jiǎn)單例子

          jxltest.java
          import java.io.File;
          import java.io.FileOutputStream;

          import jxl.Workbook;
          import jxl.format.BorderLineStyle;
          import jxl.format.VerticalAlignment;
          import jxl.write.Label;
          import jxl.write.Number;
          import jxl.write.WritableFont;
          import jxl.write.WritableSheet;
          import jxl.write.WritableWorkbook;

          public class jxltest
          {
          public static void main(String [] args)
          {
          String templatePath 
          = "c:\\template.xls";//模板文件名
          String outFileStr = "c:\\test.xls";//測(cè)試文件名(輸出文件)
          try
          {
          //創(chuàng)建小字體:Arial,大小為8號(hào),非粗體,非斜體
          WritableFont wf = new WritableFont(WritableFont.ARIAL, 8,WritableFont.NO_BOLD, false);
          //字體顏色為紅色
          wf.setColour(jxl.format.Colour.RED);
          //創(chuàng)建大字體:Arial,大小為18號(hào),粗體,非斜體
          WritableFont Bwf = new WritableFont(WritableFont.ARIAL, 18,WritableFont.NO_BOLD, false);
          Bwf.setColour(jxl.format.Colour.RED);
          //創(chuàng)建單元格格式:設(shè)置水平對(duì)齊為向右對(duì)齊
          jxl.write.WritableCellFormat RwcfF = new jxl.write.WritableCellFormat(wf);
          RwcfF.setAlignment(jxl.write.Alignment.RIGHT);
          //創(chuàng)建單元格格式:設(shè)置水平對(duì)齊為向左對(duì)齊
          jxl.write.WritableCellFormat LwcfF = new jxl.write.WritableCellFormat(wf);
          LwcfF.setAlignment(jxl.write.Alignment.LEFT);
          //創(chuàng)建單元格格式:設(shè)置水平對(duì)齊為居中對(duì)齊
          jxl.write.WritableCellFormat CwcfF = new jxl.write.WritableCellFormat(wf);
          CwcfF.setAlignment(jxl.write.Alignment.CENTRE);
          jxl.write.WritableCellFormat CBwcfF 
          = new jxl.write.WritableCellFormat(Bwf);
          CBwcfF.setAlignment(jxl.write.Alignment.CENTRE);
          //設(shè)置垂直對(duì)齊為居中對(duì)齊
          CBwcfF.setVerticalAlignment(VerticalAlignment.CENTRE);
          //設(shè)置頂部邊框線為實(shí)線(默認(rèn)是黑色--也可以設(shè)置其他顏色)
          CBwcfF.setBorder(jxl.format.Border.TOP, BorderLineStyle.MEDIUM);
          //設(shè)置右邊框線為實(shí)線
          CBwcfF.setBorder(jxl.format.Border.RIGHT, BorderLineStyle.MEDIUM);
          //設(shè)置頂部框線為實(shí)線
          CBwcfF.setBorder(jxl.format.Border.BOTTOM, BorderLineStyle.MEDIUM);
          jxl.write.WritableCellFormat CMwcfF 
          = new jxl.write.WritableCellFormat(wf);
          CMwcfF.setAlignment(jxl.write.Alignment.LEFT);
          //設(shè)置垂直對(duì)齊為向上對(duì)齊
          CMwcfF.setVerticalAlignment(VerticalAlignment.TOP);
          CMwcfF.setWrap(
          true);
          File tFile 
          = new File(templatePath);//創(chuàng)建模板文件對(duì)象
          File outFile = new File(outFileStr);//創(chuàng)建輸出文件對(duì)象
          //創(chuàng)建文件輸出流對(duì)象
          FileOutputStream os = new FileOutputStream(outFile);
          //模板工作簿對(duì)象
          Workbook tBook = Workbook.getWorkbook(tFile);
          //輸出工作簿對(duì)象
          WritableWorkbook wbook = Workbook.createWorkbook(os, tBook);
          //在坐標(biāo)為(0,0)的單元格寫入"測(cè)試"字符串使用8號(hào)紅色小字體,向右對(duì)齊
          wsheet.addCell(new Label(00"test", RwcfF));
          //在坐標(biāo)為(1,1)的單元格寫入"test"字符串使用8號(hào)紅色小字體,向左對(duì)齊
          wsheet.addCell(new Label(11"test", LwcfF));
          //在坐標(biāo)為(2,2)的單元格寫入"測(cè)試test"字符串使用8號(hào)紅色小字體,居中對(duì)齊
          wsheet.addCell(new Label(2,2"測(cè)試test", CMwcfF));
          //在坐標(biāo)為(3,3)的單元格寫入2.00使用18號(hào)紅色大字體,居中對(duì)齊
          wsheet.addCell(new Number(23142.00, CBwcfF));
          //寫入
          wbook.write();
          wbook.close();
          tBook.close();
          //關(guān)閉文件輸出流
          os.close();     
          }

          catch(Exception e)
          {
          System.out.println(e.getMessage());
          e.printStackTrace();
          }


          }


          }
           



          Let life be beautiful like summer flowers and death like autumn leaves.

          posted on 2008-06-07 10:26 Alexwan 閱讀(4041) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 蒙阴县| 新邵县| 搜索| 五莲县| 友谊县| 黄梅县| 库车县| 通州区| 集安市| 竹山县| 平湖市| 揭东县| 尖扎县| 盈江县| 印江| 西乡县| 西充县| 天柱县| 内江市| 兴义市| 长葛市| 五寨县| 石柱| 临猗县| 青海省| 宁陵县| 定陶县| 固安县| 永丰县| 南江县| 亳州市| 措美县| 颍上县| 林周县| 洪洞县| 宾阳县| 渝北区| 肃宁县| 公主岭市| 新泰市| 岳阳县|