通过easyExcel导出(简洁版)

第一步:
导入依赖

    <!-- 导处Excel -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>easyexcel</artifactId>
      <version>3.0.0-beta3</version>
    </dependency>

第二步:
新建方法类

package com.lc.yangzi.module.marketing.sell;

import lombok.extern.slf4j.Slf4j;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.apache.poi.util.IOUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * Excel工具类
 */
@Slf4j
public class ExcelUtils {

    /**
     * 导出Excel(07版.xlsx)到指定路径下
     *
     * @param path      路径
     * @param excelName Excel名称
     * @param sheetName sheet页名称
     * @param clazz     Excel要转换的类型
     * @param data      要导出的数据
     */
    public static void export2File(String path, String excelName, String sheetName, Class clazz, List data) {
        String fileName = path.concat(excelName).concat(ExcelTypeEnum.XLSX.getValue());
        EasyExcel.write(fileName, clazz).sheet(sheetName).doWrite(data);
    }

    /**
     * 导出Excel(07版.xlsx)到web
     *
     * @param response  响应
     * @param excelName Excel名称
     * @param sheetName sheet页名称
     * @param clazz     Excel要转换的类型
     * @param data      要导出的数据
     * @throws Exception
     */
    public static void export2Web(HttpServletResponse response, String excelName, String sheetName, Class clazz, List data) throws Exception {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        excelName = URLEncoder.encode(excelName, "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + excelName + ExcelTypeEnum.XLSX.getValue());
        EasyExcel.write(response.getOutputStream(), clazz).sheet(sheetName).doWrite(data);
    }

    /**
     * 将指定位置指定名称的Excel导出到web
     *
     * @param response  响应
     * @param path      文件路径
     * @param excelName 文件名称
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String export2Web4File(HttpServletResponse response, String path, String excelName) throws UnsupportedEncodingException {
        File file = new File(path.concat(excelName).concat(ExcelTypeEnum.XLSX.getValue()));
        if (!file.exists()) {
            return "文件不存在!";
        }

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        excelName = URLEncoder.encode(excelName, "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + excelName + ExcelTypeEnum.XLSX.getValue());

        try (
                FileInputStream in = new FileInputStream(file);
                ServletOutputStream out = response.getOutputStream();
        ) {
            IOUtils.copy(in, out);
            return "导出成功!";
        } catch (Exception e) {
            log.error("导出文件异常:", e);
        }

        return "导出失败!";
    }

}

第二步:
新建要导出的列表类

package com.lc.yangzi.module.marketing.sell.customerinfo.domain;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import lombok.Data;

@Data
public class CustomerInfoExcel {

    @ColumnWidth(20) // 定义列宽
    @ExcelProperty(value = {"客户名称"}, index = 0)
    private String customerName;//客户名称
    @ColumnWidth(20) // 定义列宽
    @ExcelProperty(value = {"客户等级"}, index = 1)
    private Integer customerLevel;//客户等级:A,B,C,D
    @ColumnWidth(20) // 定义列宽
    @ExcelProperty(value = {"地址"}, index = 2)
    private String address;//地址
    @ColumnWidth(20) // 定义列宽
    @ExcelProperty(value = {"联系电话"}, index = 3)
    private String contactPhone;//联系电话
    @ColumnWidth(20) // 定义列宽
    @ExcelProperty(value = {"审核状态"}, index = 4)
    private String auditStatusName;//状态
    @ColumnWidth(20) // 定义列宽
    @ExcelProperty(value = {"提货状态"}, index = 5)
    private String pickingStatusName;//提货状态
    @ColumnWidth(20) // 定义列宽
    @ExcelProperty(value = {"管理人"}, index = 6)
    private String managerUserName;//管理人姓名(多个逗号分隔)


    @ExcelIgnore
    private Integer auditStatus;//审核状态审核状态:0草稿1审核中2已完结
    @ExcelIgnore
    private Long id;//主键ID
    @ExcelIgnore
    private Long orgId;//和组织管联 yz_sys_org where category = 3、
    @ExcelIgnore
    private Integer pickingStatus;//提货状态
    @ExcelIgnore
    private Long createUserId;//创建人
}

第四步:
调用方法

    //导出Excel
    @RequestMapping(value="/exportExcel", method = RequestMethod.GET)
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) {
        try {
            String customerId = request.getParameter("customerId");
            String auditStatus = request.getParameter("auditStatus");
            String pickingStatus = request.getParameter("pickingStatus");

            if(StringUtils.isBlank(customerId)){
                customerId="0";
            }if(StringUtils.isBlank(auditStatus)){
                auditStatus="-1";
            }if(StringUtils.isBlank(pickingStatus)){
                pickingStatus="0";
            }
            List<CustomerInfoExcel> list = customerInfoService.selectAllCustomerInfoExcel(Long.parseLong(customerId),Integer.parseInt(auditStatus),Integer.parseInt(pickingStatus));
            ExcelUtils.export2Web(response, "客户信息", "客户信息", CustomerInfoExcel.class, list);
        } catch (Exception e) {
            log.error("报表导出异常:", e);
        }
    }
  • 7
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
可以通过以下步骤在uniapp中使用Java easyexcel导出Excel: 1. 在Java中编写导出Excel的代码,并将其打包成jar包或war包。 2. 在uniapp中使用axios或其他网络请求库将数据发送到Java后端。 3. 在Java后端中解析数据,使用easyexcel生成Excel文件,并将其返回给uniapp前端。 4. 在uniapp前端中使用js-xlsx或其他Excel解析库解析Excel文件,以便在前端进行处理。 以下是一个简单的Java代码示例,用于将数据导出到Excel文件中: ```java public void exportExcel(HttpServletResponse response) throws IOException { List<User> userList = userService.getUserList(); // 设置表头 List<List<String>> head = new ArrayList<>(); List<String> head0 = new ArrayList<>(); head0.add("编号"); head0.add("姓名"); head0.add("年龄"); head.add(head0); // 设置表格数据 List<List<Object>> data = new ArrayList<>(); for (User user : userList) { List<Object> row = new ArrayList<>(); row.add(user.getId()); row.add(user.getName()); row.add(user.getAge()); data.add(row); } // 导出Excel文件 response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); response.setHeader("Content-disposition", "attachment;filename=user.xlsx"); ServletOutputStream out = response.getOutputStream(); EasyExcel.write(out).head(head).sheet("用户数据").doWrite(data); } ``` 在uniapp中,您可以使用以下代码将数据发送到Java后端: ```javascript exportExcel() { axios.post('/api/exportExcel', this.userList) .then(response => { let blob = new Blob([response.data], {type: 'application/vnd.ms-excel'}) let link = document.createElement('a') link.href = URL.createObjectURL(blob) link.download = 'user.xlsx' link.click() }) } ``` 在上面的代码中,我们使用axios将用户列表发送到Java后端的/api/exportExcel接口。后端接口将生成Excel文件并将其作为响应返回。在前端,我们使用Blob和URL.createObjectURL将响应数据转换为Excel文件,并使用a标签模拟点击以下载文件

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值