springboot导入数据到Excel以及从Excel导出数据

application.properties

#DB Configuration:
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://120.24.217.133:3306/ssmbuild?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root

#spring����Mybatis����
#pojo����ɨ���
mybatis.type-aliases-package=com.chen.mvc.pojo
#����Mybatisӳ���ļ�
mybatis.mapper-locations=classpath:mapper/*Mapper.xml

实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
    // 主键
    private Integer bookID;
    // 书名
    private String bookName;
    // 数量
    private Integer bookCounts;
    // 描述
    private String detail;
}

Mapper

    //查询全部Book,返回list集合
    List<Books> queryAllBook();

Mapper.xml

    <!--查询全部Book-->
    <select id="queryAllBook" resultType="Books">
        SELECT * from books
    </select>

测试类
从数据库导出到Excel

    @Test
    public void testSaveExcel() {

        try {
            WritableWorkbook wwb=null;
            //创建可写入的Excel工作薄
            String fileName="C:\\Users\\Administrator\\Desktop\\1.xls";
            File file=new File(fileName);

            if(!file.exists()) {
                file.createNewFile();
            }
            //以fileName 为文件名来创建一个Workbook
            wwb= Workbook.createWorkbook(file);
            //创建工作表
            WritableSheet ws=wwb.createSheet("Test Shee 1", 0);

            //查询数据库中所有的数据
            List<Books> list = bookMapper.querybookList();
            System.out.println(list);

            //要插入到的Excel表格的行号,默认从0开始
            Label labelBookID=new Label(0, 0, "书籍编号");
            Label labelBookName=new Label(1, 0, "书籍名字");
            Label labelBookCounts=new Label(2, 0, "书籍数量");
            Label labelDetail=new Label(3, 0, "书籍详情");

            ws.addCell(labelBookID);
            ws.addCell(labelBookName);
            ws.addCell(labelBookCounts);
            ws.addCell(labelDetail);

            for (int i = 0; i < list.size(); i++) {
                Label labelId_i=new Label(0, i+1, list.get(i).getBookID()+"");
                Label labelName_i=new Label(1, i+1, list.get(i).getBookName());
                Label labelSex_i=new Label(2, i+1, list.get(i).getBookCounts()+"");
                Label labelNum_i=new Label(3, i+1, list.get(i).getDetail()+"");
                ws.addCell(labelId_i);
                ws.addCell(labelName_i);
                ws.addCell(labelSex_i);
                ws.addCell(labelNum_i);
            }

            //写进文档
            wwb.write();
            System.out.println("数据写入成功");

            //关闭Excel工作簿对象
            wwb.close();

        } catch (Exception e) {
            System.out.println("数据写入失败");
            e.printStackTrace();
        }
    }

从Excel读取数据

    @Test
    public void testGetExcel() {
        Sheet sheet;
        Workbook book;
        Cell cell1;
        try {
            //hello.xls为要读取的excel文件名
            book= Workbook.getWorkbook(new File("C:\\Users\\Administrator\\Desktop\\1.xls"));
            //获得第一个工作表对象(ecxel中sheet的编号从0开始,0,1,2,3,....)
            sheet=book.getSheet(0);
            //获取左上角的单元格
            cell1=sheet.getCell(2,0);
            System.out.println(cell1.getContents());
            for(int i=0;i<=4;i++){
                for(int j=0;j<=3;j++){
                    Cell s=sheet.getCell(j,i);
                    System.out.printf(s.getContents());
                }
                System.out.println();
            }
            book.close();
        }
        catch(Exception e)  { }
    }
上一篇:#力扣LeetCode1935. 可以输入的最大单词数 @FDDLC


下一篇:Java 中JAF、CORBA、JTA、JAXB、JAX-WS使用Maven的pom.xml文件配置