Coolite优化导出Excel文件实现代码

1。先来张图:

导出前数据:

Coolite优化导出Excel文件实现代码_第1张图片

导出结果:

Coolite优化导出Excel文件实现代码_第2张图片

设置列宽和屏蔽栏位:

Coolite优化导出Excel文件实现代码_第3张图片

结果2:

Coolite优化导出Excel文件实现代码_第4张图片

2.先把脚本文件定义了。

复制代码 代码如下:

//Copyright 2009 无忧[email protected]
var gridElse = {
getJsonToHidden: function(hidden, grid, format, title, fileName) {
hidden.setValue(this.getJsonDate(grid, format, title, fileName));
grid.submitData(true);
return true;
},
getJsonDate: function(grid, format, title, fileName) {
if (fileName == null || fileName == "") fileName = title;
var result = {
title: title,
format: format,
fileName: fileName,
dataCount: grid.store.reader.jsonData.length,
columns: '',
jsonDate: ''
};
//获取分组ID
var groupField;
if (typeof (grid.view.getGroupField) == "undefined")
{ groupField = false; }
else { groupField = grid.view.getGroupField(); }
//设置表头
var columns = this.getColumns(grid); //.getColumnModel().columns;
var columnCount = columns.length
for (var i = 0; i < columnCount; i++) {
if (columns[i].dataIndex != null && columns[i].dataIndex != "") {
fld = grid.store.fields.get(columns[i].dataIndex);
columns[i].recordFieldType = this.getRecordFieldType(fld);
}
if (groupField && groupField == columns[i].dataIndex)
columns[i].BGroup = true;
}
result.columns = Ext.encode(columns);
//返回数据
if (result.dataCount > 0 && result.dataCount <= 500) {
result.jsonDate = Ext.encode(grid.store.reader.jsonData);
}
else if (result.dataCount == null) result.dataCount = 0;
return Ext.encode(result);
},
getRecordFieldType: function(fld) {
if (fld == null) return "";
switch (fld.type) {
case "int": return "Int";
case "float": return "Float";
case "bool":
case "boolean": return "Boolean";
case "date": return "Date";
case "string": return "String";
default: return "Auto";
}
},
getColumns: function(grid) {
var columns = grid.getColumnModel().columns;
var columnCount = columns.length
for (var i = columnCount - 1; i >= 0; i--) {
if (columns[i].isColumnPlugin) columns.remove(columns[i]);
}
return columns;
}
};

3.调用方法:
复制代码 代码如下:

gridElse.getJsonToHidden(#{存储控件},#{GridPanel控件},'xls','标题','文件名');

4.aspx页面:
XXX.aspx
复制代码 代码如下:


......

......

......

......






5.cs代码:
XXX.aspx.cs
复制代码 代码如下:

protected void Sdate_SubmitData(object sender, StoreSubmitDataEventArgs e)
{
String json = HToFile.Value.ToString();
if (!String.IsNullOrEmpty(json))
{
ExportDate exportDate = JSON.Deserialize(json);
if (exportDate.dataCount > 0)
{
if (exportDate.Dates == null || exportDate.Dates.Length < exportDate.dataCount)
{
//如数据超过500条这重新查询数据导出
}
switch (exportDate.format)
{
case "xls":
GetToExcel(exportDate);
break;
case "pdf":
......
break;
}
}
}
}
public static void GetToExcel(ExportDate exportDate)
{
if (exportDate.Dates == null) { return; }
HttpContext context = HttpContext.Current;
if (context != null)
{
String rowid = "";
StringBuilder sb = new StringBuilder();
int columns = 0;
foreach (GridColumnInfo item in exportDate.GridColumnInfos)
{
if (!item.hidden || item.BGroup)
{
columns++;
}
}
#region 头部
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append(" ");
sb.Append(" 12.00");
sb.Append("
");
sb.Append(" ");
sb.Append(" ");
sb.Append("
");
sb.Append(" ");
sb.Append(" 11640");
sb.Append(" 19200");
sb.Append(" 0");
sb.Append(" 90");
sb.Append(" False");
sb.Append(" False");
sb.Append("
");
#region 样式
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("
");
#endregion
sb.AppendFormat("", exportDate.title);
sb.AppendFormat("", columns, exportDate.Dates.Length + 2);
#endregion
//表列宽度
int ColumnWidthsZ = 0;
foreach (GridColumnInfo item in exportDate.GridColumnInfos)
{
if (!item.hidden || item.BGroup)
{
ColumnWidthsZ += item.width;
sb.AppendFormat("", item.width);
}
}
//标题
sb.Append("");
sb.AppendFormat("", columns - 1);
sb.AppendFormat("{0}", exportDate.title);
sb.Append("
");
sb.Append("
");
//表头
sb.Append("");
foreach (GridColumnInfo item in exportDate.GridColumnInfos)
{
if (!item.hidden || item.BGroup)
{
sb.AppendFormat("{0}", item.header);
}
}
sb.Append("
");
//数据
int i = 0;
string cellClass = "";
foreach (Dictionary row in exportDate.Dates)
{
i++;
cellClass = ((i & 1) == 0) ? "odd" : "even";
sb.Append("");
foreach (GridColumnInfo item in exportDate.GridColumnInfos)
{
rowid = item.id;
if (string.IsNullOrEmpty(rowid)) rowid = item.dataIndex;
if (!String.IsNullOrEmpty(rowid) && (!item.hidden || item.BGroup) && row.ContainsKey(rowid))
{
sb.AppendFormat("{3}",
cellClass, exportDate.GetStyleID(item.recordFieldType), exportDate.GetDataType(item.recordFieldType), row[rowid]);
}
}
sb.Append("
");
}
#region 尾部
sb.Append("
");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("
");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("Blank");
sb.Append("1");
sb.Append("32767");
sb.Append("");
sb.Append("600");
sb.Append("
");
sb.Append("");
sb.Append("");
sb.Append("False");
sb.Append("False");
sb.Append("");
sb.Append("");
#endregion
context.Response.Clear();
if (context.Request.Browser.Browser != "IE")
context.Response.AppendHeader("Content-Disposition", String.Format("attachment; filename=\"{0}.xls\"", exportDate.fileName));
else context.Response.AppendHeader("Content-Disposition", String.Format("attachment; filename={0}.xls", System.Web.HttpUtility.UrlEncode(exportDate.fileName)));
context.Response.ContentType = "application/excel";
context.Response.Write(sb.ToString());
context.Response.End();
}
}

你可能感兴趣的:(Coolite优化导出Excel文件实现代码)