第三十节 Excel文件的导入和导出

亮子 2024-10-28 11:22:42 280 0 0 0

1、Excel文件导出

@PostMapping("/export")
public void export(HttpServletResponse response)
{
    List<TbProd> list = tbProdMapper.selectList(null);
    ExcelUtil<TbProd> util = new ExcelUtil<TbProd>(TbProd.class);
    util.exportExcel(response, list, "产品数据");
}

2、Excel文件导入

@PostMapping(value = "/import")
public R importExcel(@RequestPart("file") MultipartFile file) throws IOException {
    ExcelUtil<TbProd> util = new ExcelUtil<TbProd>(TbProd.class);
    List<TbProd> tbProds = util.importExcel(file.getInputStream());
    for (TbProd tbProd : tbProds) {
        System.out.println(tbProd.getProdName());
        tbProdMapper.insert(tbProd);
    }

    return R.OK();
}

3、Vue导出前端代码

// 导出
const onExport = () => {
    console.log('onExport');
    let a = document.createElement('a');
    a.href ="http://localhost:9001/user/user/exportUserList";
    a.click();
}