springboot实现导出数据库数据和导入数据到数据库

 依赖

 <!--hutool-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.16</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>

 controller层代码

//导出文件
    @GetMapping("/export")
    public void export(HttpServletResponse response)throws Exception{
        //数据库查出所有数据
        List<Role>list=roleService.list();
        //通过writer工具类写出到浏览
        ExcelWriter writer = ExcelUtil.getWriter();
        //设置别名
        writer.addHeaderAlias("name","名称");
        writer.addHeaderAlias("description","描述");
        //一次性写出list内的对象到excel,使用默认格式,强制输出标题
        writer.write(list,true);
        //设置浏览器响应的格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName= URLEncoder.encode("用户信息","UTF-8");
        response.setHeader("Content-Disposition","attachment;filename=test.xls");
        ServletOutputStream out=response.getOutputStream();
        writer.flush(out,true);
        out.close();
        writer.close();
    }
    //导入
    @PostMapping("/import")
    public List<Role> imp(MultipartFile file) throws Exception{
        System.out.println("我进来了********");
        InputStream inputStream=file.getInputStream();
        ExcelReader reader=ExcelUtil.getReader(inputStream);
        List<Role>list=reader.readAll(Role.class);
        for(int i=0;i<list.size();i++)
        {
            list.get(i).setId(null);//这里是因为数据库设置了id不能重复,并且会自增,所以把导入的数据的id设成null
        }
        roleService.saveBatch(list);
        return list;
    }

如果设置了别名,excel生成数据的列数据头是别名显示,如果导入的文件也是以别名显示,那么就会报错,这时候就要在实现类加上@Alias注解定义别名 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现数据库数据导入 Word,可以使用 Apache POI 库,它是 Java 操作 Office 文档的一个 API。以下是一个简单的示例代码: 1. 首先,需要在 pom.xml 文件中添加 Apache POI 的依赖: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> ``` 2. 然后,创建一个 WordUtils 类,用于导出 Word 文档: ```java public class WordUtils { public static void exportDataToWord(List<User> userList, String filePath) throws IOException { // 创建 Word 文档 XWPFDocument document = new XWPFDocument(); // 创建段落 XWPFParagraph paragraph = document.createParagraph(); // 创建表格 XWPFTable table = document.createTable(); // 设置表格列数 int columnCount = 2; table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(5000)); table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(5000)); // 添加表头行 XWPFTableRow headerRow = table.getRow(0); headerRow.getCell(0).setText("ID"); headerRow.getCell(1).setText("Name"); // 添加数据行 for (User user : userList) { XWPFTableRow dataRow = table.createRow(); dataRow.getCell(0).setText(String.valueOf(user.getId())); dataRow.getCell(1).setText(user.getName()); } // 输出 Word 文档 FileOutputStream outputStream = new FileOutputStream(filePath); document.write(outputStream); outputStream.close(); } } ``` 3. 最后,在 Controller 中调用 WordUtils 类的 exportDataToWord() 方法即可: ```java @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/export") public void exportToWord(HttpServletResponse response) throws IOException { List<User> userList = userService.getAllUsers(); String fileName = "user_list.docx"; String filePath = "D:\\" + fileName; WordUtils.exportDataToWord(userList, filePath); response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document"); response.setHeader("Content-Disposition", "attachment; filename=" + fileName); File file = new File(filePath); InputStream inputStream = new FileInputStream(file); IOUtils.copy(inputStream, response.getOutputStream()); response.flushBuffer(); } } ``` 注意,上述代码中的 User 类是数据库表对应的实体类,getAllUsers() 方法是用于获取所有用户数据的方法,可以根据实际情况进行调整。同时,为了让用户能够下载生成的 Word 文档,需要在响应头中设置 Content-Disposition 和 Content-Type。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值