ExtJS Gridはデータをexcelにエクスポートします.

1290 ワード

/*******************     excel************/
function exportExcel(grid) {
	var jsonStore = "";//       
	var head = "";//       
	var flag = true;//    ,         head(      )
	var cfg = null;
	var ds = grid.getStore();
	var cm = grid.getColumnModel();
	ds.each(function(record) {
				jsonStore += "&{";
				for (var i = 0; i < cm.config.length; i++) {
					cfg = cm.config[i];
					if (cfg.dataIndex && !cfg.hidden) {
						if (flag)
							head += "&{\"ename\":\"" + cfg.dataIndex + "\",\"cname\":\""
									+ cfg.header + "\",\"width\":\""
									+ (cfg.width || 20) + "\"}";
						var val = record.get(cfg.dataIndex);
						if (cfg.renderer) {
							val = cfg.renderer(val);
						}
						jsonStore += "\""+cfg.dataIndex + "\":\"" + val + "\",";
					}
				}
				jsonStore = jsonStore.substring(0, jsonStore.length - 1);
				jsonStore += "}";
				flag = false;
			});
	head = head.substring(1);
	jsonStore = jsonStore.substring(1);
	document
			.write("
");
document.write(");
document.write(");
document.write(");
document.formExcel.submit()
)