Spring 整合POI导入导出Excel,环境Spring、SpringMVC、maven

1、引入依赖  
        <!-- POI -->
            <poi.version>3.9</poi.version>
            <poi-ooxml.version>3.9</poi-ooxml.version>
        <!-- POI -->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>${poi.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>${poi-ooxml.version}</version>
            </dependency>
    2、编码
    下面是读取Excel数据的代码内容,忽略业务逻辑,自己手动删除
    @Resource
    private UwoSpecialUserService uwoSpecialUserService;

    private final static String xls = "xls";
    private final static String xlsx = "xlsx";
     //导入excel数据
    @RequestMapping(value = "1425", method = {RequestMethod.GET, RequestMethod.POST})
    public Response<Object> importFile(@RequestParam("file") MultipartFile file){
        //目标:读excel,入库
        List<UwoSpecialUser> uwoSpecialUserList=new ArrayList<>();
        Response<Object> result = new Response<Object>();
        try {
            int count = 0;
            String msg = "";
            //技巧:平时怎么读,代码怎么写。
            //1.打开工作簿(xls、xlsx格式)
            Workbook workbook = getWorkBook(file);
            //2.从工作簿中打开工作表
            //workbook.getSheet("Sheet1");//根据名字读取工作表
            Sheet sheet = workbook.getSheetAt(0);//g根据索引来读取工作表0-based physical & logical
            //3.一行一行读
            for (Row row : sheet) {
                //第一行一般是标题,要跳过
                if(row.getRowNum()==0||row.getRowNum()==1){
                    continue;
                }

                //将值封装到对象
                UwoSpecialUser uwoSpecialUser=new UwoSpecialUser();

                //一格一格读数据
                if (row.getCell(0)!=null){
                    row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                    String specialUserName = row.getCell(0).getStringCellValue();
                    uwoSpecialUser.setSpecialUserName(specialUserName);
                }
                if (row.getCell(1)!=null){
                    row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
                    String phonenum = row.getCell(1).getStringCellValue();
                    uwoSpecialUser.setPhonenum(phonenum);
                }
                if (row.getCell(2)!=null){
                    row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
                    String money = row.getCell(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值