将控件中的数据输出保存到本地excel或word中,同时保存图片到本地(c#)
2004-09-20 19:37
603 查看
//把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 enum DocumentType
{
Word,
Excel
}
当datagrid分页的时候在保存数据时需要先从新创建一个不分页的对象然后重新绑定数据再保存数据否则分页控件中按钮由于不是客户端的控件而无法保存出错。
//以下是保存图片
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事件却可以激发事件,不知道大家有没有出现过这种问题?可以讨论一下,还是我保存图片的过程有问题?
补充:在下载附件到客户端的时候 由于页面采用了多个frame的情况所以下载完后会使得dropdownlist等控件无法激发事件 会出现网页上有错误的提示 但是button却可以 可能是由于多个网页组合在一起受到干扰,目前还没有解决办法,所以如果使用到dropdownlist等控件的话只能在单个页面中(即页面不是frame的组合框)下载附件
# 回复:将控件中的数据输出保存到本地excel或word中,同时保存图片到本地(c#) 2004-09-16 11:02 AM 木子
把attachment改为online即可在线打开word excel等。
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 enum DocumentType
{
Word,
Excel
}
当datagrid分页的时候在保存数据时需要先从新创建一个不分页的对象然后重新绑定数据再保存数据否则分页控件中按钮由于不是客户端的控件而无法保存出错。
//以下是保存图片
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事件却可以激发事件,不知道大家有没有出现过这种问题?可以讨论一下,还是我保存图片的过程有问题?
评论
# 回复:将控件中的数据输出保存到本地excel或word中,同时保存图片到本地(c#) 2004-09-16 10:01 AM 木子补充:在下载附件到客户端的时候 由于页面采用了多个frame的情况所以下载完后会使得dropdownlist等控件无法激发事件 会出现网页上有错误的提示 但是button却可以 可能是由于多个网页组合在一起受到干扰,目前还没有解决办法,所以如果使用到dropdownlist等控件的话只能在单个页面中(即页面不是frame的组合框)下载附件
# 回复:将控件中的数据输出保存到本地excel或word中,同时保存图片到本地(c#) 2004-09-16 11:02 AM 木子
把attachment改为online即可在线打开word excel等。
相关文章推荐
- 将控件中的数据输出保存到本地excel或word中,同时保存图片到本地(c#)
- 将控件中的数据输出保存到本地excel或word中,同时保存图片到本地(c#)
- 将控件中的数据输出保存到本地excel或word中,同时保存图片到本地(c#)
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- 扩展GridView控件(8) - 导出数据源的数据为Excel、Word或Text
- Android实现拍照,以及从相册选择图片裁剪功能同时保存在本地
- 关于实现接收base64图片数据并以图片保存到本地的处理
- java project实现ArrayList数据到写入保存到本地Excel
- 保存图片控件上的图片到本地 出现错误:无法将类型为“System.Windows.Media.Imaging.BitmapFrameDecode”的对象强制转换为类型“System.Windows.Media.Imaging.BitmapImage”。
- 将图片保存到数据库,由数据库输出图片为临时文件给Image 控件用
- 如何使用免费控件将word表格中的数据导入到excel中
- ASP.NET中将数据输出到WORD、EXCEL、TXT、HTM
- .NET编程技术——实验08:利用GDI+技术生成简单验证码,类似excel的数据折线图、并保存为图片
- PHPExcel读取EXCEL中的图片并保存到本地的方法
- Repeater控件中的数据导出为Excel或Word文件
- 如何使用免费控件将Word表格中的数据导入到Excel中
- 将网页数据输出为Excel或Word文件
- ASP.NET中将数据输出到WORD、EXCEL、TXT、HTM
- ASP.NET中将数据输出到Excel,TXT, Word, Html