繁体中文
设为首页
加入收藏
当前位置:.Net技术首页 >> Asp.Net开发 >> 将控件中的数据输出保存到本地excel或word中,同时保存图片到本地(c#)

将控件中的数据输出保存到本地excel或word中,同时保存图片到本地(c#)

2007-08-15 08:00:00  作者:  来源:互联网  浏览次数:0  文字大小:【】【】【
简介://把table控件中的数据保存到Excel或word public void Save(System.Web.UI.Control source, DocumentType type) { Response.Clear(); Response.Buffer= true; //设置Http的头信息,编码格式 if (type == Document...

//把table控件中的数据保存到Excel或word

public void Save(System.Web.UI.Control source, DocumentType type)

{

Response.Clear();

Response.Buffer= true;

//设置Http的头信息,编码格式

if (type == DocumentType.Excel)

{

//Excel

Response.AppendHeader("Content-Disposition","attachment;filename=result.xls");

Response.ContentType = "application/ms-Excel";

}

else if (type == DocumentType.word)

{

//word

Response.AppendHeader("Content-Disposition","attachment;filename=result.doc");

Response.ContentType = "application/ms-word";

}

//设置编码

Response.Charset="GB2312";

Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");

//关闭控件的视图状态

source.EnableViewState =false;

//初始化HTMLWriter

System.IO.StringWriter writer = new System.IO.StringWriter() ;

System.Web.UI.HTMLTextWriter HTMLWriter = new System.Web.UI.HTMLTextWriter(writer);

source.RenderControl(HTMLWriter);

//输出

Response.Write(writer.ToString());

Response.End();

}

//以下是保存图片

public void SavePic()

{

string path = Server.MapPath(".") + @"\images\Chart.jpeg";

FileStream file = File.OpenRead(path);

byte[] content = new byte[file.Length];

file.Read(content,0,content.Length);

file.Close();

Response.Clear();

Response.AppendHeader("Content-Disposition","attachment;filename=Chart.jpeg");

Response.ContentType = "image/jpeg";//设置Http的头信息

Response.BinaryWrite(content);//输出

Response.End();

}

不过图片保存完后,页面上的DropDownList的Select事件不能促发,不晓得是什么缘故,而页面上的button事件却可以激发事件,不知道大家有没有出现过这种问题?可以讨论一下,还是我保存图片的过程有问题?

责任编辑:admin
相关文章