Nodejs+express using Ejsexcel to do complex export excel

Source: Internet
Author: User

  • Note: You need to understand the template to accept the data format, not directly to get up on it, usually not to have a match degree.
    • Extracting data from the database
  • The roughly processed data format is:
  • These are all ready you need something, template;

    <%forrbegin rs,i in data[0]%>: start loop; Because the data is encapsulated as
    [
    [{},{},{},{}.....]
    ]; so through data[0],
    You can take out all the data and apply the array in the array;
    <%forrend%>: End loop;
    <%=RS.ISSUE_NAME%>: Use your encapsulated key to fetch values;
  • Now is the Code section
    1. Join Ejsexcel
      var ejsexcel = require (' Ejsexcel ');
      var fs = require (' FS ');
      Of course, you need to add this module before using (NPM install ejsexcel)
  • ./GET Export Excel Test. Exporting Excel Topics/
    Router.get ('/exportexcel ', function (req, res, next) {
    var filedir = './public/uploadfile/temporaryfile/';
    Fs.ensuredir (Filedir, function (err) {
    });
    var meet_name = Req.query.meet_name;
    try {
    Before exporting, determine if the file to be exported exists, according to the requirements
    Fs.exists ('./public/uploadfile/temporaryfile/' + meet_name + ' _xxx.xlsx ', function (r) {
    if (r) {
    Fs.unlink ('./public/uploadfile/temporaryfile/' + meet_name + ' _xxx.xlsx ', function (err) {
    if (err) throw err;
    })
    }
    Issueservice.getissueinfo (1, 10000, meet_name, function (result) {
    Querying data
    if (Result.rows.length < 1) {
    No data returned 0 page get the return value to make a corresponding prompt
    Res.send ("0");
    } else {
    Fs.ensuredir (Filedir, function (err) {});
    (Async function () {
    var datas = [];
    var data = [];
    for (var i = 0; i < result.rows.length; i++) {
    /*
    Processing data for the above format
    /
    Data.push (Result.rows[i]);
    }
    Datas.push (data);
    To get the buffer object for an Excel template
    Const EXLBUF = await Readfileasync ('./public/uploadfile/template/issue.xlsx ');
    Render an Excel template with data source (object)
    Const EXLBUF2 = await ejsexcel.renderexcel (exlbuf, datas);
    Await Writefileasync ('./public/uploadfile/temporaryfile/' + Meet_name + ' _ Issue report. xlsx ', exlBuf2);
    SetTimeout (() = {
    Console.info ("delete issue");
    try {
    Fs.access ('./public/uploadfile/temporaryfile/' + meet_name + ' _xxx.xlsx ', Fs.constants.F_OK,
    (err) = {
    if (!err) {
    Fs.unlinksync ('./public/uploadfile/temporaryfile/' + meet_name + ' _xxx.xlsx ')
    }
    });
    } catch (e) {
    Console.info ("exception");
    Console.error (e);
    }
    }, 5 -1000);
    Returns the generated table path for download
    Res.send ('/uploadfile/temporaryfile/' + meet_name + ' _xxx.xlsx ');
    })();
    }
    });
    });
    } catch (Error) {
    Return exception
    Res.send ("err");
    }
    });
    Note: At any time, the code problems do not catch the urgent, see if the error is not a module, or quotation marks, and so on, in short
    The more edgy the new middle, the more time it takes to solve the problem
  • Nodejs+express using Ejsexcel to do complex export excel

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.