ExtJS Grid导出数据到excel

/*******************导出数据到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(); }

 

你可能感兴趣的:(JavaScript)