Java数据Exal表格导出数据

前端代码

    /** 导出按钮操作 */
    handleExport() {
      if (this.tableData.length == 0) {
        this.$message({
          message: '没有考生信息可导出',
          type: 'warning'
        })
      } else {
        this.$modal.confirm('确定导出所有统计信息吗?').then(() => {
          let fileName = '需要导出名称.xlsx'
            this.download('数据路径', this.form, fileName)
//例:
//          this.download('/analyse/planCycle/export', this.form, fileName)
        })
      }
    },

后端代码:

实际处理类:传入list对象

    /**
     * 导出考生信息列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, StaPlanCycle staPlanCycle) throws IOException {
        int index = 0;
//      查询需要导出的数据:
        List<StaPlanCycle> list = staPlanCycleService.selectStaPlanCycleList(staPlanCycle);
        AnalyseFileUtil analyseFileUtil = new AnalyseFileUtil();
        Map<String, Object> map = new HashMap();
        for (StaPlanCycle planCycle : list) {
            planCycle.setIndex(++index);
        }
        map.put("mapList", list);
        analyseFileUtil.exportExcel(map, "表格文件名称", "表格文件名称", "export", response);
    }

public class AnalyseFileUtil {


    public String srcDir = "/zip/";

    private static final int BUFFER_SIZE = 2 * 1024;
    private static final String OS_NAME="os.name";
    private static final String STR_WINDOWS="windows";
    private static final String STR_TEMPLATES="templates";
    private static final String STR_CONTEND_DISPOSITION="Content-Disposition";
    private static final String STR_ATTACHMENT="attachment;filename=";
    private static final String STR_UTF8="UTF-8";



    private String filePath = "/";

    /**
     * 按模板导出Excel数据(支持文件流读取文件)
     *
     * @param paramsMap       数据
     * @param fileName        模板名称
     * @param goalName        文件名称
     * @param fileTemplateUrl 模板地址
     * @return 文件存储地址
     * @throws Exception
     */
    public String exportExcel(Map<String, Object> paramsMap, String fileName, String goalName, String fileTemplateUrl, HttpServletResponse response) {
        String templateFileName = "";
        try {
            String os = System.getProperty(OS_NAME);
            if (os != null && os.toLowerCase().startsWith(STR_WINDOWS)) {
                templateFileName = this.getClass().getClassLoader().getResource("").getPath();
                // 获取resource目录下的模板位置
                templateFileName = templateFileName.substring(1);
                templateFileName = templateFileName.replace("/", File.separator);
            } else {
                templateFileName = filePath;
            }
            templateFileName = templateFileName + STR_TEMPLATES + File.separator + fileTemplateUrl + File.separator + fileName + ".xlsx";
            // 第二个参数true是为了开启多sheet扫描,就是同一个xls文件中有多个工作表的时候。
            TemplateExportParams params = new TemplateExportParams(templateFileName, true);
            // 数据载入,生成excel文件
            Workbook workbook = ExcelExportUtil.exportExcel(params, paramsMap);
            //写文件流
            response.setCharacterEncoding(STR_UTF8);
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader(STR_CONTEND_DISPOSITION, STR_ATTACHMENT + URLEncoder.encode(goalName + ".xlsx", STR_UTF8));
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
            //"按模板导出Excel数据异常:" + e
            return null;
        }
        return goalName;
    }

}

pom文件(如果不引可能会报错):

     <!-- poi-tl -->
        <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.10.0</version>
        </dependency>
        <!-- easy-poi -->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>30.1-jre</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>
    </dependencies>

如果要指定字段到Exal表可以以下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值