SpringCloud--微服务数据实现表格下载

xl_echo编辑整理,欢迎转载,转载请声明文章来源。更多IT编程案例、资料请联系QQ:1280023003 ,群:298140694
百战不败,依不自称常胜,百败不颓,依能奋力前行。——这才是真正的堪称强大!!


前言:微服务架构导出表格的技术比较多,不过也有需要注意的问题,不然可能不会成功。这里提一个问题,也是亲测的时候出现的问题。springcloud项目在处理返回数据的时候,网关做了统一处理,所以当表格制作成功时,会被网关拦截。解决办法(在接口路径上加一个export即可)。这里使用了POI对数据进行导出。

需要使用POI,请先导入它的依赖

<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>3.17</version>
</dependency>

工具类,请导入项目(声明:该工具类来源于网上的代码整理,并对代码进行过修改编写而成)

package com.echo.demo;

import java.io.IOException;
import java.net.URLEncoder;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import sun.misc.BASE64Encoder;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FileUtils {
  /**
   * 下载文件时,针对不同浏览器,进行附件名的编码
   *
   * @param filename
   *            下载文件名
   * @param agent
   *            客户端浏览器
   * @return 编码后的下载附件名
   * @throws IOException
   */
  public static String encodeDownloadFilename(String filename, String agent)
      throws IOException {
    if (agent.contains("Firefox")) { // 火狐浏览器
      filename = "=?UTF-8?B?"
          + new BASE64Encoder().encode(filename.getBytes("utf-8"))
          + "?=";
      filename = filename.replaceAll("\r\n", "");
    } else { // IE及其他浏览器
      filename = URLEncoder.encode(filename, "utf-8");
      filename = filename.replace("+"," ");
    }
    return filename;
  }

  public static void excelExport(HttpServletResponse response, HttpServletRequest request, HSSFWorkbook hssfWorkbook, String filename){

    // 通过浏览器下载导出
    // 设置表头信息
    ServletOutputStream outputStream = null;
    try {
      outputStream = response.getOutputStream();
      response.setContentType("application/vnd.ms-excel,charset=utf-8");
      String agent = request.getHeader("user-agent");
      filename = encodeDownloadFilename(filename, agent);
      response.setHeader("Content-Disposition", new String(("attachment;filename=" + filename).getBytes(), "iso-8859-1"));
      hssfWorkbook.write(outputStream);
    } catch (IOException e) {
      e.printStackTrace();
    }finally {
      // 关闭
      try {
        hssfWorkbook.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

}

接下来使用一个demo来演示一下

public void getDownExcel(HttpServletResponse response, HttpServletRequest request) {

    //这个list使数据库生成的
    List<Customer> list = Fin.getList();
    // 得到结果,生成Excel文件
    HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
    HSSFSheet sheet = hssfWorkbook.createSheet("统计表");
    // 生成表头
    HSSFRow headRow = sheet.createRow(0);
    headRow.createCell(0).setCellValue("表头1");
    headRow.createCell(1).setCellValue("表头2");
    headRow.createCell(2).setCellValue("表头3");
    headRow.createCell(3).setCellValue("表头4");
    headRow.createCell(4).setCellValue("表头5");

    // 将数据插入表中
    for (Customer customer : list) {
      HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);

      //日期格式转换
      Date date = new Date(customer.getDate());
      SimpleDateFormat sdFmt = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
      dataRow.createCell(0).setCellValue(sdFmt.format(date));

      dataRow.createCell(1).setCellValue(customer.getUm());
      dataRow.createCell(2).setCellValue(customer.getUm());
      dataRow.createCell(3).setCellValue(customer.getUm());
      dataRow.createCell(4).setCellValue(customer.getUm());
    }

String filename = "充值统计表.xls";
FileUtils.excelExport(response, request, hssfWorkbook, filename);

}

注意:HttpServletResponse response, HttpServletRequest request,这两个参数直接写好就行了,接口里面也照样写,会自动注入,不需要手动传递参数。

猜你喜欢

转载自blog.csdn.net/xlecho/article/details/80825804