前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >杨校老师课堂之POI导入数据

杨校老师课堂之POI导入数据

作者头像
杨校
发布2021-05-06 16:23:33
3860
发布2021-05-06 16:23:33
举报
文章被收录于专栏:Java技术分享圈Java技术分享圈
  1. 导入坐标
代码语言:javascript
复制
	<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>

2.编码

代码语言:javascript
复制
package com.example.springbootmybatis;

import com.example.springbootmybatis.entity.Book;
import com.example.springbootmybatis.entity.User;
import com.example.springbootmybatis.service.BookService;
import com.example.springbootmybatis.service.UserService;
import org.apache.poi.ss.usermodel.*;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
public class SpringbootMybatisApplicationTests {

    @Resource
    private BookService bookService;
    // 实例化集合,用于存储读取的excel数据
    List<Book> bookList = new ArrayList<>();

    @Test
    public void contextLoads() throws IOException {
        //  1. 手写创建 File  ||  稍后记得导入  commons-io 、commons-fileupload
        File file = new File("C:\\Users\\Mryang\\Desktop\\book.xlsx");
        //  2.创建工作簿
        Workbook workbook = WorkbookFactory.create(file);

        int sheets = workbook.getNumberOfSheets();

        System.out.println("该文件有"+sheets+"表");

        Sheet sheet = workbook.getSheetAt(0);

        System.out.println(sheet.getSheetName());

        int rows = sheet.getPhysicalNumberOfRows();//getPhysicalNumberOfRows 获取所有行数
        System.out.println("行数是" + rows);

        //  循环所有的行数
        for (int j = 0; j < rows; j++) {

            // 判断是不是第一行标题 是不是表头?
            if (sheet.getFirstRowNum() == j){
                //  第一行不要了
                continue;
            }
            Row row = sheet.getRow(j);
            int cells = row.getPhysicalNumberOfCells();//getPhysicalNumberOfCells 获取所有列数

            Book book = new Book();// 创建10个呀

            for (int i = 0; i < cells; i++) {
                switch (i){
                    case 0 ://第1列
                        row.getCell(i).setCellType(CellType.STRING);//  设置单元格的类型字符串类型
                        int bookId  = Integer.parseInt(row.getCell(i).getStringCellValue());//转换为整数
                        book.setBookId(bookId);
                        break;
                    case 1 ://第2列
                        row.getCell(i).setCellType(CellType.STRING);
                        book.setBookName(row.getCell(i).getStringCellValue());
                        break;
                    case 2 ://第3列
                        row.getCell(i).setCellType(CellType.STRING);
                        book.setBookPrice(Double.parseDouble(row.getCell(i).getStringCellValue()));
                        break;
                    case 3 ://第4列
                        row.getCell(i).setCellType(CellType.STRING);
                        book.setBookPublish(row.getCell(i).getStringCellValue());
                        break;
                }
            }
            bookList.add(book);
        }
        //System.out.println("bookList = " + bookList);//检验是否将配置文件内的书信息存储到集合中

        for (Book book : bookList) {
          //System.out.println(book);
            bookService.addBook(book);
          
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-04-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档