2016-06-07 38 views
0

我應該如何將條件格式添加到我在Node.js中生成的Excel文件中的某些列中?當我將xml添加到工作表中像這樣answer時,我在Excel中收到錯誤,認爲文件打開。我產生Node.js的使用xlsx的文件,使Excel文件看起來像這裏面:如何在Javascript中添加條件格式到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

我想出瞭如何將條件格式添加到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'); 
       }); 
     }); 
});