0
A
回答
0
我提供建議
謝謝在ExtJS 4中爲網格編譯了一種插件。它可以幫助您製作可打印的版本和導出到Excel。它精確地輸出你看到的內容,並且它也適用於列渲染器。包括此腳本:
/**
* Export grid data. Based on:
* http://www.sencha.com/forum/showthread.php?125611-data-download-function-from-Grid-and-Chart
* http://www123.ddo.jp/grid/array-grid.js
* http://edspencer.net/2009/07/extuxprinter-printing-for-any-ext.html
* @param {Object} opt (optional)
* format: 'html',
* headers: true,
* stylesheetPath: 'css/print.css'
*/
Ext.grid.GridPanel.prototype.exportData = function(opt){
opt=opt||{};
//Get the array of columns from a grid
var me=this, columns=[], data=[];
Ext.each(me.columns, function(col) {
if (col.hidden != true && col.dataIndex) columns.push(col);
});
//Sometimes there's no colum header text (when using icons)
Ext.each(columns, function(column) {
if (!column.text || column.text == ' ') {
column.text=column.dataIndex;
}
});
//Build a useable array of store data for the XTemplate
me.store.data.each(function(item) {
var convertedData = {};
//apply renderers from column model
Ext.iterate(item.data, function(key, value) {
Ext.each(columns, function(column) {
if (column.dataIndex == key) {
if (column.renderer) {
if (column.xtype==='templatecolumn') {
convertedData[key] = column.renderer(value, {}, item);
} else {
convertedData[key] = column.renderer(value, undefined, undefined, undefined, columns.indexOf(column), undefined, me.view);
}
} else {
convertedData[key] = value;
}
if (typeof convertedData[key]==='string') {
convertedData[key]=Ext.util.Format.htmlToText(convertedData[key]);
}
return false;
}
});
});
data.push(convertedData);
});
//generate finale template to be applied with the data
var headings=[], body=[], str;
if (opt.format==="html") {
headings=opt.headers?new Ext.XTemplate(
'<tr>',
'<tpl for=".">',
'<th>{text}</th>',
'</tpl>',
'</tr>'
).apply(columns):'';
body=new Ext.XTemplate(
'<tr>',
'<tpl for=".">',
'<td>\{{dataIndex}\}</td>',
'</tpl>',
'</tr>'
).apply(columns);
var str=[
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
'<html>',
'<head>',
'<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />',
opt.stylesheetPath?'<link href="' + opt.stylesheetPath + '" rel="stylesheet" type="text/css" media="screen,print" />':'',
me.title?'<title>' + me.title + '</title>':'',
'</head>',
'<body>',
'<table>',
Ext.String.format('{0}\n<tpl for=".">{1}\n</tpl>', headings, body),
'</table>',
'</body>',
'</html>'
].join('\n');
} else {
Ext.each(columns, function(v) {
headings.push(Ext.util.Format.htmlToText(v.text));
body.push('{'+v.dataIndex+'}');
});
headings=opt.headers?headings.join('\t'):'';
body =body.join('\t');
var str=Ext.String.format('{0}\n<tpl for=".">{1}\n</tpl>', headings, body);
}
//console.log('toText', columns, data, headings, body, str);
return new Ext.XTemplate(str).apply(data);
};
現在,打印網格,像這樣:
var html=grid.exportData({
format: 'html',
headers: true,
stylesheetPath: 'resources/css/print.css'
});
var name = grid.getXType ? Ext.String.format("print_{0}_{1}", grid.getXType(), grid.id) : "print";
name=name.replace(/\W*/g, ''); //IE disallows spaces and other special characters in window name (the second argument). You need to remove them before passing as argument.
var win = window.open(undefined, name);
win.document.write(html);
win.document.close();
win.print();
到Excel的出口要求您網格導出到製表符分隔字符串。您發佈到服務器(的download.php)這串
var html=grid.exportData({
format: 'txt',
headers: true,
stylesheetPath: 'resources/css/print.css'
});
var form = Ext.DomHelper.append(document.body, {
tag: 'form',
style: 'display:none',
action: 'download.asp',
method: 'post',
cn:[{
tag:'textarea',
name:'body',
html:html
},{
tag:'input',
name:'filename',
value: grid.title||'download'
},{
tag:'input',
name:'extension',
value:'xls'
}]
});
form.submit();
document.body.removeChild(form);
隨後的download.php將需要回聲出貼體。使用:
Content-Disposition=attachment;filename=file.xls
Content-Type=application/vnd.ms-excel
現在,您的瀏覽器應該顯示提示,要求您保存xls文件。
0
我做了一個小的修改,以在庫Ext.ux.Exporter的代碼,改變此:
this.getEl().child('a', true).href = 'data:application/vnd.ms-excel;base64,' + Ext.ux.Exporter[config.exportFunction](this.component, null, config);
通過這樣的:
var domA = this.getEl().child('a', true),
string64 = Ext.ux.Exporter[config.exportFunction](this.component, null, config);
domA.href = '#';
domA.rel = string64;
this.getEl().child('a').addListener({
'click':{
fn: function(){
__exportExcel(this.getEl().child('a', true).rel);
},
scope: this
}
});
然後,使用 「髒」 的功能__exportExcel
__exportExcel = function(base64){
Ext.DomHelper.useDom = true;
var form = Ext.DomHelper.append('grid1', {
tag: 'form',
style: 'display:none',
action: 'download.php',
method: 'post',
cn:[{
tag:'textarea',
name:'body',
html: base64
},{
tag:'input',
name:'filename',
value: grid.title||'download'
},{
tag:'input',
name:'extension',
value:'xls'
}]
});
form.submit();
document.getElementById('grid1').removeChild(form);
return false;
}
發送base64字符串到php文件download.php
<?php
header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename=file.xls');
echo base64_decode($_POST['body']);
?>
這些更改可在IE8和其他瀏覽器中使用。
相關問題
- 1. ie8彈出窗口
- 2. ExtJS的Ext.ux.form.FileUploadField問題與IE8
- 3. Javascript在IE8中彈出窗口寬度?
- 4. 如何從彈出窗口在ExtJS 4
- 5. ExtJs在彈出窗口中跳轉
- 6. ExtJS Grid Scrollbar跳轉ie8
- 7. ie8中的彈出窗口閃爍
- 8. IE8中彈出窗口的問題
- 9. 如何在IE8中調試ExtJS
- 10. 爲什麼IE8阻止出現在IE7中的彈出窗口?
- 11. Extjs。點擊不在窗口關閉彈出窗口
- 12. IE8不刷新彈出窗口
- 13. Extjs國際化沒有出口項目?
- 14. 刷新父窗口在IE8
- 15. 什麼是視口在Extjs
- 16. Extjs - 在視口中拖動窗口
- 17. EXTJS Fileupload - IE8安全欄問題
- 18. IE8中的關閉窗口
- 19. 給孩子的窗口焦點在IE8
- 20. Extjs 3.x輸入類型文件中的文本字段重置在IE8(Extjs)中不起作用EXTJS * 3.2
- 21. EXTJS關閉窗口
- 22. ExtJs:銷燬窗口
- 23. 僅在IE8窗口大小調整後出現CSS效果
- 24. 在IE8彈出窗口中未加載JavaScript源文件
- 25. 在IE8中關閉子彈出窗口失敗
- 26. ExtJS的登錄窗口+軌
- 27. ExtJS的窗口等待
- 28. 帶extjs窗口的Mxgraph
- 29. ExtJS的視口不渲染
- 30. 關於在ExtJS的窗口部件
您使用的是什麼版本的extjs?你用什麼插件導出? –
可能有助於添加一小段代碼。 – Chewpers