日期:2014-05-16 浏览次数:20392 次
最近生成Excel表格,稍微得到一点新的体会,特此更新自己开发时候的版本。
开发工具:VS2005
数据库:oracle10.1
浏览器:firefox3.5.9
?
第一步,web层aspx文件 需要载入附件中的ExportGridToExcel.js(注释的地方可以改变表Excel表格最后的展示样式)。
载入方法:在<head></head>之间写入代码:
<!--导出excel表格脚本-->
<script type="text/javascript" src="../../../Js/Common/ExportGridToExcel.js"></script>
?
第二步,基本参数初始化,准备调用导出Excel表格函数ExportExcel(componentsGridPanel, config,"GetComponentsBillList");(这个方法大家可以自己定义参数)
//==导出Excel配置==//
var config = {
store: null,//因为后续可能需要处理分页,因此此处一般不直接传递GridPanel的数据源
title: '',//标题
checkId: '',
storageName: '',
beginTime: '',
endTime: '',
checker: ''
};
//盘点单基本信息设置
config.title = title;
config.checkId = checkId;
config.storageName = storageName;
config.beginTime = beginTime;
config.endTime = endTime;
config.checker = checker;
//导出Excel表格,生成盘点计划中的部件盘点单 公共函数 CheckWarehouseView.js文件中
//属性解释:gridPanel名,基本配置信息,store后台方法
ExportExcel(componentsGridPanel, config,"GetComponentsBillList");
?第三步,函数ExportExcel(gridPanel, config,operate)//原先有部分浏览器的判断,导致导出的文件中文名无法传递。去掉后,到目前没出现安全问题。
//==导出Execl表格==//
//gridPanel:当前grid,config:生成单配置,operate:后台url调用的方法
function ExportExcel(gridPanel, config,operate){
if (gridPanel) {
var tmpStore = gridPanel.getStore();
var tmpExportContent = '';
var tmpAllStore = new Ext.data.GroupingStore({//重新定义一个数据源
proxy: tmpStore.proxy,
reader: tmpStore.reader
});
tmpAllStore.load({//获取所有数据
params: {
secondLevelStorageId: secondLevelStorageId,
thirdLevelStorageId: thirdLevelStorageId,
operate: operate
}
});
tmpAllStore.on('load', function(store){
config.store = store;
tmpExportContent = gridPanel.getExcelXml(false, config);//此方法用到了一中的扩展
if (!Ext.fly('frmDummy')) {
var frm = document.createElement('form');
frm.id = 'frmDummy';
frm.name = id;
frm.className = 'x-hidden';
document.body.appendChild(frm);
}
Ext.Ajax.request({
url: '../../../Url/Common/ExportServicePage.aspx',//将生成的xml发送到服务器端
method: 'POST',
form: Ext.fly('frmDummy'),
callback: function(o, s, r){
//alert(r.responseText);
},
isUpload: true,
params: {
ExportContent: tmpExportContent,
ExportFile: config.storageName+"-"+config.title+ '.xls'
}
});
});
}
};
?
第四步,后台页面ExportServicePage.aspx代码(去掉编码处理,这样可以避免传到客户端的文件为乱码)
?
?
//===============================================================================
// Copyright (C) 2010 XXXX有限公司。版权所有。
//===============================================================================
//
// 文件名:ExportServicePage.aspx.cs
//
// 文件描述:导出Excel表格
//
//
// 创建人:truman
// 创建时间:2010年3月24日
//
// 修改人:
// 修改时间:
// 修改描述:
//===============================================================================
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace AfcMaintenanceSystem.Url.Common
{
public partial class ExportServicePage : System.Web.UI.Page
{