源码网,源码论坛,源码之家,商业源码,游戏源码下载,discuz插件,棋牌源码下载,精品源码论坛

 找回密码
 立即注册
楼主: ttx9n

[ASP.NET] Coolite优化导出Excel文件实现代码

[复制链接]

7万

主题

861

回帖

32万

积分

论坛元老

Rank: 8Rank: 8

积分
329525
发表于 2010-3-2 12:53:37 | 显示全部楼层 |阅读模式
Coolite 优化 导出 Excel 文件。可以设定列宽和导出列选择。

1。先来张图:

导出前数据:

导出结果:

设置列宽和屏蔽栏位:

结果2:

2.先把脚本文件定义了。
复制代码 代码如下:
//Copyright 2009 无忧lwz0721@gmail.com
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.dataIndex != null && columns.dataIndex != "") {
fld = grid.store.fields.get(columns.dataIndex);
columns.recordFieldType = this.getRecordFieldType(fld);
}
if (groupField && groupField == columns.dataIndex)
columns.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.isColumnPlugin) columns.remove(columns);
}
return columns;
}
};

3.调用方法:
复制代码 代码如下:
gridElse.getJsonToHidden(#{存储控件},#{GridPanel控件},'xls','标题','文件名');

4.aspx页面:
XXX.aspx
复制代码 代码如下:
<ext:Hidden ID="HToFile" runat="server" />
......
<ext:Store ID="Sdate" runat="server" OnSubmitData="Sdate_SubmitData" >
......
</ext:Store>
......
<ism:GridPanel ID="GPData" runat="server" StoreID="Sdate">
......
<ext:Button ID="Button1" runat="server" Text="Submit">
<Listeners>
<Click Handler="gridElse.getJsonToHidden(#{HToFile},#{GPData},'xls','标题','文件名');" />
</Listeners>
</ext:Button>

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<ExportDate>(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("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sb.Append("<?mso-application progid=\"Excel.Sheet\"?>");
sb.Append("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\">");
sb.Append(" <DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">");
sb.Append(" <Version>12.00</Version>");
sb.Append(" </DocumentProperties>");
sb.Append(" <OfficeDocumentSettings xmlns=\"urn:schemas-microsoft-com:office:office\">");
sb.Append(" <RemovePersonalInformation/>");
sb.Append(" </OfficeDocumentSettings>");
sb.Append(" <ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">");
sb.Append(" <WindowHeight>11640</WindowHeight>");
sb.Append(" <WindowWidth>19200</WindowWidth>");
sb.Append(" <WindowTopX>0</WindowTopX>");
sb.Append(" <WindowTopY>90</WindowTopY>");
sb.Append(" <ProtectStructure>False</ProtectStructure>");
sb.Append(" <ProtectWindows>False</ProtectWindows>");
sb.Append(" </ExcelWorkbook>");
#region 样式
sb.Append("<Styles>");
sb.Append("<Style ss:ID=\"Default\">");
sb.Append("<Alignment ss:Vertical=\"Top\" ss:WrapText=\"1\" />");
sb.Append("<Font ss:FontName=\"宋体\" ss:Size=\"11\" />");
//sb.Append("<Borders>");
//sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Top\" />");
//sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Bottom\" />");
//sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Left\" />");
//sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Right\" />");
//sb.Append("</Borders>");
sb.Append("<Interior />");
sb.Append("<NumberFormat />");
sb.Append("<Protection />");
sb.Append("</Style>");
sb.Append("<Style ss:ID=\"title\">");
sb.Append("<Borders />");
sb.Append("<Font ss:Size=\"16\" ss:Bold=\"1\" />");
sb.Append("<Alignment ss:WrapText=\"1\" ss:Vertical=\"Center\" ss:Horizontal=\"Center\" />");
sb.Append("<NumberFormat ss:Format=\"@\" />");
sb.Append("</Style>");
sb.Append("<Style ss:ID=\"headercell\">");
sb.Append("<Font ss:Bold=\"1\" ss:Size=\"12\" />");
sb.Append("<Alignment ss:WrapText=\"1\" ss:Horizontal=\"Center\" />");
sb.Append("<Interior ss:Pattern=\"Solid\" ss:Color=\"#F2F2F2\" />");
sb.Append("<Borders>");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Top\" />");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Bottom\" />");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Left\" />");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Right\" />");
sb.Append("</Borders>");
sb.Append("</Style>");
sb.Append("<Style ss:ID=\"even\">");
sb.Append("<Interior ss:Pattern=\"Solid\" />");
sb.Append("<Borders>");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Top\" />");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Bottom\" />");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Left\" />");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Right\" />");
sb.Append("</Borders>");
sb.Append("</Style>");
sb.Append("<Style ss:Parent=\"even\" ss:ID=\"evendate\">");
sb.Append("<NumberFormat ss:Format=\"[ENG][$-409]dd-mmm-yyyy;@\" />");
sb.Append("<Borders>");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Top\" />");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Bottom\" />");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Left\" />");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Right\" />");
sb.Append("</Borders>");
sb.Append("</Style>");
sb.Append("<Style ss:Parent=\"even\" ss:ID=\"evenint\">");
sb.Append("<NumberFormat ss:Format=\"0\" />");
sb.Append("<Borders>");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Top\" />");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Bottom\" />");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Left\" />");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Right\" />");
sb.Append("</Borders>");
sb.Append("</Style>");
sb.Append("<Style ss:Parent=\"even\" ss:ID=\"evenfloat\">");
sb.Append("<NumberFormat ss:Format=\"0.00\" />");
sb.Append("<Borders>");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Top\" />");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Bottom\" />");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Left\" />");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Right\" />");
sb.Append("</Borders>");
sb.Append("</Style>");
sb.Append("<Style ss:ID=\"odd\">");
sb.Append("<Interior ss:Pattern=\"Solid\" />");
sb.Append("<Borders>");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Top\" />");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Bottom\" />");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Left\" />");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Right\" />");
sb.Append("</Borders>");
sb.Append("</Style>");
sb.Append("<Style ss:Parent=\"odd\" ss:ID=\"odddate\">");
sb.Append("<NumberFormat ss:Format=\"[ENG][$-409]dd-mmm-yyyy;@\" />");
sb.Append("<Borders>");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Top\" />");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Bottom\" />");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Left\" />");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Right\" />");
sb.Append("</Borders>");
sb.Append("</Style>");
sb.Append("<Style ss:Parent=\"odd\" ss:ID=\"oddint\">");
sb.Append("<NumberFormat ss:Format=\"0\" />");
sb.Append("<Borders>");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Top\" />");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Bottom\" />");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Left\" />");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Right\" />");
sb.Append("</Borders>");
sb.Append("</Style>");
sb.Append("<Style ss:Parent=\"odd\" ss:ID=\"oddfloat\">");
sb.Append("<NumberFormat ss:Format=\"0.00\" />");
sb.Append("<Borders>");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Top\" />");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Bottom\" />");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Left\" />");
sb.Append("<Border ss:Weight=\"1\" ss:LineStyle=\"Continuous\" ss:Position=\"Right\" />");
sb.Append("</Borders>");
sb.Append("</Style>");
sb.Append("</Styles>");
#endregion
sb.AppendFormat("<Worksheet ss:Name=\"{0}\">", exportDate.title);
sb.AppendFormat("<Table x:FullRows=\"1\" x:FullColumns=\"1\" ss:ExpandedColumnCount=\"{0}\" ss:ExpandedRowCount=\"{1}\">", 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("<Column ss:AutoFitWidth=\"1\" ss:Width=\"{0}\" />", item.width);
}
}
//标题
sb.Append("<Row ss:Height=\"28\">");
sb.AppendFormat("<Cell ss:StyleID=\"title\" ss:MergeAcross=\"{0}\">", columns - 1);
sb.AppendFormat("<Data ss:Type=\"String\">{0}</Data><NamedCell ss:Name=\"Print_Titles\" />", exportDate.title);
sb.Append("</Cell>");
sb.Append("</Row>");
//表头
sb.Append("<Row ss:AutoFitHeight=\"1\">");
foreach (GridColumnInfo item in exportDate.GridColumnInfos)
{
if (!item.hidden || item.BGroup)
{
sb.AppendFormat("<Cell ss:StyleID=\"headercell\"><Data ss:Type=\"String\">{0}</Data><NamedCell ss:Name=\"Print_Titles\" /></Cell>", item.header);
}
}
sb.Append("</Row>");
//数据
int i = 0;
string cellClass = "";
foreach (Dictionary<string, string> row in exportDate.Dates)
{
i++;
cellClass = ((i & 1) == 0) ? "odd" : "even";
sb.Append("<Row>");
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("<Cell ss:StyleID=\"{0}{1}\"><Data ss:Type=\"{2}\">{3}</Data></Cell>",
cellClass, exportDate.GetStyleID(item.recordFieldType), exportDate.GetDataType(item.recordFieldType), row[rowid]);
}
}
sb.Append("</Row>");
}
#region 尾部
sb.Append("</Table>");
sb.Append("<WorksheetOptions>");
sb.Append("<PageSetup>");
sb.Append("<Layout x:CenterHorizontal=\"1\" x:Orientation=\"Landscape\" />");
sb.Append("<Footer x:Data=\"Page &P of &N\" x:Margin=\"0.5\" />");
sb.Append("<PageMargins x:Top=\"0.5\" x:Right=\"0.5\" x:Left=\"0.5\" x:Bottom=\"0.8\" />");
sb.Append("</PageSetup>");
sb.Append("<FitToPage />");
sb.Append("<Print>");
sb.Append("<PrintErrors>Blank</PrintErrors>");
sb.Append("<FitWidth>1</FitWidth>");
sb.Append("<FitHeight>32767</FitHeight>");
sb.Append("<ValidPrinterInfo />");
sb.Append("<VerticalResolution>600</VerticalResolution>");
sb.Append("</Print>");
sb.Append("<Selected />");
sb.Append("<DoNotDisplayGridlines />");
sb.Append("<ProtectObjects>False</ProtectObjects>");
sb.Append("<ProtectScenarios>False</ProtectScenarios>");
sb.Append("</WorksheetOptions>");
sb.Append("</Worksheet></Workbook>");
#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();
}
}

