2016-03-22 31 views
0

我創建了一個自定義報表,顯示每個故事在每個看板狀態中停留的總天數。我可以將報表中的數據導出到CSV。但是,下載的excel表單的名稱默認爲「download.xls」。如何重命名生成的Excel表。另外,當我打開excel文件時,它會發出警告,提示「download.xls文件格式和擴展名不匹配」。文件可能已損壞或不安全。除非您信任其源,否則不打開它。無論如何打開它?「。但是當我點擊「是」時,文件通常會打開顯示數據。我想擺脫這個警告。有什麼方法可以編碼,以免發生此警告。好心提醒。 代碼: 代碼來創建按鈕:重命名在拉力賽中生成的Excel表格

var button =Ext.create('Ext.Button', { 
     text  : 'Export', 
     renderTo : Ext.getBody(), 
      handler: function() { 
       that.onClickExport(); 
      } 
     } 

});

代碼來創建網格:

var grid = Ext.create('Ext.grid.Panel', { 
      store: mydata, 
      id: 'taskgrid', 
      columns: [ 
      { 
       text: 'ID', 
       dataIndex: 'FormattedID', 
       locked:true, 
      }, 
      { 
       text: 'Story Name', 
       dataIndex: 'Name', 
       locked:true, 
      }, 
      { 
      text: 'Defined', 
      dataIndex: 'def', 
      lockable: false, 
      }, 
      { 
      text: 'In Dev', 
      dataIndex: 'dev', 
      lockable: false, 
      }, 
      { 
      text: 'Completed', 
      dataIndex: 'comp', 
      lockable: false, 
      } 
     ], 
     height: 300, 
     width: 400, 
     viewConfig: { 
      stripeRows: true 
     } 

    }); 
    this.add(button); 
    this.add(grid); 

出口到CSV功能:

onClickExport: function() { 

if (/*@[email protected]*/0) { //Exporting to Excel not supported in IE 
    Ext.Msg.alert('Error', 'Exporting to CSV is not supported in Internet Explorer. Please switch to a different browser and try again.'); 
} else if (document.getElementById('taskgrid')) { 

    Ext.getBody().mask('Exporting the Report...'); 

    setTimeout(function() { 
     var template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-' + 
      'microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head>' + 
      '<!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>' + 
      '{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>' + 
      '</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}' + 
      '</table></body></html>'; 

     var base64 = function (s) { 
      return window.btoa(unescape(encodeURIComponent(s))); 
     }; 
     var format = function (s, c) { 
      return s.replace(/{(\w+)}/g, function (m, p) { 
       return c[p]; 
      }) 
     }; 
     var table = document.getElementById('taskgrid'); 
     console.log("table :", table); 
     var excel_data = '<tr>'; 
     Ext.Array.each(table.innerHTML.match(/<span .*?x-column-header-text.*?>.*?<\/span>/gm), function (column_header_span) { 
      excel_data += (column_header_span.replace(/span/g, 'td')); 
     }); 
     excel_data += '</tr>'; 
     console.log("excel data: ",excel_data); 
     Ext.Array.each(table.innerHTML.match(/<tr class="x-grid-row.*?<\/tr>/gm), function (line) { 
     alert(line); 
      excel_data += line.replace(/[^\011\012\015\040-\177]/g, '>>'); 
     alert(line); 
     }); 
     var ctx = {worksheet: name || 'Worksheet', table: excel_data}; 
     window.location.href = 'data:application/vnd.ms-excel;base64,' + base64(format(template, ctx)); 
     Ext.getBody().unmask(); 
    }, 500); 
}else{ 
console.log("taskgrid does not exist"); 
} 

} });

回答

0

對於重命名的Excel,你應該使用,而不是window.location.href = 'data:application/vnd.ms-excel;base64,' + base64(format(template, ctx));

使用下面的代碼下面的代碼:

var el = Ext.DomHelper.append(document.body, { 
      tag: "a", 
      download: '<yourFileName>.xls', 
      href: 'data:application/vnd.ms-excel;base64,' + base64(format(template, ctx)) 
}); 
el.click();  
Ext.fly(el).destroy(); 
+0

感謝。但無論如何要擺脫問題中提到的警告? @ankit chaudhary – Rana

+0

我也需要這個解決方案 –