浏览代码

excel-export/xlsx comparing

TonyKang 8 年之前
父节点
当前提交
279a8ff563

+ 34 - 1
modules/reports/controllers/rpt_controller.js

@@ -54,6 +54,39 @@ module.exports = {
             }
         );
     },
-    remove: function(req, res){
+    getDummyExcel: function(req, res) {
+        var nodeExcel = require('excel-export'); //关联excel-export模块
+        console.log("req.params.id:" + req.params.id);
+        console.log("req.params.pm:" + req.params.pm);
+        var conf = {};
+        conf.cols = [
+            {caption:'采购编号', type:'string'},
+            {caption:'合同名称', type:'string'},
+            {caption:'甲方', type:'string'},
+            {caption:'甲方部门', type:'string'},
+            {caption:'乙方', type:'string'},
+            {caption:'乙方部门', type:'string'},
+            {caption:'签订日期', type:'date'},
+            {caption:'中标日期', type:'date'},
+            {caption:'结束日期', type:'date'},
+            {caption:'销售负责人', type:'string'},
+            {caption:'商务负责人', type:'string'},
+            {caption:'业绩归属部门', type:'string'},
+            {caption:'金额', type:'number'},
+            {caption:'状态', type:'string'}
+        ];
+        var data = [{myId: 0, name: "dummyExcel", partyA: "", partyADept: "", partyB:"", partyBDept: "", signDate: "", beginDate:"", endDate:"", amount: 100, state:"dummy!"}]
+        var m_data = [];
+        var arry = [data[0].myId, data[0].name, data[0].partyA, data[0].partyADept, data[0].partyB, data[0].partyBDept, data[0].signDate, data[0].beginDate, data[0].endDate, "销售负责人", "商务负责人", "业绩归属部门", data[0].amount, data[0].state ];
+        m_data[0] = arry;
+        conf.rows = m_data;
+
+        var result = nodeExcel.execute(conf);
+        //console.log(result);
+        res.setHeader('Content-Type', 'application/vnd.openxmlformats');
+        res.setHeader("Content-Disposition", "attachment; filename=" +data[0].name+ ".xlsx");
+        res.end(result, 'binary');
+    },
+    dummyFunc: function(req, res){
     }
 };

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

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

+ 2 - 1
package.json

@@ -17,6 +17,7 @@
   },
   "dependencies": {
     "bluebird": "^3.5.0",
-    "excel-export": "^0.5.1"
+    "excel-export": "^0.5.1",
+    "xlsx": "^0.9.6"
   }
 }

+ 30 - 0
test/unit/excel_export/excelDummyDataOutput.js

@@ -0,0 +1,30 @@
+/**
+ * Created by Tony on 2017/3/28.
+ */
+
+var nodeExcel = require('excel-export'); //关联excel-export模块
+var conf = {};
+conf.cols = [
+    {caption:'采购编号', type:'string'},
+    {caption:'合同名称', type:'string'},
+    {caption:'甲方', type:'string'},
+    {caption:'甲方部门', type:'string'},
+    {caption:'乙方', type:'string'},
+    {caption:'乙方部门', type:'string'},
+    {caption:'签订日期', type:'date'},
+    {caption:'中标日期', type:'date'},
+    {caption:'结束日期', type:'date'},
+    {caption:'销售负责人', type:'string'},
+    {caption:'商务负责人', type:'string'},
+    {caption:'业绩归属部门', type:'string'},
+    {caption:'金额', type:'number'},
+    {caption:'状态', type:'string'}
+];
+var data = [{myId: 0, name: "dummy name", partyA: "", partyADept: "", partyB:"", partyBDept: "", signDate: "", beginDate:"", endDate:"", amount: 100, state:"dummy!"}]
+var m_data = [];
+var arry = [data[0].myId, data[0].name, data[0].partyA, data[0].partyADept, data[0].partyB, data[0].partyBDept, data[0].signDate, data[0].beginDate, data[0].endDate, "销售负责人", "商务负责人", "业绩归属部门", data[0].amount, data[0].state ];
+m_data[0] = arry;
+conf.rows = m_data;
+
+var result = nodeExcel.execute(conf);
+console.log(result);

+ 8 - 1
test/unit/others/rounding.js

@@ -8,6 +8,7 @@ test('test javascript rounding: ', function (t) {
     t.equal(Math.round(f1*10), 24);
     t.equal(Math.round(f2*10), 25);
     t.equal(Math.round(f3*10), 25);
+    t.equal(Math.round(f4*10), 25);
     t.end();
 });
 
@@ -17,4 +18,10 @@ test('test javascript fixed rounding: ', function (t) {
     t.equal(f2.toFixed(1), '2.5');
     t.equal(f3.toFixed(1), '2.5');
     t.end();
-});
+});
+
+//test('test javascript decimal: ', function (t) {
+//    var x = new Decimal(0.1), y = new Decimal(0.2);
+//    t.equal(x + y, 0.3);
+//    t.end();
+//});

+ 8 - 0
web/report/html/RptHome.html

@@ -31,6 +31,7 @@
                 <input type="button" value="< 前一页" onclick="showPage(-1)"/>
                 <input type="button" value="后一页 >" onclick="showPage(1)"/>
                 <input type="button" value="打印" onclick="printCurrentPage()"/>
+                <input type="button" value="Excel" onclick="getExcel()"/>
             </td>
         </tr>
     </table>
@@ -108,6 +109,13 @@
         }
     }
 
+    function getExcel() {
+        var id = "dummy_request_id";
+        var p1 = "dummy_paramm_2";
+        var url =  "/report_api/getExcel/" + id + "/" + p1;
+        window.location = url;//这里不能使用get方法跳转,否则下载不成功
+    }
+
     function ajaxCall() {
         if (setupReport()) {
             var canvas = document.getElementById("myCanvas");