回复

使用道具 举报

14

主题

1万

回帖

75

积分

注册会员

Rank: 2

积分
75
发表于 2022-9-12 16:17:24 | 显示全部楼层
谢谢楼主分享
回复 支持 反对

使用道具 举报

4

主题

1万

回帖

60

积分

注册会员

Rank: 2

积分
60
发表于 2022-10-10 18:47:15 | 显示全部楼层
哦哦哦ijhhsdj
回复 支持 反对

使用道具 举报

2

主题

2万

回帖

221

积分

中级会员

Rank: 3Rank: 3

积分
221
发表于 2022-10-16 21:25:44 | 显示全部楼层
感谢楼主分享
回复 支持 反对

使用道具 举报

8

主题

2万

回帖

52

积分

注册会员

Rank: 2

积分
52
发表于 2022-12-5 02:11:45 | 显示全部楼层
的vgdsvsdvdsvdsvds
回复 支持 反对

使用道具 举报

3

主题

2万

回帖

294

积分

中级会员

Rank: 3Rank: 3

积分
294
发表于 2023-11-23 02:15:47 | 显示全部楼层
66666666666666666666
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2024-6-9 22:53:27 | 显示全部楼层
来看看怎么样
回复 支持 反对

使用道具 举报

1

主题

2万

回帖

207

积分

中级会员

Rank: 3Rank: 3

积分
207
发表于 2024-9-5 22:19:53 | 显示全部楼层
啊啊啊啊啊啊啊啊啊啊啊啊啊啊
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2024-9-11 20:02:19 | 显示全部楼层
先把创新班才能下班才能下班
回复 支持 反对

使用道具 举报

5

主题

2万

回帖

183

积分

注册会员

Rank: 2

积分
183
发表于 2024-9-18 10:33:59 | 显示全部楼层
天天源码社区。。。。
回复 支持 反对

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|小黑屋|网站地图|源码论坛 ( 海外版 )

GMT+8, 2024-12-26 22:58 , Processed in 0.105274 second(s), 25 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表