Web项目中导出数据的一个控件类
2008-01-29 16:03
323 查看
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
namespace WebHJControls
{
/// <summary>
/// Export 的摘要说明。
/// </summary>
public class WebExport: LinkButton
{
public WebExport()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
protected override void OnClick(EventArgs e)
{
ExportButton_Click();
}
/// <summary>
/// 扩展名,在设置导出文件类型的时候同时设置
/// </summary>
private string ExtensionType
{
get
{
if(ViewState["ExtensionType"] == null)
return ".xls";
return (string)ViewState["ExtensionType"];
}
set
{
ViewState["ExtensionType"] = value;
}
}
/// <summary>
/// 要导出内容的目标控件ID,如果目标控件的父亲控件不是Page,那么应该从Page下的该控件的根控件开始传入,格式是ParentControlID.ControlID.ControlID...
/// </summary>
public string TargetControlID
{
get
{
if(ViewState["TargetControlID"] == null)
return String.Empty;
return (string)ViewState["TargetControlID"];
}
set
{
ViewState["TargetControlID"] = value;
}
}
/// <summary>
/// 文件类型
/// </summary>
public ExportFileType FileType
{
get
{
if(ViewState["ExportFileType"] == null)
return ExportFileType.Excel;
return (ExportFileType)ViewState["ExportFileType"];
}
set
{
ViewState["ExportFileType"] = value;
switch(value)
{
case ExportFileType.Excel:
this.ExtensionType = ".xls";
break;
case ExportFileType.Word:
this.ExtensionType = ".doc";
break;
case ExportFileType.html:
this.ExtensionType = ".html";
break;
default:
this.ExtensionType = ".xls";
break;
}
}
}
/// <summary>
/// 导出的文件名
/// </summary>
public string ExportFileName
{
get
{
if(ViewState["ExportFileName"] == null)
return "ExportFile";
return (string)ViewState["ExportFileName"];
}
set
{
ViewState["ExportFileName"] = value;
}
}
protected override void Render(HtmlTextWriter writer)
{
if(Page != null)
{
Page.VerifyRenderingInServerForm(this);
}
this.CausesValidation = false;
base.Render (writer);
}
private void ExportButton_Click()
{
//确保找到控件
Control c = AnalyseControlID();
if(c == null)
return;
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.Buffer= true;
response.ContentType = SetContentType();
response.AddHeader("Content-Disposition", "attachment; filename=" + ExportFileName + ExtensionType + "");
response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
response.Charset = "gb2312";
EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
ClearControls(c);
c.RenderControl(oHtmlTextWriter);
response.Write(oStringWriter.ToString());
response.End();
}
private string SetContentType()
{
string contentType = String.Empty;
switch(FileType)
{
case ExportFileType.Excel:
contentType = "application/vnd.ms-excel";
break;
case ExportFileType.Word:
contentType = "application/vnd.ms-word";
break;
case ExportFileType.html:
contentType = "application/txt";
break;
}
return contentType;
}
private Control AnalyseControlID()
{
if(Page != null)
{
string[] controlIDArray = TargetControlID.Split('.');
Control c = Page.FindControl(controlIDArray[0]);
for(int i = 1;i < controlIDArray.Length;i++)
{
c = c.FindControl(controlIDArray[i]);
}
return c;
}
return null;
}
public bool ThumbnailCallback()
{
return false;
}
/// <summary>
/// 清除可能产生回发的子控件变成文本控件,如果不这样做的话,调用RenderControl会产生错误
/// Reference:http://www.c-sharpcorner.com/Code/2003/Sept/ExportASPNetDataGridToExcel.asp
/// </summary>
/// <param name="control"></param>
private void ClearControls(Control control)
{
if(control is System.Web.UI.WebControls.Image)
{
// string vpath = ((System.Web.UI.WebControls.Image)control).ImageUrl;
// Uri baseUri = new Uri(vpath);
// string apath =this.Context.Server.MapPath(baseUri.AbsolutePath);
// System.Drawing.Image img = System.Drawing.Image.FromFile(apath);
// System.Drawing.Image.GetThumbnailImageAbort cbl = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
// System.Drawing.Image newimg = img.GetThumbnailImage(100,150,cbl,IntPtr.Zero);
//newimg.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Gif);
}
if(control.ClientID.Equals("Photo"))
{
}
for (int i=control.Controls.Count -1; i>=0; i--)
{
ClearControls(control.Controls[i]);
}
if (!(control is TableCell))
{
if (control.GetType().GetProperty("SelectedItem") != null)
{
if(control.Parent != 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)
{
if(control.Parent != 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;
}
}
/// <summary>
/// 导出的文件类型
/// </summary>
public enum ExportFileType
{
Word = 1,
Excel = 2,
html = 3
}
}
相关文章推荐
- 一个导出CListCtrl控件数据到Excel文件的封装函数[r]
- 使用UltraWebGridExcelExporter控件导出分页Grid所有数据的简单解决办法
- C#Web项目中导出数据为Excel
- web项目实现Excel数据导入导出
- web项目导出CSV格式数据
- 控件中国网:WebGrid.NET Enterprise®,一个为ASP.NET平台下WEB开发而设计的高级数据表格控件。(最新版本发布)
- 发现了一个好用的WEB项目打印控件--四方打印
- 一个Asp.net MVC 控件项目分析---Telerik.Web.Mvc
- 关于Web项目里的给表单验证控件添加结束时间不得小于开始时间的验证方法,日期转换和前台显示格式之间,还有JSON取日期数据格式转换成标准日期格式的问题
- 一个Asp.net MVC 控件项目分析---Telerik.Web.Mvc 推荐
- 创建一个可以绑定数据的 ASP.NET Web 部件控件
- 一个Asp.net MVC 控件项目分析---Telerik.Web.Mvc
- 一个Asp.net MVC 控件项目分析---Telerik.Web.Mvc
- U6数据导出工具项目总结三 鼠标单击或者双击DataGridView控件时获取鼠标点击位置的值
- net控件中数据导到Excel的格式 首先,我们了解一下excel从web页面上导出的原理。当我们把这些数据发送到客户端时,我们想让客户端程序(浏览器)以excel的格式读取它,所以把mime类型设为:application/vnd.ms-excel,当excel读取文件时会以每个cell的格式呈现数据,如果cell没有规定的格式,则excel会以默认的格式去呈现该cell的数据。这样就给我们提供了自定义数据格式的空间,当然我们必须使用excel支持的格式。下面就列出常用的一些格式: 1) 文本
- 今天写的一个自动校验表单数据的js小控件[可扩展]
- 一个简单的java web 项目
- 一个Web报表项目的性能分析和优化实践(三) :提高Web应用服务器Tomcat的内存配置,并确认配置正确
- ETL控件学习之一从数据库导出数据到平面
- eclipse出现web项目导出war时没有jsp等文件的问题