java中实现Excel的导入与导出

1 使用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.   
  110. </book></book></book></book></book>  

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

2 使用Poi实现Excel的导入与导出

我们在网上找 POI下载的时候,会发现它有点大,而且还有几个不同的包,那么我们到底需要那个。如果你只需要对xls文件进行解释,只需要加入poi-Version.jar;如果要对xlsx文件进行解释,那么你需要加入poi-ooxml-Version.jar;如果你要对word、ppt、viso、outlook等时需要用到poi-scratchpad-version-yyyymmdd.jar。因为我们这里只是对xls文件解释,所以我们加入的poi-3.17.jar这个文件。

[Java]  view plain  copy
  1. package com.main;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8. import java.io.OutputStream;  
  9. import java.util.ArrayList;  
  10. import java.util.List;  
  11.   
  12. import org.apache.poi.hssf.usermodel.HSSFRow;  
  13. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  14. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  15.   
  16. import com.bean.Book;  
  17.   
  18. public class ExclePoiBook {  
  19.     /** 
  20.      * 针对Book类进行导出的操作 
  21.      *  
  22.      * @param list 
  23.      */  
  24.     public void excleOut(List<book> list) {  
  25.         // 创建Excel文档  
  26.         HSSFWorkbook hwb = new HSSFWorkbook();  
  27.         // 通过excle对象创建一个选项卡对象  
  28.         HSSFSheet sheet = hwb.createSheet("sheet1");  
  29.         Book book = null;  
  30.         // 循环list创建行  
  31.         for (int i = 0; i < list.size(); i++) {  
  32.             // 新建一行  
  33.             HSSFRow row = sheet.createRow(i);  
  34.             book = list.get(i);  
  35.             // 设置i+1行第0列的数据  
  36.             row.createCell(0).setCellValue(book.getId());  
  37.             // 设置i+1行第1列的数据  
  38.             row.createCell(1).setCellValue(book.getName());  
  39.             // 设置i+1行第2列的数据  
  40.             row.createCell(2).setCellValue(book.getAuthor());  
  41.         }  
  42.         OutputStream out = null;  
  43.         try {  
  44.             out = new FileOutputStream("h:/excleTest/bookPoi.xls");  
  45.             hwb.write(out);  
  46.         } catch (FileNotFoundException e) {  
  47.             e.printStackTrace();  
  48.         } catch (IOException e) {  
  49.             e.printStackTrace();  
  50.         } finally {  
  51.             try {  
  52.                 out.close();  
  53.             } catch (IOException e) {  
  54.                 // TODO Auto-generated catch block  
  55.                 e.printStackTrace();  
  56.             }  
  57.         }  
  58.     }  
  59.   
  60.     /** 
  61.      * 针对Book类进行导入的操作 
  62.      *  
  63.      * @return 
  64.      */  
  65.     public List<book> excleIn() {  
  66.         List<book> list = new ArrayList<>();  
  67.         Book book = null;  
  68.         try {  
  69.             InputStream is = new FileInputStream("h:/excleTest/bookPoi.xls");  
  70.             HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);  
  71.             // 获取选项卡对象 第0个选项卡 , 因为我们这里只有一个选项卡,如果你每个选项卡的内容是一样,可以通过循环取出  
  72.             HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);  
  73.             // 循环取出每行的值  
  74.             for (int rowNum = 0; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {  
  75.                 HSSFRow hssfRow = hssfSheet.getRow(rowNum);  
  76.                 book = new Book();  
  77.                 //注意Poi读取的内容是有类型的,处理起来也jxl有所不同  
  78.                 book.setId((int) hssfRow.getCell(0).getNumericCellValue());  
  79.                 book.setName(hssfRow.getCell(1).getStringCellValue());  
  80.                 book.setAuthor(hssfRow.getCell(2).getStringCellValue());  
  81.                 list.add(book);  
  82.             }  
  83.         } catch (FileNotFoundException e) {  
  84.             // TODO Auto-generated catch block  
  85.             e.printStackTrace();  
  86.         } catch (IOException e) {  
  87.             // TODO Auto-generated catch block  
  88.             e.printStackTrace();  
  89.         }  
  90.         return list;  
  91.     }  
  92.     public static void main(String[] args) {  
  93.         ExclePoiBook exclePoiBook = new ExclePoiBook();  
  94.         List<book> list = new ArrayList<>();  
  95.         Book book2 = new Book();  
  96.         book2.setId(1);  
  97.         book2.setName("书本名1");  
  98.         book2.setAuthor("张三");  
  99.         Book book3 = new Book();  
  100.         book3.setId(2);  
  101.         book3.setName("书本名2");  
  102.         book3.setAuthor("李四");  
  103.         list.add(book2);  
  104.         list.add(book3);  
  105.         exclePoiBook.excleOut(list);  
  106.         List<book> books = exclePoiBook.excleIn();  
  107.         for (int i = 0; i < books.size(); i++) {  
  108.             System.out.println(books.get(i).getId()  + " " + books.get(i).getName() + " " + books.get(i).getAuthor());  
  109.         }  
  110.     }  
  111. }  
  112.   

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值