首先,請到http://www.andykhan.com/jexcelapi/index.html下載java excel api,主頁上同時有比較詳細的介紹。最新版本為2.4.3,同時也可以到:http://www.andykhan.com/jexcelapi/jexcelapi_2_4_3.tar.gz下載到該最新版的API,由于該項目是開源的,所以下載的文件中已經包含了源代碼,同樣的,文件中也有javadoc,大家在開發中可以參考javadoc。
下載完畢后,我們需要把文件中的jxl.jar加入到你的開發classpath中。
下圖是現在要生產的excel截圖:
http://blog.csdn.net/beming/gallery/image/3437.aspx
代碼如下:
???File excel = new File("d:/aming.xls");
???if(!excel.exists()){
????excel.createNewFile();
???}???
???WritableWorkbook wwb = Workbook.createWorkbook(excel);
???WritableSheet ws = wwb.createSheet("testexcel",0);
???Label lable = null;
???
???//對中文的支持非常好
???lable = new Label(0,0,"我的中國心");
???ws.addCell(lable);
???
???//可以定義模板格式化你的cell
???WritableFont wf = new WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
???WritableCellFormat wcf = new WritableCellFormat(wf);
???wcf.setBackground(Colour.WHITE);
???lable = new Label(0,1,"fdsl",wcf);
???ws.addCell(lable);
???
???wf = new WritableFont(WritableFont.TIMES,18,WritableFont.BOLD,true);
???wcf = new WritableCellFormat(wf);
???lable = new Label(0,2,"aming",wcf);
???ws.addCell(lable);
???
???//cell的類型同樣可以定義為數字類型
???Number nb = new Number(0,3,21.4321321);
???ws.addCell(nb);
???
???//支持格式化你的數字串
???NumberFormat nf = new NumberFormat("#.###");
???wcf = new WritableCellFormat(nf);
???nb = new Number(0,4,21.43254354354354,wcf);
???ws.addCell(nb);
???//cell的類型可以為boolean類型
???Boolean bl = new Boolean(0,5,true);
???ws.addCell(bl);
???//cell的類型同樣可以為日期,時間
???DateTime dt = new DateTime(0,6,new Date());
???ws.addCell(dt);
???//并且可以很好格式化你的日期格式
???DateFormat df = new DateFormat("MM dd yyyy hh:mm:ss");
???wcf = new WritableCellFormat(df);
???dt = new DateTime(0,7,new Date(),wcf);
???ws.addCell(dt);
???
???//開始寫文件了
???wwb.write();
???wwb.close();
上面的下載地址無法打開.
下載請到:http://prdownloads.sourceforge.net/jexcelapi
posted on 2006-05-22 16:54
LORD BLOG 閱讀(395)
評論(0) 編輯 收藏 所屬分類:
J2EE