1 ) 新建实体类Book
- package com.bean;
- public class Book {
- private Integer id ;
- private String name ;
- private String author;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getAuthor() {
- return author;
- }
- public void setAuthor(String author) {
- this.author = author;
- }
- }
2 ) 实现导入与导出方法
- package com.main;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import com.bean.Book;
- import jxl.Cell;
- import jxl.Sheet;
- import jxl.Workbook;
- import jxl.write.Label;
- import jxl.write.WritableSheet;
- import jxl.write.WritableWorkbook;
- import jxl.write.WriteException;
- public class ExcleBook {
- /**
- * 针对Book类进行导出的操作
- * @param list
- */
- public void excleOut(List<book> list) {
- WritableWorkbook book = null;
- try {
- // 创建一个excle对象
- book = Workbook.createWorkbook(new File("h:/excleTest/book.xls"));
- // 通过excle对象创建一个选项卡对象
- WritableSheet sheet = book.createSheet("sheet1", 0);
- // 创建一个单元格对象 列 行 值
- // Label label = new Label(0, 2, "test");
- for (int i = 0; i < list.size(); i++) {
- Book book2 = list.get(i);
- Label label1 = new Label(0, i, String.valueOf(book2.getId()));
- Label label2 = new Label(1, i, book2.getName());
- Label label3 = new Label(2, i, book2.getAuthor());
- // 将创建好的单元格对象放入选项卡中
- sheet.addCell(label1);
- sheet.addCell(label2);
- sheet.addCell(label3);
- }
- // 写入目标路径
- book.write();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- book.close();
- } catch (WriteException | IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- /**
- * 针对Book类进行导入的操作
- * @return
- */
- public List<book> excleIn() {
- List<book> list = new ArrayList<>();
- Workbook workbook = null;
- try {
- // 获取Ecle对象
- workbook = Workbook.getWorkbook(new File("h:/excleTest/book.xls"));
- // 获取选项卡对象 第0个选项卡
- Sheet sheet = workbook.getSheet(0);
- // 循环选项卡中的值
- for (int i = 0; i < sheet.getRows(); i++) {
- Book book = new Book();
- // 获取单元格对象
- Cell cell0 = sheet.getCell(0, i);
- // 取得单元格的值,并设置到对象中
- book.setId(Integer.valueOf(cell0.getContents()));
- // 获取单元格对象,然后取得单元格的值,并设置到对象中
- book.setName(sheet.getCell(1, i).getContents());
- book.setAuthor(sheet.getCell(2, i).getContents());
- list.add(book);
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- workbook.close();
- }
- return list;
- }
- public static void main(String[] args) {
- ExcleBook book = new ExcleBook();
- List<book> list = new ArrayList<>();
- Book book2 = new Book();
- book2.setId(1);
- book2.setName("书本名1");
- book2.setAuthor("张三");
- Book book3 = new Book();
- book3.setId(2);
- book3.setName("书本名2");
- book3.setAuthor("李四");
- list.add(book2);
- list.add(book3);
- book.excleOut(list);
- List<book> books = book.excleIn();
- for (Book bo : books) {
- System.out.println(bo.getId() + " " + bo.getName() + " " + bo.getAuthor());
- }
- }
- }
在这当中需要加入jxl.jar这个包,自己在网上下载,如果你使用Mavne,进行对应的配置就可以了。
源地址 :https://blog.csdn.net/nieguohui94/article/details/78011813