当前位置: 首页 > 工具软件 > Excel2Entity > 使用案例 >

easypoi操作excel导入导出

荀豪
2023-12-01
在这里插入代码片

package com.example.springboot6.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;

import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot6.entity.User;
import com.example.springboot6.mapper.UserMapper;
import com.example.springboot6.util.Json;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Controller
public class uplod {
@Autowired(required = false)
private UserMapper userMapper;
///excel导入
private List xia(){
List a=new ArrayList<>();
QueryWrapper u=new QueryWrapper<>();
u.equals(null);
List d= userMapper.selectList(u);
System.out.println(d);
System.out.println(d);
return d;
}
@GetMapping("/ku")
public void daochu() throws IOException {
//导出到excel 类需要继承Serializable
List user=xia();
//参数1,导出配置对象2,导出类型3导出数据集合
Workbook workbook= ExcelExportUtil.exportExcel(new ExportParams(“用户列表”,“用户信息”),User.class,user);
FileOutputStream fileOutputStream = new FileOutputStream(“C:\Users\孙振\Desktop\简历\aa.xls”);
workbook.write(fileOutputStream);
fileOutputStream.close();
workbook.close();
}
@GetMapping("/ft")
public void dinpu() throws Exception {
ImportParams params=new ImportParams();
params.setTitleRows(1);//标题占几行
params.setHeadRows(1);//header列占几行
List em= ExcelImportUtil.importExcel(new FileInputStream(“C:\Users\孙振\Desktop\简历\aa.xls”),User.class,params);
em.forEach(System.out :: println);
for(User user : em){
userMapper.insert(user);
}
System.out.println(“成功”);
}

@PostMapping("/upload")
public Json insLostImage(@RequestParam("file") MultipartFile file){
    //上传图片
    System.out.println("5555555555");

    //判断文件是否为空
    if(file.isEmpty()){
        return Json.fail("上传失败");
    }
    //获取文件名
    String onefilename=file.getOriginalFilename();
    //新文件名          获取时间挫                          扩展名
    String fileName=System.currentTimeMillis()+"."+onefilename.substring(onefilename.lastIndexOf(".")+1);
    String filePath="C:\\Users\\孙振\\Desktop\\img\\ft\\";
    //获取文件
    File date =new File(filePath+fileName);
    //判断文件夹存在
    if(!date.getParentFile().exists()){
        //创建文件夹
        date.getParentFile().mkdirs();
    }
    try {
        //上传
        file.transferTo(date);
        System.out.println("1111111111111111");
    } catch (IOException e) {
        e.printStackTrace();
        return Json.fail("失败");
    }
    return Json.succcess("成功");
}

}

 类似资料: