Excel上传和下载

Excel上传

<el-form-item> 
        <el-upload :shaw-file-list="false" :on-change="onChange" :auto-upload="false"> 
           <el-button type="primary">&nbsp导入</el-button>
        </el-upload>
   </el-form-item>
const onChange = (uploadFile: any, _uploadFiles: any) => {

    let name = uploadFile.name
    let reader = new FileReader()
    reader.readAsDataURL(uploadFile.raw) // 异步的读
    reader.onload = (file) => {
        callUploadApi(name, file.target?.result)
    }

}

const callUploadApi = (name: string, base64: any) => {

    productApi.upload.call({ name, base64 }).then((res: any) => {
         ElMessage.success("上传成功")
    })
} 

@PostMapping("upload")
    public String upload(@RequestBody UploadDto uploadDto) throws IOException {

        String base64 = uploadDto.getBase64();
        String[] base64s = StrUtil.splitToArray(base64, "base64,");
        byte[] decode = Base64.decode(base64s[1]);

        //用于创建一个基于字节数组的输入流。它允许你从一个字节数组中读取数据。
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(decode);

        //创建Excel读取器,用于后续的Excel数据读取操作
        ExcelReader reader = ExcelUtil.getReader(byteArrayInputStream);

        //通过反射机制读取所有产品数据。
        List<Product> readAll = reader.readAll(Product.class);

        productService.insert(readAll);

        byteArrayInputStream.close();
        return "success";

    }

Excel下载

<el-form-item>
            <el-button type="primary" @click="exportExcel">&nbsp导出                    </el-button>
      </el-form-item>

const exportExcel = () => {
    let name = formData.name == '' ? undefined : formData.name
    window.open('/api/pro/download?name='+name, '_blank', '')
}

    @GetMapping("download")
    public void download(HttpServletResponse response, ProductQuery productQuery) throws IOException {

        List<Product> productList = productService.select(productQuery);

        // 初始化Excel写入器 加true指定Excel文件是xlsx格式
        ExcelWriter writer = ExcelUtil.getWriter(true);

        writer.addHeaderAlias("id", "Id");
        writer.addHeaderAlias("name", "姓名");
        writer.addHeaderAlias("subName", "介绍");
        writer.addHeaderAlias("status", "状态");
        writer.addHeaderAlias("price", "价格");

        // 写入当前批次的数据
        writer.write(productList,true);

        //response为HttpServletResponse对象   设置响应的内容类型为Excel文件
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("UTF-8");
        //test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
        //设置响应头,告诉浏览器以附件形式下载文件,文件名为test.xlsx。这样设置可以让浏览器弹出文件下载对话框。
        String time = System.currentTimeMillis() +"";
        response.setHeader("Content-Disposition", "attachment;filename=product"+time+".xlsx");
        //获取响应输出流,它是用于将响应的数据发送给客户端的流。
        ServletOutputStream out = response.getOutputStream();
        //将Excel数据写入输出流。第二个参数为true表示追加写入,即将数据追加到已有的Excel文件中。
        writer.flush(out, true);

        //关闭输出流
        out.close();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这孩子叫逆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值