收藏
回答

云函数如何使用npm:excel4node 导出带图片excel?

npm地址:https://github.com/natergj/excel4node

欢迎指教,拜谢大佬~

回答关注问题邀请回答
收藏

2 个回答

  • 郑旭东
    郑旭东
    2022-03-23
    //这个例子有导出图片功能
    const xl = require('excel4node')
    exports.main = async (event) => {
      // create a new instance of a Workbook class
      const wb = new xl.Workbook()
      
      // cdd Worksheets to the workbook
      const ws = wb.addWorksheet('列表')
    
      
      { //for循环填充行列数据i和j,自己编吧,这里仅用花括号意思一下
        
        //i循环里,设定行高,控制图片显示大小
        ws.row(i + 1).setHeight(65) 
    
        //j循环里,把图片插在第一列
        let res = await cloud.downloadFile({
          fileID
        })
        let image = res.fileContent
        ws.addImage({
          image,
          type: 'picture',
          position: {
            type: 'twoCellAnchor',
            from: {
              col: j + 1,
              colOff: 0,
              row: i + 1,
              rowOff: 0,
            },
            to: {
              col: j + 2,
              colOff: 0,
              row: i + 2,
              rowOff: 0,
            },
          },
        })
    
      }
    
      const buffer = await wb.writeToBuffer()
    
      const resFile = await cloud.uploadFile({
        cloudPath: cloudPath,
        fileContent: buffer
      })
    
    }
    
    2022-03-23
    有用 3
    回复 7
    • 大雪纷飞
      大雪纷飞
      2022-03-30
      excel4node合并单元格方法是啥了,有没有教程连接。遇到了问题解决不了
      2022-03-30
      回复
    • 郑旭东
      郑旭东
      2022-03-31回复大雪纷飞
      试试: ws.cell(2, 1, 2, 6, true).string('One big merged cell')

      文档在这里:https://github.com/natergj/excel4node
      2022-03-31
      1
      回复
    • 无道
      无道
      2022-04-24
      Error: cloud.callFunction:fail Error: errCode: -504002 functions execute fail | errMsg: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
          at validateString (internal/validators.js:117:11)
          at Object.basename (path.js:1156:5)
          at new Picture (/var/user/node_modules/excel4node/distribution/lib/drawing/picture.js:55:89)
          at DrawingCollection.add (/var/user/node_modules/excel4node/distribution/lib/drawing/index.js:22:34)
          at Worksheet.addImage (/var/user/node_modules/excel4node/distribution/lib/worksheet/worksheet.js:268:51)
          at /var/user/index.js:107:8
          at Array.forEach (<anonymous>)
          at Runtime.exports.main [as handler] (/var/user/index.js:77:13)
          at processTicksAndRejections (internal/process/task_queues.js:97:5) (callId: 1650792911572-0.21096792130201525) (trace: 17:35:11 start->17:35:13 system error (Error: errCode: -504002 functions execute fail | errMsg: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
          at validateString (internal/validators.js:117:11)
          at Object.basename (path.js:1156:5)
          at new Picture (/var/user/node_modules/excel4node/distribution/lib/drawing/picture.js:55:89)
          at DrawingCollection.add (/var/user/node_modules/excel4node/distribution/lib/drawing/index.js:22:34)
          at Worksheet.addImage (/var/user/node_modules/excel4node/distribution/lib/worksheet/worksheet.js:268:51)
          at /var/user/index.js:107:8
          at Array.forEach (<anonymous>)
          at Runtime.exports.main [as handler] (/var/user/index.js:77:13)
          at processTicksAndRejections (internal/process/task_queues.js:97:5)), abort)
          at C (http://127.0.0.1:16367/appservice/__dev__/WAService.js?t=wechat&s=1650785945374&v=2.23.4:2:658323)
          at http://127.0.0.1:16367/appservice/__dev__/WAService.js?t=wechat&s=1650785945374&v=2.23.4:2:684615
          at http://127.0.0.1:16367/appservice/__dev__/WAService.js?t=wechat&s=1650785945374&v=2.23.4:2:46812
          at n (http://127.0.0.1:16367/appservice/__dev__/WAService.js?t=wechat&s=1650785945374&v=2.23.4:2:23689)
      2022-04-24
      回复
    • 无道
      无道
      2022-04-24
      ws.addImage 方法报错,说的好像是必须要传 path,并且是个string类型,您的这个 image  不是string类型
      2022-04-24
      回复
    • 郑旭东
      郑旭东
      2022-04-25回复无道
      你别管我那个具体写法,我那是从云存储里拿的。你自己放一个图片网址进去就行。
      2022-04-25
      回复
    查看更多(2)
  • 萧萧
    萧萧
    2022-03-23
    //这个例子就差个导出图片的功能
    const cloud = require('wx-server-sdk')
    //这里最好也初始化一下你的云开发环境
    cloud.init()
    const db = cloud.database()
    const xlsx = require('node-xlsx');//引入npm
    exports.main = async (event, context) => {
      //查询要生成表格的数据
      const dataList = await db.collection("bhds").where({
        kdid: event.kdid,
        sssj:event.sssj,
      }).limit(1000).get()
      //获取数据
      const data = dataList.data
      //获取创建工作簿
      let sheet = []
      //设置表头名称
      let title = ['图片','商家代号','款号''商品名称''颜色''均码','XS','S','M','L','XL','XXL','XXXL','总数','拿货价',]
      await sheet.push(title)
      //循环写入数据
      for (let rowIndex in data) {
        let rowcontent = []
        rowcontent.push(data[rowIndex].cpimg[0]+'/sf2')
        rowcontent.push(data[rowIndex].sssj)
        rowcontent.push(data[rowIndex].hh)
        rowcontent.push(data[rowIndex].cpname)
        rowcontent.push(data[rowIndex].color)
        rowcontent.push(data[rowIndex].jm)
        rowcontent.push(data[rowIndex].xs)
        rowcontent.push(data[rowIndex].s)
        rowcontent.push(data[rowIndex].m)
        rowcontent.push(data[rowIndex].l)
        rowcontent.push(data[rowIndex].xl)
        rowcontent.push(data[rowIndex].xxl)
        rowcontent.push(data[rowIndex].xxxl)
        rowcontent.push(data[rowIndex].zs)
        rowcontent.push(data[rowIndex].cbj)
        await sheet.push(rowcontent)
      }
      //生成文件
      const buffer = await xlsx.build([{
        name: event.nhmc+event.nazt,
        data: sheet
      }])
      //上传文件到云存储
      return await cloud.uploadFile({
        cloudPath: 'nhb/' +event.sssjs+ event.nhmc+event.nazt + Date.now() + '.xlsx',
        fileContent: buffer,
      })
    }
    
    2022-03-23
    有用 1
    回复
登录 后发表内容