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);