如何在Javascript中将条件格式添加到Excel文件?

时间:2016-06-07 14:54:27

标签: javascript node.js excel excel-formula xlsx

如何在Node.js中生成的Excel文件中的某些列中添加条件格式?当我将xml添加到工作表中时,如answer我在Excel中收到错误,认为文件打开了。我在Node.js中使用xlsx生成文件,因此Excel文件内部如下所示:

  • Sample.xlsx
    • [CONTENT_TYPES]的.xml
    • ...
    • XL
      • _rels
      • sharedString.xml
      • styles.xml
      • 主题
      • workbook.xml
      • 工作表
        • sheet1.xml

我将条件格式写入sheet1.xml,这是执行此操作的代码:

var fs = require("fs");
var jszip = require("jszip");
var CONDITIONAL_FORMATTING = '<conditionalFormatting sqref="A1">' +
  '<cfRule type="expression" dxfId="0" priority="1"><formula>LEN(A1)&' +
  'gt;2</formula></cfRule></conditionalFormatting>';
var WORKSHEET1 = 'xl/worksheets/sheet1.xml';

fs.readFile("excel/rewrite.xlsx", function(err, data) {
    jszip.loadAsync(data).
        then(function(zip) {
            rezip = zip;
            return zip.file(WORKSHEET1).async("string");
        }).then(function (worksheet) {
            var pos = worksheet.indexOf('<pageMargins');
            if(pos > 0) {
                worksheet = worksheet.substring(0,pos) +
                  CONDITIONAL_FORMATTING + worksheet.substring(pos);
            } 
            rezip.file(WORKSHEET1,worksheet);
            rezip.generateNodeStream({type:'nodebuffer',streamFiles:true})
                .pipe(fs.createWriteStream('out.xlsx'))
                .on('finish', function () {
                    console.log('done');    
                });
        });
});

1 个答案:

答案 0 :(得分:1)

我想出了如何在Excel中为列添加条件格式,以便我共享我的解决方案。此代码读取由xlsx生成的Excel文件,并向A列和C列添加条件格式。技巧是styles.xml和sheet1.xml都需要修改,以便条件格式具有要应用的样式。

var fs = require("fs");
var jszip = require("jszip");
var WORKSHEET1 = 'xl/worksheets/sheet1.xml';
var STYLES = 'xl/styles.xml';
var STYLE_WITHOUT_CONDITIONAL_FORMATTING = '<dxfs count="0"/>';
var STYLE_WITH_CONDITIONAL_FORMATTING = '<dxfs count="1"><dxf><font>' + 
   '<color rgb="FF9C0006"/></font><fill><patternFill>' +
   '<bgColor rgb="FFFFC7CE"/></patternFill></fill></dxf></dxfs>';
var zip;

function buildConditionalFormulas(worksheet,columnFormats) {
    var conditionalFormatting = '';
    var i;
    for(i = 0; i < columnFormats.length; i++) {
        conditionalFormatting += '<conditionalFormatting sqref="' + columnFormats[i].column +
            '1:' + columnFormats[i].column + '200"><cfRule type="expression" dxfId="0" ' +
            ' priority="1"><formula>LEN(' + columnFormats[i].column + '1)&gt;' +
            columnFormats[i].maximum + '</formula></cfRule></conditionalFormatting>';
    }
    conditionalFormatting += '<pageMargins';
    return worksheet.replace('<pageMargins',conditionalFormatting);
}

fs.readFile("excel/rewrite.xlsx", function(err, data) {
    jszip.loadAsync(data).
        then(function(ziper) {
            zip = ziper;
            return zip.file(WORKSHEET1).async("string");
        }).then(function (worksheet) {
            worksheet = buildConditionalFormulas(worksheet,[{column:'A',maximum:5},{column:'C',maximum:10}]);
            zip.file(WORKSHEET1,worksheet);
            return zip.file(STYLES).async("string");
        }).then(function (styles) {
            styles = styles.replace(STYLE_WITHOUT_CONDITIONAL_FORMATTING,STYLE_WITH_CONDITIONAL_FORMATTING);
            zip.file(STYLES,styles);
            zip.generateNodeStream({type:'nodebuffer',streamFiles:true})
                .pipe(fs.createWriteStream('out.xlsx'))
                .on('finish', function () {
                    console.log('done');
                });
        });
});