被上面催的急 新手求助大家 如何把gridview导到excel后存在服务器

l840828 2012-02-14 09:29:52
新手刚接触.net,现在需要把GridView的内容存在excel后保存在服务器指定位置,在网上找到了ToExcel函数,实现了将GridView的内容复制到excel,提示将excel保存到客户端,会有小框 【打开】【保存】【取消】,怎么能实现将excel保存在服务器项目目录呢? 改的代码有问题,新手求指教,求帮助啊。


private void ToExcel(Control ctl)
{
string FileName = "z.xls";
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName);
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
//到这里将gridview保存到excel表z.xls了, 执行HttpContext.Current.Response.End()后提示将excel保存到客户端,
//会有小框 【打开】【保存】【取消】,可是怎么将表存到服务器呢?我在后面加了SaveAs代码有问题啊!
HttpFileCollection files = HttpContext.Current.Request.Files;
System.Web.HttpPostedFile myFile = files[FileName];
string ppath = @"D:/excel/"+ FileName ;
myFile.SaveAs(ppath);//保存上载文件的内容。
// 这里报错未将对象引用设置到对象的实例。感觉是System.Web.HttpPostedFile myFile = files[FileName];这句的问题,怎么找到gridview保存后的excel文件z.xls保存在服务器上呢?
HttpContext.Current.Response.End();
}
...全文
216 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
这个要在

string FileName = "z.xls";
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName);
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);


这段代码里可以设置 具体的去Google吧
l840828 2012-02-15
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 net_lover 的回复:]
保存方法
string ppath = @"D:/excel/"+ FileName ;
System.IO.File.WriteAllText(ppath,tw.ToString());
HttpContext.Current.Response.Write(tw.ToString());
[/Quote]

感谢大家,net_lover兄的方法可以保存到服务器了,可是怎么让客户端不提示小框 【打开】【保存】【取消】呢,这个excel文件只要保存在服务器上就行了。
superliu1122 2012-02-15
  • 打赏
  • 举报
回复
string FileName = "z.xls";
ctl.Page.EnableViewState = false;
using(System.IO.StreamWriter tw = new System.IO.StreamWriter(FileName)){
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
}
SomethingJack 2012-02-15
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 net_lover 的回复:]

不是上传文件,不能用 HttpPostedFile
[/Quote]
- - 我也误解了。学习了
孟子E章 2012-02-15
  • 打赏
  • 举报
回复
不是上传文件,不能用 HttpPostedFile
孟子E章 2012-02-15
  • 打赏
  • 举报
回复
保存方法
string ppath = @"D:/excel/"+ FileName ;
System.IO.File.WriteAllText(ppath,tw.ToString());
HttpContext.Current.Response.Write(tw.ToString());
  • 打赏
  • 举报
回复
都跟你讲了 那个控件是不允许那样做的, 换个思路吧 。。。
l840828 2012-02-15
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 shulin85 的回复:]

直接保存不就行了?
确定需要保存的路径由权限,PostedFile.SaveAs(FilePath)
[/Quote]
保存路径设置了写入权限了,请问如何定义PostedFile呢
l840828 2012-02-15
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 hooyke 的回复:]

引用 8 楼 shulin85 的回复:
直接保存不就行了?
确定需要保存的路径由权限,PostedFile.SaveAs(FilePath)


你试试再说 这个控件不允许默认赋值的。。
[/Quote]
我试了,不赋值
System.Web.HttpPostedFile PostedFile;
PostedFile.SaveAs(FilePath);
报错 11 使用了未赋值的局部变量“PostedFile”
我一开始想的就是用SaveAs(FilePath)直接保存。
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 shulin85 的回复:]
直接保存不就行了?
确定需要保存的路径由权限,PostedFile.SaveAs(FilePath)
[/Quote]

你试试再说 这个控件不允许默认赋值的。。
l840828 2012-02-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ytmf007 的回复:]

是跟程序服务器是同一台?如果是那很简单啊
[/Quote]

是同一台,我也觉得思路很简单,但是新手不知道怎么实现,请指教
shulin85 2012-02-15
  • 打赏
  • 举报
回复
直接保存不就行了?
确定需要保存的路径由权限,PostedFile.SaveAs(FilePath)
  • 打赏
  • 举报
回复
有个变通的方式就是利用运行DOS 的方式 使用 net user 链接服务器共享的目录 然后把产生的文件拷贝过去。。
  • 打赏
  • 举报
回复
HttpFileCollection files = HttpContext.Current.Request.Files;
System.Web.HttpPostedFile myFile = files[FileName];
string ppath = @"D:/excel/"+ FileName ;
myFile.SaveAs(ppath);

我觉得这个控件因为安全考虑的问题不能直接赋值给它。。。
Teng_s2000 2012-02-15
  • 打赏
  • 举报
回复
1.有可能会涉及权限的问题,服务器的硬盘不可能随便访问吧
2.借助SQL语句,参考SQL to excel的方法,变通一下应该没问题了
那一抹嫣红 2012-02-15
  • 打赏
  • 举报
回复
是跟程序服务器是同一台?如果是那很简单啊
l840828 2012-02-15
  • 打赏
  • 举报
回复
麻烦各位大侠指教了
在途中 2012-02-15
  • 打赏
  • 举报
回复
参考:

//绑定数据到DataGrid1
this.DataGrid1.DataSource = SourceTb.DefaultView;
this.DataGrid1.DataBind();
//将DataGrid1构成的html代码写进StringWriter
this.DataGrid1.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
this.DataGrid1.RenderControl(hw);
string HtmlInfo = tw.ToString().Trim();

string DocFileName = "审批信息.xls";
string FilePathName = Request.PhysicalPath;
FilePathName = FilePathName.Substring(0,FilePathName.LastIndexOf("\\"));
//得到Excel文件的物理地址
FilePathName = FilePathName +"\\" + DocFileName;
System.IO.File.Delete(FilePathName);
FileStream Fs = new FileStream(FilePathName, FileMode.Create);
BinaryWriter BWriter= new BinaryWriter(Fs,System.Text.Encoding.GetEncoding("GB18030"));
//将DataGrid的信息写入Excel文件
BWriter.Write(HtmlInfo);
BWriter.Close();
Fs.Close();
l840828 2012-02-15
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 hooyke 的回复:]

这段代码里可以设置 具体的去Google吧

[/Quote]
google搜出来的都是使用COM组件实现的禁止弹出保存和覆盖的询问提示框,
//设置禁止弹出保存和覆盖的询问提示框
excel.DisplayAlerts =false;
excel.AlertBeforeOverwriting =false;

我是不需要保存在客户端的,也不需要在客户端打开,仅仅在服务器上保存就行了
l840828 2012-02-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 falconfei 的回复:]

如果只是单纯的保存,你可以考虑CSV格式的文件。
[/Quote]

是单纯保存,可是新人呀,gridviw保存成CVS文件更不会啦,而且现在是已经可以生成excel文件了,可是是在客户端提示保存,如何直接保存在服务器呢?
加载更多回复(1)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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