{"id":3367,"date":"2021-07-11T22:47:14","date_gmt":"2021-07-11T14:47:14","guid":{"rendered":"http:\/\/123.57.164.21\/?p=3367"},"modified":"2021-07-11T22:47:14","modified_gmt":"2021-07-11T14:47:14","slug":"exceljs-%e7%94%9f%e6%88%90excel%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=3367","title":{"rendered":"ExcelJS \u751f\u6210Excel\u6587\u4ef6"},"content":{"rendered":"\n<p>\u6709\u7684\u65f6\u5019\u6211\u4eec\u5e0c\u671b\u80fd\u5728Server\u7aef\u901a\u8fc7\u4ee3\u7801\u628a\u6570\u636e\u751f\u6210Excel\u6587\u4ef6\uff0c\u5982\u679c\u662fNodejs\u7684\u8bdd\uff0cExcelJs\u53ef\u4ee5\u5f88\u597d\u7684\u6ee1\u8db3\u6211\u4eec\u7684\u8981\u6c42\u3002\u4f7f\u7528\u975e\u5e38\u7b80\u5355\uff0c\u53ef\u4ee5\u770b\u5b98\u7f51\u7684\u4f8b\u5b50\u3002<\/p>\n\n\n\n<p>ExcelJS\u5b98\u65b9\u5730\u5740\uff1ahttps:\/\/www.npmjs.com\/package\/exceljs<\/p>\n\n\n\n<p>\u4e2d\u6587\u6587\u6863\u5730\u5740\uff1ahttps:\/\/github.com\/exceljs\/exceljs\/blob\/HEAD\/README_zh.md<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/07\/image-2-1024x633.png\" alt=\"\" class=\"wp-image-3368\" width=\"560\" height=\"346\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-2-1024x633.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-2-300x186.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-2-768x475.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-2-1536x950.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-2-2048x1267.png 2048w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-2-830x513.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-2-230x142.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-2-350x217.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-2-480x297.png 480w\" sizes=\"(max-width: 560px) 100vw, 560px\" \/><\/figure><\/div>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2aNodejs \u4f7f\u7528\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const detailTextFont = {\n  \/\/ \u5b57\u4f53\u540d\n  name: 'Yu Gothic Regular (\u672c\u6587)',\n  \/\/ Font family for fallback. An integer value. \n  family: 4,\n  \/\/ \u5b57\u4f53\u5927\u5c0f\n  size: 11,\n  \/\/ \u4e0b\u5212\u7ebf\n  underline: false,\n  \/\/ \u52a0\u7c97\n  bold: false,\n};\n\n\/\/ nodejs \u65b9\u6cd5\nrouter.post('\/createExcel', async function (req, res) {\n\n  const jsonParm = req.body;\n  \/\/ \u4ece\u6570\u636e\u5e93\u53d6\u5f97\u67d0\u4e9b\u6570\u636e\n  const returnData = dataBase.xxxxxxx(dbschema, jsonParm)\n\n\n  \/\/ \u751f\u6210workbook\u5bf9\u8c61\n  const workbook = new Excel.Workbook();\n  \/\/ \u8bfb\u53d6\u672c\u5730\u7684\u4e00\u4e2aExcel\u6587\u4ef6\uff08\u6a21\u7248\u6587\u4ef6\uff09\n  await workbook.xlsx.readFile(\".\/template.xlsx\");\n  \/\/ \u83b7\u53d6\u6587\u4ef6\u7684\u4e00\u4e2aSheet\u9875\u9762\n  const worksheet = workbook.getWorksheet('shee1');\n  \/\/ \u7ed9K1\u5355\u5143\u683c\u8d4b\u503c\n  worksheet.getCell('K1').value = \"XXXX\";\n  \/\/ \u7ed9N4\u5355\u5143\u683c\u8d4b\u503c\n  worksheet.getCell('N4').value = \"XXXXX\";\n  \/\/ \u7ed9N4\u5355\u5143\u683c\u8d4b\u503c\u8bbe\u5b9a\u5b57\u4f53\n  worksheet.getCell('N4').font = detailTitleFontBold;\n\n  \/\/ \u4ece\u7f51\u7edc\u4e0a\u4e0b\u8f7d\u56fe\u7247\u5230Server\u7aef\u672c\u5730\u76ee\u5f55\n  var downLoadFlag = await downLoadImage(\"imageName\")\n\n  \/\/ \u5982\u679c\u4e0b\u8f7d\u6210\u529f\uff0c\u628a\u56fe\u7247\u63d2\u5165\u5230Excel\u4e2d\n  if (downLoadFlag) {\n      const imageId1 = workbook.addImage({\n        filename: imageName,\n        extension: 'png',\n      });\n      worksheet.addImage(imageId1, 'D37:T47');\n    }\n  }\n\n  \/\/ \u4fdd\u5b58\u4fee\u6539\u5230Excel\u6587\u4ef6\n  workbook.xlsx.writeFile(fileName + \".xlsx\").then(function () {\n    \/\/ \u5982\u679c\u6210\u529f\uff0c\u8bfb\u53d6\u628aExcel\u6587\u4ef6\uff0c\u5e76\u628aExcel\u6587\u4ef6\u8fd4\u56de\u5230Http Response\u4e2d\n    fs.createReadStream(fileName + \".xlsx\").pipe(res);\n    \/\/ \u8bbe\u5b9aHttp Response\u7684Header\uff0c\u6ce8\u610f\u8de8\u57df\n    res.writeHead(200, {\n      'Access-Control-Allow-Origin': '*',\n      'Access-Control-Allow-Headers': 'X-Requested-With',\n      'Access-Control-Allow-Methods': 'PUT,POST,GET,DELETE,OPTIONS',\n      'Content-Type': 'application\/octet-stream;charset=UTF8',\n      'Content-Disposition': 'attachment; filename=' + fileName + '.xlsx'\n    });\n\n    \/\/ \u540e\u7eed\u64cd\u4f5c \u5220\u9664\u76f8\u5173\u7684\u56fe\u7247 \u548c \u751f\u6210\u7684Excel\u6587\u4ef6\u3002\n    fs.unlinkSync(fileName + \".xlsx\");\n    fs.unlinkSync(imageName);   \n    }\n  });\n});<\/pre>\n\n\n\n<p>\u6d4f\u89c8\u5668\u7aefVUE\u8c03\u7528\u7684\u4f8b\u5b50<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">createExcelFile() {\n      return new Promise((resolve, reject) => {\n     \n        let jsonData = {\n          ids: xxxx,\n        };\n        this.$axios({\n          \/\/ timeout\u65f6\u95f4\n          timeout: 180000,\n          method: \"post\",\n          url:  \"https\uff1a\/\/xxxxx\/createExcel\",\n          data: jsonData,\n          \/\/ \u6ce8\u610fresponseType blob\n          responseType: \"blob\"\n        })\n          .then((response) => {\n            const fileName = new Date().format(\"yyyyMMddhhmm\");\n            let url = window.URL.createObjectURL(new Blob([response.data]));\n            let link = document.createElement(\"a\");\n            link.style.display = \"none\";\n            link.href = url;\n            link.setAttribute(\"download\", fileName + \".xlsx\");\n            document.body.appendChild(link);\n            link.click();\n          })\n          .catch((error) => {\n            reject(error);\n          });\n      });\n    },<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u7684\u65f6\u5019\u6211\u4eec\u5e0c\u671b\u80fd\u5728Server\u7aef\u901a\u8fc7\u4ee3\u7801\u628a\u6570\u636e\u751f\u6210Excel\u6587\u4ef6\uff0c\u5982\u679c\u662fNodejs\u7684\u8bdd\uff0cExcelJs\u53ef\u4ee5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3367"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3367"}],"version-history":[{"count":3,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3367\/revisions"}],"predecessor-version":[{"id":3371,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3367\/revisions\/3371"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}