xls文档的读写改(使用的jxl版本是2.6.8)

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);
  • 支持字体、数字、日期的格式化;
  • 支持单元格的阴影操作,以及颜色操作;
  • 修改已经存在的数据表;

现在还不支持以下功能,但不久就会提供了:

  1. 不能够读取图表信息;
  2. 可以读,但是不能生成公式,任何类型公式最后的计算值都可以读出;

应用示例


 

1 从Excel文件读取数据表

 

Java Excel API既可以从本地文件系统的一个文件(.xls),也可以从输入流中读取Excel数据表。读取Excel数据表的第一步是创建Workbook(术语:工作薄),下面的代码片段举例说明了应该如何操作,读xls文档:

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. package liu.createExcel;  
  2. /* 
  3.  * Created on Dec 30, 2007 
  4.  * 
  5.  * To change the template for this generated file go to 
  6.  * Window>Preferences>Java>Code Generation>Code and Comments 
  7.  */  
  8. import java.io.*;  
  9. import jxl.*;  
  10. /** 
  11.  * @author 933soft 
  12.  * 
  13.  * To change the template for this generated type comment go to 
  14.  * Window>Preferences>Java>Code Generation>Code and Comments 
  15.  */  
  16. public class ReadXLS {  
  17.     public static void main(String[] args) {  
  18.         try {  
  19.             //InputStream is = new FileInputStream(new File("d://GSI-11336263.xls"));  
  20.             Workbook book = Workbook.getWorkbook(new File("d://GSI-11336263.xls"));  
  21.             //get a Sheet object.   
  22.             Sheet sheet = book.getSheet(0);  
  23.             String sheetName = sheet.getName() ;  
  24.             System.out.println("sheet 名称:"+sheetName) ;  
  25.             int sheetColumns = sheet.getColumns() ;  
  26.             System.out.println("sheet列数:"+sheetColumns) ;  
  27.             Cell[] cell = sheet.getColumn(0) ;  
  28.             for(int i=0 ;i<cell.length;i++){  
  29.                 System.out.println("cell0列第"+(i+1)+"行的值 :"+cell[i].getContents()) ;  
  30.             }  
  31.             int sheetRows = sheet.getRows() ;  
  32.             System.out.println("sheet的总行数 :"+sheetRows) ;  
  33.               
  34.             Cell[] sheetRowCell =sheet.getRow(0) ;  
  35.             System.out.println(sheetRowCell.length) ;  
  36.             for(int j=0 ;j <sheetRowCell.length ;j++){  
  37.                 System.out.println("cell1列行第"+(j+1)+"列行的值 :"+sheetRowCell[j].getContents()) ;  
  38.             }  
  39.             //get 1st-Column,1st-Row content.  
  40.             Cell cell00 = sheet.getCell(00);  
  41.             String result00 = cell00.getContents();  
  42.             System.out.println("Cell(0, 0)" + " value : " + result00 + "; type : " + cell00.getType());  
  43.             Cell cell01 = sheet.getCell(01) ;  
  44.             String result01= cell01.getContents() ;  
  45.             System.out.println("Cell(0, 1)" + " value : " + result01 + "; type : " + cell01.getType());  
  46.             book.close();  
  47.         } catch (Exception e) {  
  48.             e.printStackTrace();  
  49.         }  
  50.     }  
  51. }  

 

