DataGrid 导出到 Excel 的帮助类
//
===============================================================================
//
// 从 DataGrid 或数据源中导出数据到 Excel 并提示下载的帮助类。
//
// Author: Neil Chen (木野狐)
// Date: 2005-1-27
// Version: 1.22
// History:
// v1.00 使用静态方法的形式实现该类,提供多种重载方式。
// v1.01 添加了对 DevExpress.Web.ASPxGrid.ASPxGrid 的直接导出支持。
// v1.20 改写为实体类。 减少了重复代码。
// v1.21 2005-2-1
// 修改了一个构造函数的重载形式中异常检测的代码。延迟到 Export() 方法。
// v1.22 2005-2-3
// 1. 修正了 Export() 方法中缺少 _titles != null 判断的 bug.
// 2. 修正了长的数字被 Excel 自动转换为科学计数法的毛病。
// (修改的办法来自 http://dotnet.aspx.cc )
//
// ===============================================================================
namespace RChen.Demos {
using System;
using System.IO;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Globalization;
using System.Collections;
using DevExpress.Web.ASPxGrid;
public class ExcelHelper {
#region Fields
string _fileName;
DataTable _dataSource;
string [] _titles = null ;
string [] _fields = null ;
int _maxRecords = 1000 ;
#endregion
#region Properties
/// <summary>
/// 限制输出到 Excel 的最大记录数。超出则抛出异常
/// </summary>
public int MaxRecords {
set { _maxRecords = value; }
get { return _maxRecords; }
}
/// <summary>
/// 输出到浏览器的 Excel 文件名
/// </summary>
public string FileName {
set { _fileName = value; }
get { return _fileName; }
}
#endregion
#region .ctor
/// <summary>
/// 构造函数
/// </summary>
/// <param name="titles"> 要输出到 Excel 的列标题的数组 </param>
/// <param name="fields"> 要输出到 Excel 的字段名称数组 </param>
/// <param name="dataSource"> 数据源 </param>
public ExcelHelper( string [] titles, string [] fields, DataTable dataSource): this (titles, dataSource) {
if (fields == null || fields.Length == 0 )
throw new ArgumentNullException( " fields " );
if (titles.Length != fields.Length)
throw new ArgumentException( " titles.Length != fields.Length " , " fields " );
_fields = fields;
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="titles"> 要输出到 Excel 的列标题的数组 </param>
/// <param name="dataSource"> 数据源 </param>
public ExcelHelper( string [] titles, DataTable dataSource): this (dataSource) {
if (titles == null || titles.Length == 0 )
throw new ArgumentNullException( " titles " );
// if (titles.Length != dataSource.Columns.Count)
// throw new ArgumentException("titles.Length != dataSource.Columns.Count", "dataSource");
_titles = titles;
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="dataSource"> 数据源 </param>
public ExcelHelper(DataTable dataSource) {
if (dataSource == null )
throw new ArgumentNullException( " dataSource " );
// maybe more checks needed here (IEnumerable, IList, IListSource, ) ???
// 很难判断,先简单的使用 DataTable
_dataSource = dataSource;
}
public ExcelHelper() {}
#endregion
#region public Methods
/// <summary>
/// 导出到 Excel 并提示下载
/// </summary>
/// <param name="dg"> DataGrid </param>
public void Export(DataGrid dg) {
if (dg == null )
throw new ArgumentNullException( " dg " );
if (dg.AllowPaging || dg.PageCount > 1 )
throw new ArgumentException( " paged DataGrid can't be exported. " , " dg " );
// 添加标题样式
dg.HeaderStyle.Font.Bold = true ;
dg.HeaderStyle.BackColor = System.Drawing.Color.LightGray;
RenderExcel(dg);
}
/// <summary>
/// 导出到 Excel 并提示下载
/// </summary>
/// <param name="xgrid"> ASPxGrid </param>
public void Export(DevExpress.Web.ASPxGrid.ASPxGrid xgrid) {
if (xgrid == null )
throw new ArgumentNullException( " xgrid " );
if (xgrid.PageCount > 1 )
throw new ArgumentException( " paged xgird not can't be exported. " , " xgrid " );
// 添加标题样式
xgrid.HeaderStyle.Font.Bold = true ;
xgrid.HeaderStyle.BackColor = System.Drawing.Color.LightGray;
RenderExcel(xgrid);
}
/// <summary>
/// 导出到 Excel 并提示下载
/// </summary>
public void Export() {
if (_dataSource == null )
throw new Exception( " 数据源尚未初始化 " );
if (_fields == null && _titles != null && _titles.Length != _dataSource.Columns.Count)
throw new Exception( " _titles.Length != _dataSource.Columns.Count " );
if (_dataSource.Rows.Count > _maxRecords)
throw new Exception( " 导出数据条数超过限制。请设置 MaxRecords 属性以定义导出的最多记录数。 " );
DataGrid dg = new DataGrid();
dg.DataSource = _dataSource;
if (_titles == null ) {
dg.AutoGenerateColumns = true ;
}
else {
dg.AutoGenerateColumns = false ;
int cnt = _titles.Length;
System.Web.UI.WebControls.BoundColumn col;
if (_fields == null ) {
for ( int i = 0 ; i < cnt; i ++ ) {
col = new System.Web.UI.WebControls.BoundColumn();
col.HeaderText = _titles[i];
col.DataField = _dataSource.Columns[i].ColumnName;
dg.Columns.Add(col);
}
}
else {
for ( int i = 0 ; i < cnt; i ++ ) {
col = new System.Web.UI.WebControls.BoundColumn();
col.HeaderText = _titles[i];
col.DataField = _fields[i];
dg.Columns.Add(col);
}
}
}
// 添加标题样式
dg.HeaderStyle.Font.Bold = true ;
dg.HeaderStyle.BackColor = System.Drawing.Color.LightGray;
dg.ItemDataBound += new DataGridItemEventHandler(DataGridItemDataBound);
dg.DataBind();
RenderExcel(dg);
}
#endregion
#region private Methods
private void RenderExcel(Control c) {
// 确保有一个合法的输出文件名
if (_fileName == null || _fileName == string .Empty || ! (_fileName.ToLower().EndsWith( " .xls " )))
_fileName = GetRandomFileName();
HttpResponse response = HttpContext.Current.Response;
response.Charset = " GB2312 " ;
response.ContentEncoding = Encoding.GetEncoding( " GB2312 " );
response.ContentType = " application/ms-excel/msword " ;
response.AppendHeader( " Content-Disposition " , " attachment;filename= " +
HttpUtility.UrlEncode(_fileName));
CultureInfo cult = new CultureInfo( " zh-CN " , true );
StringWriter sw = new StringWriter(cult);
HtmlTextWriter writer = new HtmlTextWriter(sw);
writer.WriteLine( " <meta http-equiv=\ " Content - Type\ " content=\ " text / html;charset = GB2312\ " > " );
DataGrid dg = c as DataGrid;
if (dg != null ) {
dg.RenderControl(writer);
}
else {
ASPxGrid xgrid = c as ASPxGrid;
if (xgrid != null )
xgrid.RenderControl(writer);
else
throw new ArgumentException( " only supports DataGrid or ASPxGrid. " , " c " );
}
c.Dispose();
response.Write(sw.ToString());
response.End();
}
/// <summary>
/// 得到一个随意的文件名
/// </summary>
/// <returns></returns>
private string GetRandomFileName() {
Random rnd = new Random(( int ) (DateTime.Now.Ticks));
string s = rnd.Next(Int32.MaxValue).ToString();
return DateTime.Now.ToShortDateString() + " _ " + s + " .xls " ;
}
private void DataGridItemDataBound( object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) {
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
e.Item.Attributes.Add( " style " , " vnd.ms-excel.numberformat:@ " );
// e.Item.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:¥#,###.00");
}
}
#endregion
}
}
//
// 从 DataGrid 或数据源中导出数据到 Excel 并提示下载的帮助类。
//
// Author: Neil Chen (木野狐)
// Date: 2005-1-27
// Version: 1.22
// History:
// v1.00 使用静态方法的形式实现该类,提供多种重载方式。
// v1.01 添加了对 DevExpress.Web.ASPxGrid.ASPxGrid 的直接导出支持。
// v1.20 改写为实体类。 减少了重复代码。
// v1.21 2005-2-1
// 修改了一个构造函数的重载形式中异常检测的代码。延迟到 Export() 方法。
// v1.22 2005-2-3
// 1. 修正了 Export() 方法中缺少 _titles != null 判断的 bug.
// 2. 修正了长的数字被 Excel 自动转换为科学计数法的毛病。
// (修改的办法来自 http://dotnet.aspx.cc )
//
// ===============================================================================
namespace RChen.Demos {
using System;
using System.IO;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Globalization;
using System.Collections;
using DevExpress.Web.ASPxGrid;
public class ExcelHelper {
#region Fields
string _fileName;
DataTable _dataSource;
string [] _titles = null ;
string [] _fields = null ;
int _maxRecords = 1000 ;
#endregion
#region Properties
/// <summary>
/// 限制输出到 Excel 的最大记录数。超出则抛出异常
/// </summary>
public int MaxRecords {
set { _maxRecords = value; }
get { return _maxRecords; }
}
/// <summary>
/// 输出到浏览器的 Excel 文件名
/// </summary>
public string FileName {
set { _fileName = value; }
get { return _fileName; }
}
#endregion
#region .ctor
/// <summary>
/// 构造函数
/// </summary>
/// <param name="titles"> 要输出到 Excel 的列标题的数组 </param>
/// <param name="fields"> 要输出到 Excel 的字段名称数组 </param>
/// <param name="dataSource"> 数据源 </param>
public ExcelHelper( string [] titles, string [] fields, DataTable dataSource): this (titles, dataSource) {
if (fields == null || fields.Length == 0 )
throw new ArgumentNullException( " fields " );
if (titles.Length != fields.Length)
throw new ArgumentException( " titles.Length != fields.Length " , " fields " );
_fields = fields;
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="titles"> 要输出到 Excel 的列标题的数组 </param>
/// <param name="dataSource"> 数据源 </param>
public ExcelHelper( string [] titles, DataTable dataSource): this (dataSource) {
if (titles == null || titles.Length == 0 )
throw new ArgumentNullException( " titles " );
// if (titles.Length != dataSource.Columns.Count)
// throw new ArgumentException("titles.Length != dataSource.Columns.Count", "dataSource");
_titles = titles;
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="dataSource"> 数据源 </param>
public ExcelHelper(DataTable dataSource) {
if (dataSource == null )
throw new ArgumentNullException( " dataSource " );
// maybe more checks needed here (IEnumerable, IList, IListSource, ) ???
// 很难判断,先简单的使用 DataTable
_dataSource = dataSource;
}
public ExcelHelper() {}
#endregion
#region public Methods
/// <summary>
/// 导出到 Excel 并提示下载
/// </summary>
/// <param name="dg"> DataGrid </param>
public void Export(DataGrid dg) {
if (dg == null )
throw new ArgumentNullException( " dg " );
if (dg.AllowPaging || dg.PageCount > 1 )
throw new ArgumentException( " paged DataGrid can't be exported. " , " dg " );
// 添加标题样式
dg.HeaderStyle.Font.Bold = true ;
dg.HeaderStyle.BackColor = System.Drawing.Color.LightGray;
RenderExcel(dg);
}
/// <summary>
/// 导出到 Excel 并提示下载
/// </summary>
/// <param name="xgrid"> ASPxGrid </param>
public void Export(DevExpress.Web.ASPxGrid.ASPxGrid xgrid) {
if (xgrid == null )
throw new ArgumentNullException( " xgrid " );
if (xgrid.PageCount > 1 )
throw new ArgumentException( " paged xgird not can't be exported. " , " xgrid " );
// 添加标题样式
xgrid.HeaderStyle.Font.Bold = true ;
xgrid.HeaderStyle.BackColor = System.Drawing.Color.LightGray;
RenderExcel(xgrid);
}
/// <summary>
/// 导出到 Excel 并提示下载
/// </summary>
public void Export() {
if (_dataSource == null )
throw new Exception( " 数据源尚未初始化 " );
if (_fields == null && _titles != null && _titles.Length != _dataSource.Columns.Count)
throw new Exception( " _titles.Length != _dataSource.Columns.Count " );
if (_dataSource.Rows.Count > _maxRecords)
throw new Exception( " 导出数据条数超过限制。请设置 MaxRecords 属性以定义导出的最多记录数。 " );
DataGrid dg = new DataGrid();
dg.DataSource = _dataSource;
if (_titles == null ) {
dg.AutoGenerateColumns = true ;
}
else {
dg.AutoGenerateColumns = false ;
int cnt = _titles.Length;
System.Web.UI.WebControls.BoundColumn col;
if (_fields == null ) {
for ( int i = 0 ; i < cnt; i ++ ) {
col = new System.Web.UI.WebControls.BoundColumn();
col.HeaderText = _titles[i];
col.DataField = _dataSource.Columns[i].ColumnName;
dg.Columns.Add(col);
}
}
else {
for ( int i = 0 ; i < cnt; i ++ ) {
col = new System.Web.UI.WebControls.BoundColumn();
col.HeaderText = _titles[i];
col.DataField = _fields[i];
dg.Columns.Add(col);
}
}
}
// 添加标题样式
dg.HeaderStyle.Font.Bold = true ;
dg.HeaderStyle.BackColor = System.Drawing.Color.LightGray;
dg.ItemDataBound += new DataGridItemEventHandler(DataGridItemDataBound);
dg.DataBind();
RenderExcel(dg);
}
#endregion
#region private Methods
private void RenderExcel(Control c) {
// 确保有一个合法的输出文件名
if (_fileName == null || _fileName == string .Empty || ! (_fileName.ToLower().EndsWith( " .xls " )))
_fileName = GetRandomFileName();
HttpResponse response = HttpContext.Current.Response;
response.Charset = " GB2312 " ;
response.ContentEncoding = Encoding.GetEncoding( " GB2312 " );
response.ContentType = " application/ms-excel/msword " ;
response.AppendHeader( " Content-Disposition " , " attachment;filename= " +
HttpUtility.UrlEncode(_fileName));
CultureInfo cult = new CultureInfo( " zh-CN " , true );
StringWriter sw = new StringWriter(cult);
HtmlTextWriter writer = new HtmlTextWriter(sw);
writer.WriteLine( " <meta http-equiv=\ " Content - Type\ " content=\ " text / html;charset = GB2312\ " > " );
DataGrid dg = c as DataGrid;
if (dg != null ) {
dg.RenderControl(writer);
}
else {
ASPxGrid xgrid = c as ASPxGrid;
if (xgrid != null )
xgrid.RenderControl(writer);
else
throw new ArgumentException( " only supports DataGrid or ASPxGrid. " , " c " );
}
c.Dispose();
response.Write(sw.ToString());
response.End();
}
/// <summary>
/// 得到一个随意的文件名
/// </summary>
/// <returns></returns>
private string GetRandomFileName() {
Random rnd = new Random(( int ) (DateTime.Now.Ticks));
string s = rnd.Next(Int32.MaxValue).ToString();
return DateTime.Now.ToShortDateString() + " _ " + s + " .xls " ;
}
private void DataGridItemDataBound( object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) {
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
e.Item.Attributes.Add( " style " , " vnd.ms-excel.numberformat:@ " );
// e.Item.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:¥#,###.00");
}
}
#endregion
}
}
0
0
0
(请您对文章做出评价)
posted on 2005-02-02 00:16 木野狐(Neil Chen) 阅读(10278) 评论(60) 编辑 收藏 网摘 所属分类: .NET
本文介绍了一个帮助类,能够将DataGrid或数据源中的数据导出至Excel,并提供了多种构造函数重载方式以适应不同需求。支持DevExpress.Web.ASPxGrid.ASPxGrid直接导出。
2933







评论
11834772005-02-02 09:03
呵呵,根本看不到你的代码呀
211.158.23.* 2005-02-02 09:36
hehe, expand the tree
[楼主] 2005-02-02 10:25
代码就是页面里这些了。不过忘记了写调用的例子,现在补上:
以下例子实际调用时需要在外面用 try 块捕获并处理异常。可能抛出的异常有 ParameterException, ParameterNullException, Exception 等。
203.207.189.* 2005-02-02 13:25
本方法的技术实质是另存HTML为EXCEL文件.如果需要套用一个EXCEL的标准格式,那又该如何实现呢?
61.171.96.* 2005-02-02 13:37
这个类有点小问题。
DataTable dt = ...;
ExcelHelper helper = new ExcelHelper(dt);
helper.MaxRecords = 2000;
helper.Export();
如果用这种方法
类中的
if (_fields == null && _titles.Length != _dataSource.Columns.Count)
throw new Exception("_titles.Length != _dataSource.Columns.Count");
由于_titles 设有初始化而就使用 Length 属性将会出现异常:
Object reference not set to an instance of an object.
应改为:
if (_fields == null && _titles!=null && _titles.Length != _dataSource.Columns.Count)
throw new Exception("_titles.Length != _dataSource.Columns.Count");
即可。
[楼主] 2005-02-02 18:02
to ezwyj,
要套用模板的话恐怕还是得使用 Excel 对象了。不过也可以考虑把样式附加到内部创建的那个 DataGrid 上去。
to deirlym:
非常感谢。的确存在这个问题。 这个判断原先是写在构造函数里的,后来我移到这个方法里时出现了一个疏漏。按你写的代码就对了。
[楼主] 2005-02-03 23:00
代码又做了更新。 解决了 DataGrid 中导出 Excel 时如果数字过大自动变为科学计数法的问题。
218.86.215.* 2005-03-04 11:20
using DevExpress.Web.ASPxGrid;
找不到DevExpress命名空间????
[楼主] 2005-03-04 17:17
to 天地归一,
这个是因为我在项目中用到这个第三方的 grid 控件,所以添加了对他的支持。你可以直接从这个类中把关于他的内容去掉即可。
211.103.36.* 2005-05-09 15:12
保存成功后,点击当前页的其它模块失效,这是什么原因?
怎么解决?
谁知道吗?请告诉之:
E-mail:hardy1011@163.com
218.206.194.* 2005-05-16 10:46
这是DELPHI2005吧,
我用的还是DELPHI 6,看不懂呀
61.144.60.* 2005-05-18 08:44
数据中有图片,还能导出吗
61.149.82.* 2005-05-19 16:21
真的很感谢木野狐,通过使用你的这种方法使我可以进行分页导出。非常非常感谢。
220.168.13.* 2005-05-29 19:30
非常感谢!:_)
172.16.30.* 2005-06-21 14:28
能不能用这种方法把DataTable 导入到一个有固定模板的Excel表中
比如说导入到有固定表头标题的Excel表中。但一定是DataTable中的
数据
[楼主] 2005-07-07 22:20
很多朋友都提到对 Excel 模版的支持问题,我觉得这也是一个很有意思的话题。下一个阶段我争取进行这方面的学习和补充,以完善这个助手类!
218.75.95.* 2005-08-08 17:32
比如,在线考试系统,把试卷题目的信息导出到EXCEL呢?
[楼主] 2005-08-09 10:38
@spring
我没有做过考试系统,请告诉我详细点的场景好吗?
60.26.185.* 2005-08-25 16:28
十分感谢木野狐,这个导出类程序帮了我很大忙,谢谢!现在有一个问题请教,就是当把此程序用于 windows XP 媒体中心版,IE 版本为 6.0.2900.2180.xpsp_sp2_rtm.040803-2158 作为客户端时以及XP sp2 版本下的 IE 当作客户端时会发生以下现象:在弹出的下载窗口点“打开”时一闪而过,点“取消”也如此,但是点“保存”可以保存,而且无论点哪个按钮,当前的IE窗口都会自动关闭!不知道是何原因?其他ie稍老一些的版本可以用没有问题!请木野狐帮忙查看一下!谢谢!!!
[楼主] 2005-08-29 00:59
@sunpop, 你好:
我想这个应该是 XP sp2 中对 IE 的默认设置有一些内容不能直接打开吧,你仔细查看一下 IE 里的相关安全设置看看。另外如果你有安装 3721 那最好把他卸掉。
因为我机器上没有 XP sp2 环境,所以暂时没法测试。非常抱歉。
218.4.73.* 2005-09-09 17:11
我现在在学习ASP.NET,很好的学习知识!谢谢楼主
221.15.172.* 2005-09-12 19:22
if (dg.AllowPaging || dg.PageCount > 1)
throw new ArgumentException("paged DataGrid can't be exported.", "dg");
这两个语句我觉得不好,因为实际中,DATAGRID分页的情况为大多数,如果分页了,而且页数大于1就抛出异常,那么这个这个类的功能就很弱了,我目前的做法是如果DATAGRID是分页的,则先取消分页,然后绑定一次(绑定函数如:BINDDATAGRID(),在PAGEBASE类中,所有WEBFORM PAGE都派生于PAGEBASE类,在需要导出DATAGRID的页面实现BINDDATAGRID的重载),然后导出,导出以后再进行分页,再绑定,通过这种方式导出,页面上有控件的时候必须先清除控件
呵呵,我只是大概看了一下,如果有别的地方对这个情况已经进行过处理,那当我废话啦!!
[楼主] 2005-09-23 02:11
@mzyqy:
的确做过处理的,你仔细看看:)
分页的 DataGrid 只有通过赋数据源属性到我这个帮助类里面才会起作用。
59.42.183.* 2005-10-26 19:49
类型“CheckBox”的控件“ASPxGrid1__ctl3__ctl3__ctl0_Checkbox1”必须放在具有 runat=server 的窗体标记内。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Web.HttpException: 类型“CheckBox”的控件“ASPxGrid1__ctl3__ctl3__ctl0_Checkbox1”必须放在具有 runat=server 的窗体标记内。
源错误:
行 248:
行 249: if (xgrid != null)
行 250: xgrid.RenderControl(writer);
行 251: else
行 252: throw new ArgumentException("only supports DataGrid or ASPxGrid.", "c");
我在对aspxgrid作导出的时候会出现这个问题,是在bind之后做的,请问木野狐,正确的做法应该是怎么样啊?
[楼主] 2006-01-13 14:09
傲宇,
因为给出的信息不太够,我没法判断你的程序是哪里出了错误,sorry.
你可以把程序 email 给我交流一下看。
chenrong2003[at]gmail[dot]com
2006-01-13 15:50
有VS2005的么,不能运行看效果
2006-01-13 15:53
还有 DevExpress.Web.ASPxGrid; 是什么命名空间?VS 2005中找不到。急啊,我正需要一个这样的功能:把datagridview导出到excel
[楼主] 2006-01-13 18:42
DevExpress.Web.ASPxGrid
是 ASPxGrid 这个控件的名称空间,你把关于它的东西去掉就可以了。
2005 下面要等我过些天有时间了改一下。
2006-02-25 13:36
感谢木野孤提供的代码。
希望多多交流
BLOG:
210.83.202.* 2006-03-15 11:39
木兄:
我导出ASPxGrid中的数据,用Excel打开后是代码,请问是怎么回事,
221.221.21.* 2006-05-22 16:36
我现在需要一个从asp.net的页面中把文件(excel,txt,dbf等)倒入到数据库中的功能!请各位高手指点!万分感谢!
221.221.21.* 2006-05-22 16:36
我现在需要一个从asp.net的页面中把文件(excel,txt,dbf等)倒入到数据库中的功能!请各位高手指点!万分感谢!
[楼主] 2006-08-10 11:31
@ahz
你好,excel 等需要首先上传到服务器,然后你可以读取后写 ADO.NET 代码来导入。
[楼主] 2006-08-10 11:32
@wangx
可能跟你页面所用的编码有关系,用 utf-8 还是 gb2312, 需要设置一致才行。
[楼主] 2006-08-10 11:33
@孤剑
谢谢鼓励
[楼主] 2006-08-10 13:31
"XXXX 必须放在具有 runat=server 的窗体标记内" 问题的解决办法是在调用页面内添加代码:
public override void VerifyRenderingInServerForm(Control control)
{
}
适用于 GridView.
222.66.55.* 2006-08-10 21:26
哪位大哥知道,下载破解VS 2005的DevExpress.Web.ASPxGrid控件
222.222.67.* 2006-09-29 10:46
话不多说了.谢过.
221.224.21.* 2007-01-10 13:43
问下啊,在导出DataGrid的时候,如果DataGrid里有模版列如:
<asp:TemplateColumn HeaderText="选择">
<ItemTemplate>
<asp:CheckBox id="checkbox_Choose" runat="server" checked='false'></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
就会报"checkbox必须放在具有 runat=server 的窗体标记内"错误,你上面介绍了GridView的解决方法,DataGrid有什么好的解决方法吗?
58.208.188.* 2007-01-10 20:15
已经知道了
[楼主] 2007-01-11 01:26
@火草
我想和 GridView 的解决办法是一样的吧?没有时间试验。
2007-02-07 11:53
不错
2007-02-22 21:32
挺好的。
202.118.70.* 2007-03-17 21:48
对于动态datagrid不管用阿。
222.137.187.* 2007-06-21 10:31
为什么点击到出的保存对话框时候,当前页面会关闭呢?
218.63.205.* 2007-06-21 20:18
我是个出学者,请问你能不能做一个完整的页给我。我看了你的类不知道怎么调用啊!!!!
58.35.91.* 2007-06-21 21:40
@尹真
我也遇到这样的问题,有高手能解答吗?
[楼主] 2007-06-22 11:39
@尹真
不能理解你所说的情况,我怎么没有遇到过?
60.21.155.* 2007-07-19 11:03
木老师,您的博客在哪?
我想跟您学习学习。可以告诉一下吗?我的邮箱是kane-second@163.com
220.163.12.* 2007-12-03 17:29
我用WEB在做这个的时候,有时导出EXECL时会导出整个网页,也就是在EXECL中出现了WEB的的控件,比如按钮之类的,导出TXT时出现了这个网页的HTML ,我改了Charset的值 还是出错 ,但有时又不出错。请问大家知道是哪里出了问题吗?
[楼主] 2007-12-05 13:12
@lgq
需要你提供详细的出错情况才知道。
2007-12-07 14:08
@木野狐(Neil Chen)
public static void DataGrid_Derives(DropDownList DDL,DataGrid dg, string filename)
{
System.Web.HttpResponse resp=System.Web.HttpContext.Current.Response;
System.Web.HttpServerUtility Serv=System.Web.HttpContext.Current.Server;
try
{
string theColumnSeparatorString = "";
string theFileType = "";
string theContentType = "";
switch(DDL.SelectedValue)
{
case "CSV":
theColumnSeparatorString = ",";
theFileType = "csv";
theContentType = "application/ms-excel";
break;
case "PDF":
theColumnSeparatorString = "\t";
theFileType = "pdf";
theContentType = "application/pdf";
resp.Write("<script language=javascript>alert('本功能正在实现中.....!');</script>");
break;
case "TXT":
theColumnSeparatorString = "\t";
theFileType = "txt";
theContentType = "application/ms-txt";
break;
default://EXCEL
theColumnSeparatorString = "\t";
theFileType = "xls";
theContentType = "application/ms-excel";
break;
}
resp.Clear();
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AddHeader("Content-Disposition", "attachment; filename=" + Serv.UrlEncode(filename+"."+ theFileType));
resp.Buffer= true;
resp.ContentType = "application/vnd.ms-excel";
resp.Charset = "";
dg.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
ClearControls(dg);
dg.RenderControl(oHtmlTextWriter);
resp.Write(oStringWriter.ToString());
resp.End();
}
catch
{
}
finally
{
}
}
private static void ClearControls(Control control)
{
for (int i=control.Controls.Count -1; i>=0; i--)
{
ClearControls(control.Controls[i]);
}
if (!(control is TableCell))
{
if (control.GetType().GetProperty("SelectedItem") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
try
{
literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control,null);
}
catch
{
}
control.Parent.Controls.Remove(control);
}
else
if (control.GetType().GetProperty("Text") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control,null);
control.Parent.Controls.Remove(control);
}
}
return;
}
EXECL功能可以实现 但是在 用TXT导出的时候却成了HTML .DropDownList用来选择导出格式的。
2007-12-07 14:11
有点错误 ,把resp.ContentType = "application/vnd.ms-excel"; 换成
resp.ContentType =theContentType 其实这句话好像无关紧要
2007-12-07 14:19
编译运行都没有出错 只是选择导出TXT是导出了网页部分的HTML,其中有GataGrid的HMRL代码。请问这是什么原因?我运行的是.NET 2003 WEB应用程序
218.94.84.* 2008-01-11 13:36
DataTable dt = ;
ExcelHelper helper = new ExcelHelper(dt);
// 最大导出条数(可省)
helper.MaxRecords = 2000;
helper.Export();
如何指定数据源呢?
我的datagrid中的数据源传不过去,请问可以给个例子吗?
[楼主] 2008-01-11 17:20
@tp
如果你的 DataGrid 的数据源是 DataTable, 可以:
DataTable table = grid.DataSource as DataTable;
58.211.105.* 2008-03-14 13:17
运行提示错误为"类型“DataGridLinkButton”的控件“dgdApprove__ctl2__ctl0”必须放在具有 runat=server 的窗体标记内。 "该怎么解决呢?谢谢!
[楼主] 2008-03-14 17:47
@girl1225
能提供一下你的测试代码吗,尽量简化了贴上来。
222.212.74.* 2008-05-05 15:33
大侠很了不起!!!谢谢啦