Browse Source

excel download ok

TonyKang 8 years ago
parent
commit
5b9835e53c

+ 42 - 0
modules/reports/controllers/rpt_controller.js

@@ -8,6 +8,8 @@ var TemplateData = require('../models/rpt_temp_data');
 var JpcEx = require('../rpt_component/JpcEx');
 //var cache = require('../../../public/cache/cacheUtil');
 var rptUtil = require("../util/rpt_util");
+var rpt_xl_util = require('../util/rpt_excel_util');
+var fs = require('fs');
 
 //统一回调函数
 var callback = function(req, res, err, data){
@@ -54,6 +56,46 @@ module.exports = {
             }
         );
     },
+    getExcel: function(req, res) {
+        var grp_id = req.params.id, tpl_id = req.params.pm, pageSize = req.params.size, rptName = req.params.rptName;
+        var rptTpl = null;
+        Template.getPromise(grp_id, tpl_id).then(function(rst) {
+            rptTpl = rst;
+            if (rptTpl) {
+                return TemplateData.getPromise(tpl_id);
+            } else {
+                callback(req, res, 'No report template was found!', null);
+            }
+        }).then(function(tplData){
+                if (tplData) {
+                    var printCom = JpcEx.createNew();
+                    rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
+                    var defProperties = rptUtil.getReportDefaultCache();
+                    printCom.initialize(rptTpl);
+                    printCom.analyzeData(rptTpl, tplData, defProperties);
+                    var maxPages = printCom.totalPages;
+                    var pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties);
+                    if (pageRst) {
+                        rpt_xl_util.exportExcel(pageRst, rptName, null, function(newName){
+                            res.setHeader('Content-Type', 'application/vnd.openxmlformats');
+                            res.setHeader("Content-Disposition", "attachment; filename=" + rptName + ".xlsx");
+                            var filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + newName + '.xlsx');
+                            filestream.on('data', function(chunk) {
+                                res.write(chunk);
+                            });
+                            filestream.on('end', function() {
+                                res.end();
+                            });
+                        });
+                    } else {
+                        callback(req, res, "Have errors while on going...", null);
+                    }
+                } else {
+                    callback(req, res, 'No report data were found!', null);
+                }
+            }
+        );
+    },
     getDummyExcel: function(req, res) {
         var nodeExcel = require('excel-export'); //关联excel-export模块
         console.log("req.params.id:" + req.params.id);

+ 2 - 1
modules/reports/routes/report_router.js

@@ -9,6 +9,7 @@ var reportController = require('./../controllers/rpt_controller');
 
 rptRouter.post('/getReport', reportController.getReportAllPages);
 //rptRouter.post('/getPreview', pmController.updateProjects);
-rptRouter.get('/getExcel/:id/:pm', reportController.getDummyExcel);
+rptRouter.get('/getDummyExcel/:id/:pm', reportController.getDummyExcel);
+rptRouter.get('/getExcel/:id/:pm/:size/:rptName', reportController.getExcel);
 
 module.exports = rptRouter;

+ 22 - 11
modules/reports/util/rpt_excel_util.js

@@ -202,7 +202,12 @@ function writeSharedString(sharedStrList){
         rst.push(dftHeadXml + '\r\n');
         rst.push('<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="' + sharedStrList.length + '" uniqueCount="' + sharedStrList.length + '">');
         for (var i = 0; i < sharedStrList.length; i++) {
-            rst.push('<si><t>' + sharedStrList[i] + '</t></si>');
+            //rst.push('<si><t>' + sharedStrList[i] + '</t></si>');
+            if (typeof sharedStrList[i] === 'string') {
+                rst.push('<si><t>' + sharedStrList[i].replace('|','\r\n') + '</t></si>');
+            } else {
+                rst.push('<si><t>' + sharedStrList[i] + '</t></si>');
+            }
         }
         rst.push('</sst>');
     }
@@ -610,7 +615,7 @@ function writeSheet(pageData, sheetData, sharedStrList, stylesObj){
 }
 
 module.exports = {
-    exportExcel: function (pageData, fName, options) {
+    exportExcel: function (pageData, fName, options, callback) {
         var rptOptions = (options || {singlePage: false, fileName: 'report'});
         var sheets = [];
         for (var i = 0; i < pageData.items.length; i++) {
@@ -663,14 +668,20 @@ module.exports = {
         data = writeStyles(stylesObj);
         zip_xl.file(file, data.join(''), {compression: 'DEFLATE'});
 
-        zip.generateNodeStream({type:'nodebuffer',streamFiles:true})
-            //.pipe(fs.createWriteStream('../../../tmp/outExcel.xlsx'))
-            .pipe(fs.createWriteStream('../../../tmp/' + fName + '.zip'))
-            .on('finish', function () {
-                // JSZip generates a readable stream with a "end" event,
-                // but is piped here in a writable stream which emits a "finish" event.
-                console.log("outExcel.xlsx was written.");
-            }
-        );
+        if (fName) {
+            var newName = '' + (new Date()).valueOf();
+            zip.generateNodeStream({type:'nodebuffer',streamFiles:true})
+                .pipe(fs.createWriteStream(__dirname.slice(0, __dirname.length - 21) + '/tmp/' + newName + '.xlsx'))
+                .on('finish', function () {
+                    // JSZip generates a readable stream with a "end" event,
+                    // but is piped here in a writable stream which emits a "finish" event.
+                    console.log(newName + ".xlsx was written.");
+                    if (callback) callback(newName);
+                }
+            );
+        } else {
+            //return zip.generateNodeStream({type:'nodebuffer',streamFiles:true});
+            return zip;
+        }
     }
 }

+ 16 - 2
web/report/html/RptHome.html

@@ -109,10 +109,24 @@
         }
     }
 
-    function getExcel() {
+    function getDummyExcel() {
         var id = "dummy_request_id";
         var p1 = "dummy_paramm_2";
-        var url =  "/report_api/getExcel/" + id + "/" + p1;
+        var url =  "/report_api/getDummyExcel/" + id + "/" + p1;
+        window.location = url;//这里不能使用get方法跳转,否则下载不成功
+    }
+
+    function getExcel() {
+        var rpt = document.getElementById("select_k1");
+        var size = document.getElementById("select_k2");
+        var rpt_name = rpt.options[rpt.selectedIndex].text;
+        if (rpt.selectedIndex >= 0 && size.selectedIndex >= 0) {
+            var strs = rpt.options[rpt.selectedIndex].value.split(';');
+            rpt_grp = strs[0];
+            rpt_id = strs[1];
+            rpt_size = size.options[size.selectedIndex].value;
+        }
+        var url =  "/report_api/getExcel/" + rpt_grp + "/" + rpt_id + "/" + rpt_size + "/" + rpt_name;
         window.location = url;//这里不能使用get方法跳转,否则下载不成功
     }