将DataGrid选中的数据写入Excel并直接保存到本地的问题(有特别注意的地方,大家进来看看。)

YrA5lala 2004-12-27 11:49:11
下边是一段大家都很熟悉的将DataGrid的数据写入Excel文件的代码

请问,怎么样实现让他自动保存(如果指定的Excel文件已经存在,就直接遮盖)。
就是说,整个事件执行后(chick触发)。用户什么都不做,就完成所有操作?
(谁能告诉我,每次执行这个事件的时候都只是把数据写入excel,并将原来excel的数据完全遮盖啊?)
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312";
Response.AppendHeader("Content-Disposition","attachment;filename=FileName.xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.DataGrid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
...全文
159 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lionelyao 2005-02-28
  • 打赏
  • 举报
回复
asp.net我不是很熟悉,但是要求不出现保存对话框直接保存excel可以这样做:

excel.ActiveWorkbook.SaveCopyAs(path);//excel为Excel.Application,path为文件路径及文件名如"c:\\1.xls"

希望对你有用
slon3dmax 2005-02-28
  • 打赏
  • 举报
回复
public void ToExcel(System.Web.UI.Control ctl)
{
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
HttpContext.Current.Response.Charset ="UTF-8";
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType ="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctl.Page.EnableViewState =false;
System.IO.StringWriter tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
YrA5lala 2004-12-28
  • 打赏
  • 举报
回复
有人帮忙吗?
zr1982930 2004-12-28
  • 打赏
  • 举报
回复
倒,不就是多点几下嘛!这都闲麻烦!你的客户要求也太过分了吧!
YrA5lala 2004-12-28
  • 打赏
  • 举报
回复
呵呵~~是我自己觉得麻烦~~

我想让程序实现自动保存的啦!
i三千 2004-12-27
  • 打赏
  • 举报
回复
up

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