Browse Source

报表导出样式生成逻辑更新

zhuth 6 years ago
parent
commit
257c2c01df
2 changed files with 128 additions and 52 deletions
  1. 1 0
      src/models/dashboardDesigner.js
  2. 127 52
      src/utils/exportor.js

+ 1 - 0
src/models/dashboardDesigner.js

@@ -904,6 +904,7 @@ export default {
                             });
                         }
                     }else if(viewType === 'richText') {
+                        content = content || '';
                         columns = [{
                             name: content.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\"/g, '&quot;').replace(/\'/g, '&apos;'),
                             width: 500,

+ 127 - 52
src/utils/exportor.js

@@ -21,38 +21,117 @@
         DateTime: 'date',
     },
     tmplStyleXML = '<Style ss:ID="{styleID}">{styleNode}</Style>',
-    tmplStyleNodeXML = '<{nodeName} {attributes}></{nodeName}>',
-    tmplStyleNodeAttributeXML = 'ss:{key}="{value}"',
+    tmplStyleNodeXML = '<{nodeName}{attributes}>{children}</{nodeName}>',
+    tmplStyleNodeAttributeXML = 'ss:{attributeName}="{attributeValue}"',
     styleConfig = [{
         name: "date",
-        tag: "NumberFormat",
-        key: "Format",
-        value: "yyyy-mm-dd"
+        tags: [{
+            tag: "NumberFormat",
+            attributes: {
+                Format: "yyyy-mm-dd"
+            }
+        }]
     }, {
         name: "bold",
-        tag: "Font",
-        key: "Bold",
-        value: "1"
+        tags: [{
+            tag: "Font",
+            attributes: {
+                Bold: "1"
+            }
+        }]
     }, {
         name: "center",
-        tag: ["Alignment"],
-        key: [["Horizontal", "Vertical"]],
-        value: [["Center", "Center"]]
+        tags: [{
+            tag: "Alignment",
+            attributes: {
+                Horizontal: "Center",
+                Vertical: "Center",
+            }
+        }]
     }, {
         name: "header",
-        tag: ["Font", "Interior"],
-        key: ["Color", ["Color", "Pattern"]],
-        value: ["#FFFFFF", ["#70AD47", "Solid"]]
+        tags: [{
+            tag: "Font",
+            attributes:{
+                Color: "#FFFFFF"
+            }
+        }, {
+            tag: "Interior",
+            attributes: {
+                Color: "#70AD47",
+                Pattern: "Solid"
+            }
+        }]
     }, {
         name: "even",
-        tag: ["Interior"],
-        key: [["Color", "Pattern"]],
-        value: [["#C6E0B4", "Solid"]]
+        tags: [{
+            tag: "Interior",
+            attributes: {
+                Color: "#C6E0B4",
+                Pattern: "Solid"
+            }
+        }]
     }, {
         name: "odd",
-        tag: ["Interior"],
-        key: [["Color", "Pattern"]],
-        value: [["#A9D08E", "Solid"]]
+        tags: [{
+            tag: "Interior",
+            attributes: {
+                Color: "#A9D08E",
+                Pattern: "Solid"
+            }
+        }]
+    }, {
+        name: "default",
+        tags: [{
+            tag: "Borders",
+            tags: [{
+                tag: "Border",
+                attributes: {
+                    Position: "Right",
+                    LineStyle: "Continuous",
+                    Weight: "1",
+                    Color: "#EAEAEA"
+                }
+            }]
+        }]
+    }, {
+        name: "border",
+        tags: [{
+            tag: "Borders",
+            tags: [{
+                tag: "Border",
+                attributes: {
+                    Position: "Top",
+                    LineStyle: "Continuous",
+                    Weight: "1",
+                    Color: "#EAEAEA"
+                }
+            }, {
+                tag: "Border",
+                attributes: {
+                    Position: "Right",
+                    LineStyle: "Continuous",
+                    Weight: "1",
+                    Color: "#EAEAEA"
+                }
+            }, {
+                tag: "Border",
+                attributes: {
+                    Position: "Bottom",
+                    LineStyle: "Continuous",
+                    Weight: "1",
+                    Color: "#EAEAEA"
+                }
+            }, {
+                tag: "Border",
+                attributes: {
+                    Position: "Left",
+                    LineStyle: "Continuous",
+                    Weight: "1",
+                    Color: "#EAEAEA"
+                }
+            }]
+        }]
     }],
     _format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) },
     _export = function(workbookXML, wbname) {
@@ -123,7 +202,7 @@
     Exportor.prototype.generateTableHeaderXML = function(header, columnCount) {
         let str = '';
         if(!!header) {
-            let styleID = this.addStyle(['center', 'header']);
+            let styleID = this.addStyle(['center', 'header', 'border']);
             let ctx = {
                 styleID: styleID,
                 columnCount: columnCount - 1,
@@ -146,7 +225,7 @@
     };
 
     Exportor.prototype.generateHeaderRowsXML = function(columns) {
-        let cells = columns.map(c => ({ type: 'String', value: c.name, styleNames: ['header', 'bold'] }));
+        let cells = columns.map(c => ({ type: 'String', value: c.name, styleNames: ['header', 'bold', 'default'] }));
         let ctx = {
             cells: this.generateCellsXML(cells)
         };
@@ -166,7 +245,7 @@
                 cells.push({
                     type: cols[j].type,
                     value: r[j],
-                    styleNames: [s].concat(cols[j].styleNames.concat(_defaultStyleIDs[cols[j].type] ? [_defaultStyleIDs[cols[j].type]] : []))
+                    styleNames: ['default', s].concat(cols[j].styleNames.concat(_defaultStyleIDs[cols[j].type] ? [_defaultStyleIDs[cols[j].type]] : []))
                 });
             }
             let ctx = {
@@ -228,46 +307,42 @@
 
     Exportor.prototype.generateStyles = function(styleID, styleNames) {
         let configs = styleNames.map(name => styleConfig.find(s => s.name === name)).filter(c => !!c);
+        let arr = [];
         let str = '';
-        let obj = {};
         for(let i = 0; i < configs.length; i++) {
-            let config = configs[i];
-            let tags = Object.prototype.toString.call(config.tag) === "[object Array]" ? config.tag : [config.tag];
-            let keyss = Object.prototype.toString.call(config.key) === "[object Array]" ? config.key : [config.key];
-            let valuess = Object.prototype.toString.call(config.value) === "[object Array]" ? config.value : [config.value];
-            for(let t = 0; t < tags.length; t++) {
-                let tag = tags[t];
-                let keys = Object.prototype.toString.call(keyss[t]) === "[object Array]" ? keyss[t] : [keyss[t]];
-                let values = Object.prototype.toString.call(valuess[t]) === "[object Array]" ? valuess[t] : [valuess[t]];
-                for(let k = 0; k < keys.length; k++) {
-                    let key = keys[k];
-                    let value = values[k];
-                    obj[tag] = obj[tag] || {};
-                    obj[tag][key] = value;
-                }
-            }
+            configs[i].tags.forEach(config => {
+                arr.push(this.generateStyleNode(config));
+            });
         }
-        str = this.generateStyle(styleID, obj);
-        return str;
-    };
-
-    Exportor.prototype.generateStyle = function(styleID, styleConfig) {
-        let str = '';
         str = _format(tmplStyleXML, {
             styleID: styleID,
-            styleNode: this.generateStyleNode(styleConfig)
-        })
+            styleNode: arr.join('')
+        });
         return str;
     };
 
     Exportor.prototype.generateStyleNode = function(styleConfig) {
         let str = '';
-        for(let k in styleConfig) {
-            str += _format(tmplStyleNodeXML, {
-                nodeName: k,
-                attributes: this.generateStyleNodeAttribute(styleConfig[k])
-            })
+        let attributes = [''];
+        let children = [];
+        let ctx = {};
+        if(!!styleConfig.tags) {
+            for(let i = 0; i < styleConfig.tags.length; i++) {
+                let c = styleConfig.tags[i];
+                children.push(this.generateStyleNode(c));
+            }
+        }
+        styleConfig.attributes = styleConfig.attributes || [];
+        for(let k in styleConfig.attributes) {
+            attributes.push(_format(tmplStyleNodeAttributeXML, {
+                attributeName: k,
+                attributeValue: styleConfig.attributes[k]
+            }));
         }
+        ctx.nodeName = styleConfig.tag;
+        ctx.attributes = attributes.join(' ');
+        ctx.children = children.join('');
+        str = _format(tmplStyleNodeXML, ctx);
         return str;
     };