Java代碼 /* * Created on Dec 30, 2007 * * To change the template for this generated file go to * Window>PReferences>Java>Code Generation>Code and Comments */ package JExcelTest.standard;
/** * @author Ken * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class CreateXLS {
public static void main(String[] args) { try { //open file. WritableWorkbook book = Workbook.createWorkbook(new File("d:/Test.xls"));
//create Sheet named "Sheet_1". 0 means this is 1st page. WritableSheet sheet = book.createSheet("Sheet_1", 0);
//define cell column and row in Label Constructor, and cell content write "test". //cell is 1st-Column,1st-Row. value is "test". Label label = new Label(0, 0, "test"); //add defined cell above to sheet instance. sheet.addCell(label);
//create cell using add numeric. WARN:necessarily use integrated package-path, otherwise will be throws path-error. //cell is 2nd-Column, 1st-Row. value is 789.123. jxl.write.Number number = new jxl.write.Number(1, 0, 789.123); //add defined cell above to sheet instance. sheet.addCell(number);
//add defined all cell above to case. book.write(); //close file case. book.close(); } catch (Exception e) { e.printStackTrace(); } } } Java編譯執行后,會在當前位置產生一個Excel文件。
二、讀取文件 以剛才我們創建的Excel文件為例,做一個簡單的讀取操作,程序代碼如下:
Java代碼 /* * Created on Dec 30, 2007 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package JExcelTest.standard;
import java.io.*; import jxl.*;
/** * @author Ken * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class ReadXLS {
三、修改文件 利用jExcelAPI可以修改已有的Excel文件,修改Excel文件的時候,除了打開文件的方式不同之外,其他操作和創建Excel是一樣的。下面的例子是在我們已經生成的Excel文件中添加一個工作表: 修改Excel的類,添加一個工作表 Java代碼 /* * Created on Dec 30, 2007 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package JExcelTest.standard;
/** * @author Ken * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class UpdateXLS {
public static void main(String[] args) { try { //get file. Workbook wb = Workbook.getWorkbook(new File("d:/Test.xls")); //open a copy file(new file), then write content with same content with Test.xls. WritableWorkbook book = Workbook.createWorkbook(new File("d:/Test.xls"), wb); //add a Sheet. WritableSheet sheet = book.createSheet("Sheet_2", 1); sheet.addCell(new Label(0, 0, "test2")); book.write(); book.close(); } catch (Exception e) { e.printStackTrace(); } } }
高級操作
一、 數據格式化 在Excel中不涉及復雜的數據類型,能夠比較好的處理字串、數字和日期已經能夠滿足一般的應用。 字串格式化 字符串的格式化涉及到的是字體、粗細、字號等元素,這些功能主要由WritableFont和WritableCellFormat類來負責。假設我們在生成一個含有字串的單元格時,使用如下語句,為方便敘述,我們為每一行命令加了編號: WritableFont font1= new WritableFont(WritableFont.TIMES,16,WritableFont.BOLD); 或 //設置字體格式為excel支持的格式 WritableFont font3=new WritableFont(WritableFont.createFont("楷體_GB2312"),12,WritableFont.NO_BOLD ); ① WritableCellFormat format1=new WritableCellFormat(font1); ② Label label=new Label(0,0,”data 4 test”,format1) ③ 其中 I.指定了字串格式:字體為TIMES,字號16,加粗顯示。WritableFont有非常豐富的構造子,供不同情況下使用,jExcelAPI的java-doc中有詳細列表,這里不再列出。 II.處代碼使用了WritableCellFormat類,這個類非常重要,通過它可以指定單元格的各種屬性,后面的單元格格式化中會有更多描述。 III.處使用了Label類的構造子,指定了字串被賦予那種格式。 在WritableCellFormat類中,還有一個很重要的方法是指定數據的對齊方式,比如針對我們上面的實例,可以指定: //把水平對齊方式指定為居中 format1.setAlignment(jxl.format.Alignment.CENTRE); //把垂直對齊方式指定為居中 format1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); //設置自動換行 format1.setWrap(true);