创建xls文档:

 

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. /** 
  2.  *  
  3.  */  
  4. package liu.createExcel;  
  5. /* 
  6.  * Created on Dec 30, 2007 
  7.  *  
  8.  * To change the template for this generated file go to 
  9.  * Window>Preferences>Java>Code Generation>Code and Comments 
  10.  */  
  11. import java.awt.Color;  
  12. import java.io.*;  
  13. import jxl.*;  
  14. import jxl.format.UnderlineStyle;  
  15. import jxl.write.*;  
  16. import jxl.write.Boolean;  
  17. import jxl.write.Number;  
  18. /** 
  19.  * @author 933soft 
  20.  *  
  21.  * To change the template for this generated type comment go to 
  22.  * Window>Preferences>Java>Code Generation>Code and Comments 
  23.  */  
  24. public class CreateXLS {  
  25.     public static void main(String[] args) {  
  26.         try {  
  27.             WritableWorkbook ww = Workbook.createWorkbook(new File(  
  28.                     "d://test.xls"));  
  29.             WritableSheet ws = ww.createSheet("Test Sheet 1"0);  
  30.             Label labelc = new Label(00"This is a Test LabelValue");  
  31.             ws.addCell(labelc);  
  32.             // 添加带有字型Formatting的对象  
  33.             WritableFont first = new WritableFont(WritableFont.TIMES, 18,  
  34.                     WritableFont.BOLD, true);  
  35.             WritableCellFormat firstwcf = new WritableCellFormat(first);  
  36.             Label labelCF = new Label(10"this is a label Cell", firstwcf);  
  37.             ws.addCell(labelCF);  
  38.             // 添加带有字体颜色Formatting的对象  
  39.             WritableFont second = new WritableFont(WritableFont.ARIAL, 10,  
  40.                     WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,  
  41.                     Colour.RED);  
  42.             WritableCellFormat secondwcf = new WritableCellFormat(second) ;  
  43.             Label labelSecond = new Label(10"this is a label Cell", secondwcf);  
  44.             ws.addCell(labelSecond) ;  
  45.             //添加Number对象  
  46.             Number labelN = new Number(0,1,3.141592653) ;  
  47.             ws.addCell(labelN) ;  
  48.             //添加带有formatting的Number对象  
  49.             NumberFormat nf = new NumberFormat("#.##") ;  
  50.             WritableCellFormat wcfnf = new WritableCellFormat(nf) ;  
  51.             Number nff = new Number(1,1,3.141592653,wcfnf) ;  
  52.             ws.addCell(nff) ;  
  53.             //添加Boolean对象  
  54.             Boolean labelb = new Boolean(0,2,false) ;  
  55.             ws.addCell(labelb) ;  
  56.         /*  //添加DateTime对象 
  57.             DateTime labelDT = new DateTime(0,3,new java.util.Date()) ; 
  58.             ws.addCell(labelDT) ; 
  59.             //添加带有formatting的DateFormat对象 
  60.             DateFormat df = new DateFormat("dd MM yyyy hh:mm:ss") ; 
  61.             WritableCellFormat wcfDF = new WritableCellFormat(df) ; 
  62.             DateTime dtf = new DateTime(1,3,new java.util.Date(),wcfDF) ; 
  63.             ws.addCell(dtf) ;*/  
  64.             System.out.println(new java.util.Date()) ;  
  65.             //4.添加DateTime对象  
  66.             jxl.write.DateTime labelDT = new jxl.write.DateTime(03new java.util.Date());  
  67.             ws.addCell(labelDT);  
  68.             //添加带有formatting的DateFormat对象  
  69.             jxl.write.DateFormat df = new jxl.write.DateFormat("dd MM yyyy hh:mm:ss");  
  70.             jxl.write.WritableCellFormat wcfDF = new jxl.write.WritableCellFormat(df);  
  71.             jxl.write.DateTime labelDTF = new jxl.write.DateTime(13new java.util.Date(), wcfDF);  
  72.             ws.addCell(labelDTF);  
  73.               
  74.             ww.write() ;  
  75.             ww.close() ;  
  76.             System.out.println("创建成功") ;  
  77.         } catch (Exception e) {  
  78.             e.printStackTrace();  
  79.         }  
  80.     }  
  81. }  

 

修改xls文档:

 

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. package liu.createExcel;  
  2. import java.io.File;  
  3. import jxl.CellType;  
  4. import jxl.Workbook;  
  5. import jxl.write.Label;  
  6. import jxl.write.WritableCell;  
  7. import jxl.write.WritableSheet;  
  8. import jxl.write.WritableWorkbook;  
  9. /** 
  10.  *  
  11.  * @author 933soft 
  12.  * 
  13.  */  
  14. public class UpdateXLS {  
  15.     public static void main(String[] args)throws Exception{  
  16.         Workbook rw = Workbook.getWorkbook(new File("d://test.xls")) ;  
  17.         WritableWorkbook wwb = Workbook.createWorkbook(new File("d://test1.xls"), rw) ;  
  18.         WritableSheet ws = wwb.getSheet(0) ;  
  19.         WritableCell wc = ws.getWritableCell(00) ;  
  20.         if(wc.getType() == CellType.LABEL){  
  21.             Label l = (Label)wc ;  
  22.             l.setString("the value has been modified!!") ;  
  23.         }  
  24.         wwb.write() ;  
  25.         wwb.close() ;  
  26.         rw.close() ;  
  27.         System.out.println("修改成功!!") ;  
  28.     }  
  29. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值