后端返回二进制流文件 excle导出 或者xls xlsx docx pdf导出

方法一 

//数据查询记录 excle导出
export function exportInterfaceSearch(data) {
  return request({
    url: '/risen/data/exportInh',
    method: 'post',
    responseType: 'blob',   // 第一步
    data
  })




第二步  res 是后端返回的二进制流文件

        const url = window.URL.createObjectURL(res)
        const a = document.createElement('a')
        a.style.display = 'none'
        a.href = url
        a.download = '数据目录'
        document.body.appendChild(a)
        a.click()
        document.body.removeChild(a)
        window.URL.revokeObjectURL(url)

或者 这种写法

//接口列表导出
export function exportInterfaceManagement(params) {
  return request({
    method: 'get',
    isFormData: true,
    url: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    responseType: 'blob',
    params
  })
}
    async impotout() {//导出
    
      exportInterfaceManagement({
   
      }).then((res) => {
        const blob = new Blob([res], {
          type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
        })
        const a = document.createElement('a')
        const href = window.URL.createObjectURL(blob)
        a.href = href
        a.download = '接口列表信息'
        document.body.appendChild(a)
        a.click()
        // console.log(a)
        document.body.removeChild(a)
        window.URL.revokeObjectURL(href)
        // console.log(res)
      })

方法二 ://多种类型 可以定义一个方法 导出

api

//接口管理文档下载
export function exportwordlist(params) {
  return request({
    method: 'get',
    isFormData: true,
    url: '/sen/gatay/interce/downInteceDoc',
    responseType: 'blob',
    params
  })
}

定义文档 名称 和类型 

  exportwordlist({interfaceUuid: record.uuid}).then(res => {
            this.$_downloadFile(res, '接口文档', 'doc') //res:返回数据
  })
this.$_downloadFile(res, '接口文档', 'doc') //res:返回数据

 

  $_downloadFile(obj, name, suffix) {
      const DOWNLOAD_TYPE_MAP = {
        xls: 'application/vnd.ms-excel',
        xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        doc: 'application/msword',
        docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        pdf: 'application/pdf'
      }
      if (!DOWNLOAD_TYPE_MAP[suffix]) {
        throw new Error('请传入文件下载的格式后缀,eg:xls,xlsx,doc,docx,pdf')
      }
      const blob = new Blob([obj], {
        type: DOWNLOAD_TYPE_MAP[suffix]
      })
      const fileName = `${name}.${suffix}`
      let link = document.createElement('a')
      document.body.appendChild(link)
      link.href = URL.createObjectURL(blob)
      link.setAttribute('download', fileName)
      link.click()
      document.body.removeChild(link)
      URL.revokeObjectURL(link.href) // 销毁url对象
      this.$message.success('下载成功')
    },

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的JSP页面,用于从数据库中检索数据并将其导出Excel文件: ```jsp <%@ page import="java.io.*,java.sql.*,org.apache.poi.ss.usermodel.*" %> <%@ page contentType="application/vnd.ms-excel" language="java" %> <%@ page import="javax.servlet.http.*,javax.servlet.*" %> <% // 设置响应头,提示浏览器下载文件 response.setHeader("Content-Disposition", "attachment; filename=myfile.xlsx"); // 创建工作簿和工作表对象 Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("数据"); // 创建标题行并设置单元格样式 Row headerRow = sheet.createRow(0); CellStyle headerCellStyle = workbook.createCellStyle(); headerCellStyle.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex()); headerCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); headerCellStyle.setAlignment(HorizontalAlignment.CENTER); Font headerFont = workbook.createFont(); headerFont.setColor(IndexedColors.WHITE.getIndex()); headerFont.setBold(true); headerCellStyle.setFont(headerFont); // 添加标题行的单元格 Cell headerCell1 = headerRow.createCell(0); headerCell1.setCellValue("ID"); headerCell1.setCellStyle(headerCellStyle); Cell headerCell2 = headerRow.createCell(1); headerCell2.setCellValue("名称"); headerCell2.setCellStyle(headerCellStyle); Cell headerCell3 = headerRow.createCell(2); headerCell3.setCellValue("价格"); headerCell3.setCellStyle(headerCellStyle); // 从数据库中检索数据并将其填充到工作表中 try { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "root", "password"); PreparedStatement ps = con.prepareStatement("SELECT * FROM products"); ResultSet rs = ps.executeQuery(); int rowNumber = 1; while (rs.next()) { Row row = sheet.createRow(rowNumber++); row.createCell(0).setCellValue(rs.getInt("id")); row.createCell(1).setCellValue(rs.getString("name")); row.createCell(2).setCellValue(rs.getDouble("price")); } con.close(); } catch (Exception e) { out.println(e); } // 将工作簿写入输出,并关闭工作簿 workbook.write(out); workbook.close(); %> ``` 该页面从数据库中检索数据,并将其填充到Excel文件的工作表中。我们使用Apache POI库来创建工作簿和工作表,并将数据填充到单元格中。在上面的示例中,我们使用MySQL数据库,但您可以将其替换为任何其他数据库,只需更改JDBC连接字符串、用户名和密码即可。在JSP页面的顶部,我们设置了响应头,以便浏览器将文件下载到本地计算机。我们还将页面的`contentType`设置为`application/vnd.ms-excel`,以便浏览器知道我们正在发送Excel文件

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值