前提导入jxl依赖:
1、新建实体类Book
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、JAVA实现Excel导入导出
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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 {
//将list集合中的数据导出到Excel中
public void excleOut(List list) {
WritableWorkbook workbook =null;
try {
// 创建一个excle工作簿对象
workbook = Workbook.createWorkbook(new File("d:/book.xls"));
// 通过excle对象创建一个选项卡对象
WritableSheet sheet = workbook.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);
}
// 写入目标路径
workbook.write();
}catch (Exception e) {
e.printStackTrace();
}finally {
try {
workbook.close();
}catch (WriteException | IOException e) {
e.printStackTrace();
}
}
}
//将Excel中的数据导入到list集合中
public List excleIn() {
List list =new ArrayList<>();
Workbook workbook =null;
try {
// 获取Ecle对象
workbook = Workbook.getWorkbook(new File("d:/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;
}
}
3、功能测试
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
ExcleBook book =new ExcleBook();
List 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 books = book.excleIn();
for (Book bo : books) {
System.out.println(bo.getId() +" " + bo.getName() +" " + bo.getAuthor());
}
}
}
网友评论