Ext Grid数据导出到excel

目前,写了一个将Grid的数据,导出到EXCEL的简单应用,本文中的一些常量,来自我的前一篇:《JS中,操作EXCEL时,常用的常量定义》,本代码需要EXT2.1支持,以下是具体代码实现:

excel_app.js:

  1. /**
  2.  * 
  3.  * Microsoft Excel 应用,导出功能
  4.  * @author 张尧伟 [email protected]
  5.  */
  6. Ext.namespace("Ext.icss");
  7. Ext.icss.ExcelApp = function() {
  8.     return {
  9.         excelApp :  null,
  10.         EXCEL_APP : "Excel.Application",
  11.         /**
  12.          * 创建Excel应用程序对象
  13.          * @param visible{} true or false 表示主程序窗口对象是否可见
  14.          * @param displayAlerts{} true or false 如果不想在宏运行时被无穷无尽的提示和警告消息所困扰,
  15.          * 请将本属性设置为 False;这样每次出现需用户应答的消息时,Microsoft Excel 将选择默认应答。
  16.          * @return Application 对象
  17.          */
  18.         createApp : function(visible, displayAlerts) {
  19.             if (!this.excelApp) {
  20.                 this.excelApp = new ActiveXObject(this.EXCEL_APP);
  21.             }
  22.             if (this.excelApp) {
  23.                 this.excelApp.Visible = visible;
  24.                 this.excelApp.DisplayAlerts = displayAlerts;
  25.             }
  26.             return this.excelApp;
  27.         },
  28.         /**
  29.          * 关闭指定的应用程序对象
  30.          * @param exApp 需要关闭的EXCEL应用程序对象
  31.          */
  32.         closeApp : function(exApp) {
  33.             if (exApp) {
  34.                 exApp.Quit();
  35.             }
  36.         },
  37.         /**
  38.          * 在指定的应用程序对象中,创建一个新的工作簿
  39.          * @param exApp EXCEL应用程序对象
  40.          * @return Workbook 对象
  41.          */
  42.         addWorkbook : function(exApp) {
  43.             if (!exApp) {
  44.                 return;
  45.             }
  46.             var workbook = exApp.Workbooks.Add();
  47.             if (workbook) {
  48.                 workbook.Activate();
  49.             }
  50.             return workbook;
  51.         },
  52.         /**
  53.          * 在指定的workbook对象中,查找索引为index的工作表对象
  54.          * @param workbook Workbook 对象
  55.          * @param index 工作表的索引
  56.          * @return Worksheet 对象
  57.          */
  58.         findWorksheet : function(workbook, index) {
  59.             var worksheet = null;
  60.             if (index) {
  61.                 worksheet = workbook.Worksheets(index);
  62.             } else {
  63.                 worksheet = workbook.ActiveSheet;
  64.             }
  65.             return worksheet;
  66.         },
  67.         /**
  68.          * 设置指定的单元格值
  69.          * @param cell 单元格对象,取Worksheet.Cells属性 
  70.          * @param row cell的行
  71.          * @param col cell的列
  72.          * @param value 值
  73.          */
  74.         setCellValue:function(cell,row,col,value){
  75.             cell(row,col).Value = value;
  76.         },
  77.         /**
  78.          * 返回一个指定开始与结束表格的Range对象
  79.          * @param worksheet Worksheet对象
  80.          * @param startCell eg. cell(1,1)
  81.          * @param endCell eg. cell(10,1)
  82.          * @return Range对象
  83.          */
  84.         getRange:function(worksheet,startCell,endCell){
  85.             return worksheet.Range(startCell, endCell);
  86.         },
  87.         
  88.         printPreview : function() {
  89.             var visible = this.excelApp.Visible;
  90.             if (this.excelApp.ActiveSheet) {
  91.                 if (false == visible) {
  92.                     this.excelApp.Visible = true;
  93.                 }
  94.                 this.excelApp.ActiveSheet.PrintPreview();
  95.                 this.excelApp.Visible = visible;
  96.             }
  97.         },
  98.         printOut : function() {
  99.             if (this.excelApp.ActiveSheet) {
  100.                 this.excelApp.ActiveSheet.PrintOut();
  101.             }
  102.         },
  103.         close : function() {
  104.             this.closeApp(this.excelApp);
  105.             this.excelApp = null;
  106.         },
  107.         
  108.         getWorksheet:function(index){
  109.             if(!this.excelApp){
  110.                 this.createApp(true,false);
  111.             }
  112.             
  113.             var workbook = null;
  114.             if(!this.excelApp.ActiveWorkbook){
  115.                 workbook = this.addWorkbook(this.excelApp);
  116.             }
  117.             return this.findWorksheet(workbook,index);
  118.             
  119.         }
  120.     };
  121. }();
  122. ExcelApp = Ext.icss.ExcelApp;
  123. //excel.js:
    1. /**
    2.  * @include "../js/Ext2.1/icss/excel/excel_const.js"
    3.  * @include "../js/Ext2.1/icss/excel/excel_app.js"
    4.  */
    5. Ext.namespace("Ext.icss");
    6. /**
    7.  * 数据工具类的基类
    8.  * @author 张尧伟 [email protected]
    9.  * @param {} config
    10.  */
    11. Ext.icss.DataTool = function(config) {
    12.     config = config || {};
    13.     this.initialConfig = config;
    14.     Ext.icss.DataTool.superclass.constructor.call(this);
    15. };
    16. Ext.extend(Ext.icss.DataTool,Ext.util.Observable, {
    17. });
    18. /**
    19.  * 数据工具类,此类主要实现将表格数据导出到Microsoft Excel功能
    20.  * @param {} config
    21.  */
    22. Ext.icss.Data2ExcelTool = function(config) {
    23.     config = config || {};
    24.     this.excelApp = Ext.icss.ExcelApp;
    25.     Ext.icss.Data2ExcelTool.superclass.constructor.call(this);
    26. };
    27. Ext.extend(Ext.icss.Data2ExcelTool,Ext.icss.DataTool,{
    28.     
    29.     export2Excel:function(grid,filename){
    30.             this.excelApp.close();
    31.             var worksheet = this.excelApp.getWorksheet(1);
    32.             /*
    33.             var worksheet = {};
    34.             worksheet.Cells = {};
    35.             */
    36.             var cells = this.fillWorksheet(worksheet,grid,filename);
    37.     },
    38.     fillWorksheet : function(worksheet,grid,filename) {
    39.             var cells = worksheet.Cells;
    40.             
    41.             var cm = grid.getColumnModel();
    42.             var ds = grid.getStore();
    43.             
    44.             var cols = cm. getColumnCount();
    45.             var rows = ds.getCount();
    46.             
    47.             this.buildHeader(grid,cells,1,1);
    48.             this.buildSumRow(grid,cells,2,1);
    49.             this.buildBody(grid,cells,3,1);
    50.             
    51.             rows += 2;
    52.             
    53.             worksheet.Range(cells(1, 1), cells(rows,cols)).Borders.LineStyle = Excel.XlBorderWeight.xlHairline;
    54.             worksheet.Range(cells(2, 1), cells(2,cols)).Font.Bold = Excel.XlBoolean.True;
    55.             worksheet.Range(cells(1, 1), cells(rows,cols)).Columns.AutoFit();
    56.             
    57.             return cells;
    58.     },
    59.     /**
    60.      * 根据grid的cm,生成表格表头
    61.      * @param {} grid
    62.      * @param {} cells excel cell对象
    63.      * @param {} row 开始行
    64.      * @param {} col 开始列
    65.      */
    66.     
    67.     buildHeader:function(grid,cells,row,col){
    68.         var cm = grid.getColumnModel();
    69.         var cfg = null;
    70.         for(var i=0; i
    71.             cfg = cm.config[i];
    72.             if(cfg.id == 'checker')
    73.             {
    74.                 this.excelApp.setCellValue(cells,row,col+i,'');
    75.             }else{
    76.                 this.excelApp.setCellValue(cells,row,col+i,cfg.header);
    77.             }
    78.         }       
    79.     },
    80.     /**
    81.      * 根据GRID的store,导出数据
    82.      * @param {} grid
    83.      * @param {} cells
    84.      * @param {} row
    85.      * @param {} col
    86.      */
    87.     buildBody : function(grid, cells, row, col) {
    88.         var cm = grid.getColumnModel();
    89.         var ds = grid.getStore();
    90.         var cfg = null;
    91.         ds.each(function(rec) {
    92.             
    93.             for (var i = 0; i < cm.config.length; ++i) {
    94.                 cfg = cm.config[i];
    95.                 if (cfg.id == 'checker') {
    96.                     this.excelApp.setCellValue(cells, row, col + i, '');
    97.                 } else {
    98.                     var val = rec.get(cfg.dataIndex);
    99.                     if(cfg.renderer){
    100.                         val = cfg.renderer(val);
    101.                     }
    102.                     if(val){
    103.                         this.excelApp.setCellValue(cells, row, col + i, val );      
    104.                     }                   
    105.                     
    106.                 }
    107.             }
    108.             row++;
    109.         },this);
    110.     },
    111.     
    112.     /**
    113.      * 增加合计行
    114.      * @param {} grid
    115.      * @param {} cells
    116.      * @param {} row
    117.      * @param {} col
    118.      */
    119.     buildSumRow:function(grid,cells,row,col){       
    120.         var cm = grid.getColumnModel();
    121.         var cfg = null;
    122.         for (var i = 0; i < cm.config.length; ++i) {
    123.             cfg = cm.config[i];
    124.             if(cfg.sumcaption){
    125.                 this.excelApp.setCellValue(cells,row,col+i,cfg.sumcaption);
    126.             }
    127.             
    128.             if(cfg.sumvalue){
    129.                 this.excelApp.setCellValue(cells,row,col+i,cfg.sumvalue);
    130.             }
    131.         }
    132.     }
    133.     
    134. });

    调用示例:

    1. /**
    2.  * 将表格的数据导出到Excel文件
    3.  * @param {} btn
    4.  * @param {} event
    5.  */
    6. function export2Excel(btn,event)
    7. {
    8.     var filename = '预计划';
    9.     var data2Excel = new Ext.icss.Data2ExcelTool();
    10.     data2Excel.export2Excel(getGrid(),filename);
    11.     //var ExcelApp.export2Excel(getGrid(),filename);
    12. }

你可能感兴趣的:(js,ext,function,excel,include,工作,null)