Java Excel是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容、创建新的Excel文件、更新已经存在的Excel文件。使用该 API非Windows操作系统也可以通过纯Java应用来处理Excel数据表。因为是使用Java编写的,所以我们在Web应用中可以通过JSP、 Servlet来调用API实现对Excel数据表的访问。
现在发布的稳定版本是V2.0,提供以下功能:
- 从Excel 95、97、2000等格式的文件中读取数据;
- 读取Excel公式(可以读取Excel 97以后的公式);
- 生成Excel数据表(格式为Excel 97);
- 支持字体、数字、日期的格式化;
- 支持单元格的阴影操作,以及颜色操作;
- 修改已经存在的数据表;
现在还不支持以下功能,但不久就会提供了:
- 不能够读取图表信息;
- 可以读,但是不能生成公式,任何类型公式最后的计算值都可以读出;
Java Excel API既可以从本地文件系统的一个文件(.xls),也可以从输入流中读取Excel数据表。读取Excel数据表的第一步是创建Workbook(术语:工作薄),下面的代码片段举例说明了应该如何操作,读xls文档:
- package liu.createExcel;
- /*
- * Created on Dec 30, 2007
- *
- * To change the template for this generated file go to
- * Window>Preferences>Java>Code Generation>Code and Comments
- */
- import java.io.*;
- import jxl.*;
- /**
- * @author 933soft
- *
- * To change the template for this generated type comment go to
- * Window>Preferences>Java>Code Generation>Code and Comments
- */
- public class ReadXLS {
- public static void main(String[] args) {
- try {
- //InputStream is = new FileInputStream(new File("d://GSI-11336263.xls"));
- Workbook book = Workbook.getWorkbook(new File("d://GSI-11336263.xls"));
- //get a Sheet object.
- Sheet sheet = book.getSheet(0);
- String sheetName = sheet.getName() ;
- System.out.println("sheet 名称:"+sheetName) ;
- int sheetColumns = sheet.getColumns() ;
- System.out.println("sheet列数:"+sheetColumns) ;
- Cell[] cell = sheet.getColumn(0) ;
- for(int i=0 ;i<cell.length;i++){
- System.out.println("cell0列第"+(i+1)+"行的值 :"+cell[i].getContents()) ;
- }
- int sheetRows = sheet.getRows() ;
- System.out.println("sheet的总行数 :"+sheetRows) ;
- Cell[] sheetRowCell =sheet.getRow(0) ;
- System.out.println(sheetRowCell.length) ;
- for(int j=0 ;j <sheetRowCell.length ;j++){
- System.out.println("cell1列行第"+(j+1)+"列行的值 :"+sheetRowCell[j].getContents()) ;
- }
- //get 1st-Column,1st-Row content.
- Cell cell00 = sheet.getCell(0, 0);
- String result00 = cell00.getContents();
- System.out.println("Cell(0, 0)" + " value : " + result00 + "; type : " + cell00.getType());
- Cell cell01 = sheet.getCell(0, 1) ;
- String result01= cell01.getContents() ;
- System.out.println("Cell(0, 1)" + " value : " + result01 + "; type : " + cell01.getType());
- book.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
创建xls文档:
- /**
- *
- */
- package liu.createExcel;
- /*
- * Created on Dec 30, 2007
- *
- * To change the template for this generated file go to
- * Window>Preferences>Java>Code Generation>Code and Comments
- */
- import java.awt.Color;
- import java.io.*;
- import jxl.*;
- import jxl.format.UnderlineStyle;
- import jxl.write.*;
- import jxl.write.Boolean;
- import jxl.write.Number;
- /**
- * @author 933soft
- *
- * 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 {
- WritableWorkbook ww = Workbook.createWorkbook(new File(
- "d://test.xls"));
- WritableSheet ws = ww.createSheet("Test Sheet 1", 0);
- Label labelc = new Label(0, 0, "This is a Test LabelValue");
- ws.addCell(labelc);
- // 添加带有字型Formatting的对象
- WritableFont first = new WritableFont(WritableFont.TIMES, 18,
- WritableFont.BOLD, true);
- WritableCellFormat firstwcf = new WritableCellFormat(first);
- Label labelCF = new Label(1, 0, "this is a label Cell", firstwcf);
- ws.addCell(labelCF);
- // 添加带有字体颜色Formatting的对象
- WritableFont second = new WritableFont(WritableFont.ARIAL, 10,
- WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
- Colour.RED);
- WritableCellFormat secondwcf = new WritableCellFormat(second) ;
- Label labelSecond = new Label(1, 0, "this is a label Cell", secondwcf);
- ws.addCell(labelSecond) ;
- //添加Number对象
- Number labelN = new Number(0,1,3.141592653) ;
- ws.addCell(labelN) ;
- //添加带有formatting的Number对象
- NumberFormat nf = new NumberFormat("#.##") ;
- WritableCellFormat wcfnf = new WritableCellFormat(nf) ;
- Number nff = new Number(1,1,3.141592653,wcfnf) ;
- ws.addCell(nff) ;
- //添加Boolean对象
- Boolean labelb = new Boolean(0,2,false) ;
- ws.addCell(labelb) ;
- /* //添加DateTime对象
- DateTime labelDT = new DateTime(0,3,new java.util.Date()) ;
- ws.addCell(labelDT) ;
- //添加带有formatting的DateFormat对象
- DateFormat df = new DateFormat("dd MM yyyy hh:mm:ss") ;
- WritableCellFormat wcfDF = new WritableCellFormat(df) ;
- DateTime dtf = new DateTime(1,3,new java.util.Date(),wcfDF) ;
- ws.addCell(dtf) ;*/
- System.out.println(new java.util.Date()) ;
- //4.添加DateTime对象
- jxl.write.DateTime labelDT = new jxl.write.DateTime(0, 3, new java.util.Date());
- ws.addCell(labelDT);
- //添加带有formatting的DateFormat对象
- 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);
- ww.write() ;
- ww.close() ;
- System.out.println("创建成功") ;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
修改xls文档:
- package liu.createExcel;
- import java.io.File;
- import jxl.CellType;
- import jxl.Workbook;
- import jxl.write.Label;
- import jxl.write.WritableCell;
- import jxl.write.WritableSheet;
- import jxl.write.WritableWorkbook;
- /**
- *
- * @author 933soft
- *
- */
- public class UpdateXLS {
- public static void main(String[] args)throws Exception{
- Workbook rw = Workbook.getWorkbook(new File("d://test.xls")) ;
- WritableWorkbook wwb = Workbook.createWorkbook(new File("d://test1.xls"), rw) ;
- WritableSheet ws = wwb.getSheet(0) ;
- WritableCell wc = ws.getWritableCell(0, 0) ;
- if(wc.getType() == CellType.LABEL){
- Label l = (Label)wc ;
- l.setString("the value has been modified!!") ;
- }
- wwb.write() ;
- wwb.close() ;
- rw.close() ;
- System.out.println("修改成功!!") ;
- }
- }