Jxl实现Excel的导入与导出

1 ) 新建实体类Book

[Java]  view plain  copy
  1. package com.bean;  
  2.   
  3. public class Book {  
  4.     private Integer id ;  
  5.     private String  name ;  
  6.     private String  author;  
  7.     public Integer getId() {  
  8.         return id;  
  9.     }  
  10.     public void setId(Integer id) {  
  11.         this.id = id;  
  12.     }  
  13.     public String getName() {  
  14.         return name;  
  15.     }  
  16.     public void setName(String name) {  
  17.         this.name = name;  
  18.     }  
  19.     public String getAuthor() {  
  20.         return author;  
  21.     }  
  22.     public void setAuthor(String author) {  
  23.         this.author = author;  
  24.     }  
  25.       
  26.       
  27. }  

2 ) 实现导入与导出方法

[Java]  view plain  copy
  1. package com.main;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7.   
  8. import com.bean.Book;  
  9.   
  10. import jxl.Cell;  
  11. import jxl.Sheet;  
  12. import jxl.Workbook;  
  13. import jxl.write.Label;  
  14. import jxl.write.WritableSheet;  
  15. import jxl.write.WritableWorkbook;  
  16. import jxl.write.WriteException;  
  17.   
  18. public class ExcleBook {  
  19.     /** 
  20.      * 针对Book类进行导出的操作 
  21.      * @param list 
  22.      */  
  23.     public void excleOut(List<book> list) {  
  24.         WritableWorkbook book = null;  
  25.         try {  
  26.             // 创建一个excle对象  
  27.             book = Workbook.createWorkbook(new File("h:/excleTest/book.xls"));  
  28.             // 通过excle对象创建一个选项卡对象  
  29.             WritableSheet sheet = book.createSheet("sheet1"0);  
  30.             // 创建一个单元格对象 列 行 值  
  31.             // Label label = new Label(0, 2, "test");  
  32.             for (int i = 0; i < list.size(); i++) {  
  33.                 Book book2 = list.get(i);  
  34.                 Label label1 = new Label(0, i, String.valueOf(book2.getId()));  
  35.                 Label label2 = new Label(1, i, book2.getName());  
  36.                 Label label3 = new Label(2, i, book2.getAuthor());  
  37.   
  38.                 // 将创建好的单元格对象放入选项卡中  
  39.                 sheet.addCell(label1);  
  40.                 sheet.addCell(label2);  
  41.                 sheet.addCell(label3);  
  42.             }  
  43.             // 写入目标路径  
  44.             book.write();  
  45.         } catch (Exception e) {  
  46.             e.printStackTrace();  
  47.         } finally {  
  48.             try {  
  49.                 book.close();  
  50.             } catch (WriteException | IOException e) {  
  51.                 // TODO Auto-generated catch block  
  52.                 e.printStackTrace();  
  53.             }  
  54.         }  
  55.     }  
  56.   
  57.     /** 
  58.      * 针对Book类进行导入的操作 
  59.      * @return 
  60.      */  
  61.     public List<book> excleIn() {  
  62.         List<book> list = new ArrayList<>();  
  63.         Workbook workbook = null;  
  64.         try {  
  65.             // 获取Ecle对象  
  66.             workbook = Workbook.getWorkbook(new File("h:/excleTest/book.xls"));  
  67.             // 获取选项卡对象 第0个选项卡  
  68.             Sheet sheet = workbook.getSheet(0);  
  69.             // 循环选项卡中的值  
  70.             for (int i = 0; i < sheet.getRows(); i++) {  
  71.                 Book book = new Book();  
  72.                 // 获取单元格对象  
  73.                 Cell cell0 = sheet.getCell(0, i);  
  74.                 // 取得单元格的值,并设置到对象中  
  75.                 book.setId(Integer.valueOf(cell0.getContents()));  
  76.                 // 获取单元格对象,然后取得单元格的值,并设置到对象中  
  77.                 book.setName(sheet.getCell(1, i).getContents());  
  78.                 book.setAuthor(sheet.getCell(2, i).getContents());  
  79.                 list.add(book);  
  80.             }  
  81.         } catch (Exception e) {  
  82.             e.printStackTrace();  
  83.         } finally {  
  84.             workbook.close();  
  85.         }  
  86.         return list;  
  87.     }  
  88.   
  89.     public static void main(String[] args) {  
  90.         ExcleBook book = new ExcleBook();  
  91.         List<book> list = new ArrayList<>();  
  92.         Book book2 = new Book();  
  93.         book2.setId(1);  
  94.         book2.setName("书本名1");  
  95.         book2.setAuthor("张三");  
  96.         Book book3 = new Book();  
  97.         book3.setId(2);  
  98.         book3.setName("书本名2");  
  99.         book3.setAuthor("李四");  
  100.         list.add(book2);  
  101.         list.add(book3);  
  102.         book.excleOut(list);  
  103.         List<book> books = book.excleIn();  
  104.         for (Book bo : books) {  
  105.             System.out.println(bo.getId() + " " + bo.getName() + " " + bo.getAuthor());  
  106.         }  
  107.     }  
  108. }  
  109.   

在这当中需要加入jxl.jar这个包,自己在网上下载,如果你使用Mavne,进行对应的配置就可以了。


源地址 :https://blog.csdn.net/nieguohui94/article/details/78011813

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值