and generating xlsx nodeJS styling

reference:

https://www.npmjs.com/package/xlsx-style

https://www.jianshu.com/p/877631e7e411

https://sheetjs.gitbooks.io/docs/#streaming-read

 

Installation depends: npm install xlsx-style node-xlsx xlsx, dependent installed version:

  "dependencies": {
    "node-xlsx": "^0.15.0",
    "xlsx": "^0.14.4",
    "xlsx-style": "^0.8.13"
  }

 

Three or more copies js file to a custom directory, such as node-xlsx-c

Modify the code node-xlsx-c / index.js: xlsx modify xlsx-style

Write xlsx generated code:

the require FS = const ( "FS" ); 

/ * * 
 * generating XLSX 
 * @param generate a file path P 
 * @param cols column names 
 * @param data line, a two-dimensional array 
 * / 
function genXlsx (P, cols, Data) { 
    nodeXlsx the let = the require ( './ Node-XLSX-C' ); 
    the let D = [{ 
        name: "Sheet1" , 
        Data: [ 
            cols, 
            ... Data 
        ] 
    }]; 
    // cols specify the width of the column! 
    FS. writeFileSync (P, nodeXlsx.build (D, {
         'cols!': [{WCH: 60}, {WCH: 20 is }] 
    }), { 'In Flag': 'W' });
}
// Specify cell contents style: four directions black frame 
the let contentCellStyle = { 
    border: { 
        Top: { 
            style: "Medium", Color: "# 000" 
        }, 
        bottom: { 
            style: "Medium", Color: " 000 # " 
        }, 
        left: { 
            style: " Medium ", Color:" # 000 " 
        }, 
        right: { 
            style: " Medium ", Color:" # 000 " 
        }, 
    } 
}; 
// the specified title cell styles: bold centering 
the let headerStyle = { 
    font: { 
        Bold:true
    },
    alignment: {
        horizontal: "center"
    }
}

genXlsx("1.xlsx", [{
    v: "表头1",
    s: headerStyle
}, {
    v: "表头2",
    s: headerStyle
}], [[{
    v: 123,
    s: contentCellStyle
}, {
    v: 456,
    s: contentCellStyle
}]])

Code running in the current directory 1.xlsx:

 

Guess you like

Origin www.cnblogs.com/hellohello/p/11240326.html