html5中文学习网

您的位置: 首页 > 网络编程 > ASP.NET » 正文

将控件中的数据输出保存到本地excel或word中,同时保存图片到本地(c#)_.NET教程_编程技术

[ ] 已经帮助:人解决问题
excel|word|控件|数据
//把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事件却可以激发事件,不知道大家有没有出现过这种问题?可以讨论一下,还是我保存图片的过程有问题?

6RsHTML5中文学习网 - HTML5先行者学习网
6RsHTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助