重命名集会中生成的 excel sheet
Rename the excel sheet being generated in rally
我创建了一个自定义报告,显示每个故事在每个看板中停留的总天数 state.I 我能够将数据从报告导出到 CSV。但是下载的excelsheet的名字默认是"download.xls"。如何重命名生成的excelsheet。此外,当我打开 excel 文件时,它会发出警告,提示“"download.xls" 文件的文件格式和扩展名 match.The 文件可能已损坏或不安全。除非您信任其来源,否则请勿打开它。你想打开它吗?但是当我单击 "yes" 时,文件会正常打开并显示数据。我想摆脱这个警告。有什么办法可以编写代码,以免出现此警告。好心提醒。
代码:
创建按钮的代码:
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 (/*@cc_on!@*/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(/[^1250-7]/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");
}
}
});
要重命名 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();
我创建了一个自定义报告,显示每个故事在每个看板中停留的总天数 state.I 我能够将数据从报告导出到 CSV。但是下载的excelsheet的名字默认是"download.xls"。如何重命名生成的excelsheet。此外,当我打开 excel 文件时,它会发出警告,提示“"download.xls" 文件的文件格式和扩展名 match.The 文件可能已损坏或不安全。除非您信任其来源,否则请勿打开它。你想打开它吗?但是当我单击 "yes" 时,文件会正常打开并显示数据。我想摆脱这个警告。有什么办法可以编写代码,以免出现此警告。好心提醒。 代码: 创建按钮的代码:
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 (/*@cc_on!@*/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(/[^1250-7]/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");
}
} });
要重命名 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();