`
q272156430
  • 浏览: 269942 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

java导入导出excel操作(jxl)(1)

阅读更多

jxl.jar 包
下载地址:
http://www.andykhan.com/jexcelapi/
真实下载地址:
http://www.andykhan.com/jexcelapi/download.html

网站上对它的特征有如下描述:
● 支持Excel 95-2000的所有版本
● 生成Excel 2000标准格式
● 支持字体、数字、日期操作
● 能够修饰单元格属性
● 支持图像和图表
应该说以上功能已经能够大致满足我们的需要。最关键的是这套API是纯Java的,并不依赖Windows系统,即使运行在Linux下,它同样能够正确的处理Excel文件。另外需要说明的是,这套API对图形和图表的支持很有限,而且仅仅识别PNG格式。
搭建环境
将下载后的文件解包,得到jxl.jar,放入classpath,安装就完成了。

基本操作

一、创建文件
拟生成一个名为“测试数据.xls”的Excel文件,其中第一个工作表被命名为“第一页”,大致效果如下:

Java代码 复制代码
  1.   
  2. package JExcelTest.standard;   
  3.   
  4. import java.io.*;    
  5. import jxl.*;    
  6. import jxl.write.*;    
  7.   
  8.   
  9. public class CreateXLS {   
  10.   
  11.     public static void main(String[] args) {   
  12.         try {   
  13.             //open file.   
  14.             WritableWorkbook book = Workbook.createWorkbook(new File("d:/Test.xls"));   
  15.                
  16.             //create Sheet named "Sheet_1". 0 means this is 1st page.   
  17.             WritableSheet sheet = book.createSheet("Sheet_1"0);   
  18.                
  19.             //define cell column and row in Label Constructor, and cell content write "test".   
  20.             //cell is 1st-Column,1st-Row. value is "test".   
  21.             Label label = new Label(00"test");   
  22.             //add defined cell above to sheet instance.   
  23.             sheet.addCell(label);   
  24.                
  25.             //create cell using add numeric. WARN:necessarily use integrated package-path, otherwise will be throws path-error.   
  26.             //cell is 2nd-Column, 1st-Row. value is 789.123.   
  27.             jxl.write.Number number = new jxl.write.Number(10789.123);   
  28.             //add defined cell above to sheet instance.   
  29.             sheet.addCell(number);   
  30.                
  31.             //add defined all cell above to case.   
  32.             book.write();   
  33.             //close file case.   
  34.             book.close();   
  35.         } catch (Exception e) {   
  36.             e.printStackTrace();   
  37.         }   
  38.     }   
  39. }  
    编译执行后,会在当前位置产生一个Excel文件。

    二、读取文件
    以刚才我们创建的Excel文件为例,做一个简单的读取操作,程序代码如下:
Java代码 复制代码
  1.   
  2. package JExcelTest.standard;   
  3.   
  4. import java.io.*;   
  5. import jxl.*;   
  6.   
  7.   
  8. public class ReadXLS {   
  9.   
  10.     public static void main(String[] args) {   
  11.         try {   
  12.             Workbook book = Workbook.getWorkbook(new File("d:/Test.xls"));   
  13.             //get a Sheet object.    
  14.             Sheet sheet = book.getSheet(0);   
  15.             //get 1st-Column,1st-Row content.   
  16.             Cell cell = sheet.getCell(00);   
  17.             String result = cell.getContents();   
  18.             System.out.println(result);   
  19.             book.close();   
  20.         } catch (Exception e) {   
  21.             e.printStackTrace();   
  22.         }   
  23.   
  24.     }   
  25. }  
    程序执行结果:test

    三、修改文件
    利用jExcelAPI可以修改已有的Excel文件,修改Excel文件的时候,除了打开文件的方式不同之外,其他操作和创建Excel是一样的。下面的例子是在我们已经生成的Excel文件中添加一个工作表:
    修改Excel的类,添加一个工作表
Java代码 复制代码
  1.   
  2. package JExcelTest.standard;   
  3.   
  4. import java.io.*;   
  5. import jxl.*;   
  6. import jxl.write.*;   
  7.   
  8.   
  9. public class UpdateXLS {   
  10.   
  11.     public static void main(String[] args) {   
  12.         try {   
  13.             //get file.   
  14.             Workbook wb = Workbook.getWorkbook(new File("d:/Test.xls"));   
  15.             //open a copy file(new file), then write content with same content with Test.xls.     
  16.             WritableWorkbook book =   
  17.                 Workbook.createWorkbook(new File("d:/Test.xls"), wb);   
  18.             //add a Sheet.   
  19.             WritableSheet sheet = book.createSheet("Sheet_2"1);   
  20.             sheet.addCell(new Label(00"test2"));   
  21.             book.write();   
  22.             book.close();   
  23.         } catch (Exception e) {   
  24.             e.printStackTrace();   
  25.         }   
  26.     }   
  27. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics