EXCEL导出至服务器指定位置

l840828 2012-02-12 06:44:22
新手刚接触.net,现在需要把GridView的内容存在excel后保存在服务器指定位置,在网上找到了ToExcel函数,实现了将GridView的内容复制到excel,可是是将excel保存到客户端,会有小框 【打开】【保存】【取消】,怎么能实现将excel保存在服务器指定位置?新手求指教。

private void ToExcel(Control ctl, string FileName)
{
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());
HttpContext.Current.Response.End();
}

...全文
302 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhang_00 2012-12-14
  • 打赏
  • 举报
回复
LZ这个问题有没有解决啊?我现在的问题和你的问题一样,想知道解决办法。
gdlpc 2012-02-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 l840828 的回复:]
引用 3 楼 gdlpc 的回复:

导出到服务器网站中EXCEL模板中就可以

不知具体怎么导入,新手请教,多谢多谢
[/Quote]以下是我用的,
//ds是查询到的
string serverpath = Server.MapPath("~/Excel/" + filename);
String strConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;IMEX=2'", serverpath);
OleDbConnection Excel_conn = new OleDbConnection(strConnectionString);
Excel_conn.Open();

for (int i = 0; i < ds.Tables["data_table"].Rows.Count; i++)//把查询到的数据插入模板中
{
string theme = ds.Tables["data_table"].Rows[i]["theme"].ToString();
theme = theme.Substring(theme.LastIndexOf("-") + 1);

string intostr = "insert into [Sheet1$] (F1,F2,F3,F4,F5)values(" + "'" + ds.Tables["data_table"].Rows[i]["name"].ToString() + "'" + "," + "'" + ds.Tables["data_table"].Rows[i]["teach_subject"].ToString() + "'" + "," + "'"
+ ds.Tables["data_table"].Rows[i]["class"].ToString() + "'" + "," + "'" + theme + "'" + "," + "'" + ds.Tables["data_table"].Rows[i]["send_time"].ToString() + "'" + ")";
OleDbCommand cm = new OleDbCommand(intostr, Excel_conn);
cm.ExecuteNonQuery();//插入数据
}
Excel_conn.Close();
Excel_conn.Dispose();
l840828 2012-02-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 gdlpc 的回复:]

导出到服务器网站中EXCEL模板中就可以
[/Quote]
不知具体怎么导入,新手请教,多谢多谢
l840828 2012-02-13
  • 打赏
  • 举报
回复

我只要把存有GridView的一个excel上传至服务器,没用for循环,可是改的代码不对头,System.Web.HttpPostedFile myFile = files[0];索引超出范围鸟。
private void ToExcel(Control ctl)
{
string dirpath = "D:\\....";//物理路径
string FileName = "z.xls";
string FileExtention = "";

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());

HttpFileCollection files = HttpContext.Current.Request.Files;
System.Web.HttpPostedFile myFile = files[0];
string NewName = DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + "";
FileExtention = System.IO.Path.GetExtension(myFile.FileName);
string ppath = dirpath + @"\" + NewName + FileExtention;
myFile.SaveAs(ppath);//保存上载文件的内容。

HttpContext.Current.Response.End();
}
l840828 2012-02-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wosizy 的回复:]

客户端操作文件保存到服务器指定位置。
项目中写的 LZ可以稍微改下!

[/Quote]
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了,可是怎么将表存到服务器呢?wosizy

HttpFileCollection files = HttpContext.Current.Request.Files;
System.Web.HttpPostedFile myFile = files[FileName];
string ppath = "D:\\"+ @"\" + "z.xls";
myFile.SaveAs(ppath);//保存上载文件的内容。
//这里报错未将对象引用设置到对象的实例。感觉是System.Web.HttpPostedFile myFile = files[FileName];这句的问题,怎么找到gridview保存后的excel文件z.xls上传呢?
glasseating 2012-02-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wosizy 的回复:]

客户端操作文件保存到服务器指定位置。
项目中写的 LZ可以稍微改下!

[/Quote]
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了,可是怎么将表存到服务器呢?wosizy

HttpFileCollection files = HttpContext.Current.Request.Files;
System.Web.HttpPostedFile myFile = files[FileName];
string ppath = "D:\\"+ @"\" + "z.xls";
myFile.SaveAs(ppath);//保存上载文件的内容。
//这里报错未将对象引用设置到对象的实例。感觉是System.Web.HttpPostedFile myFile = files[FileName];这句的问题,怎么找到gridview保存后的excel文件z.xls上传呢?
l840828 2012-02-13
  • 打赏
  • 举报
回复
感觉就是一个窗户纸没捅破啊
l840828 2012-02-13
  • 打赏
  • 举报
回复
新手请教各位
gdlpc 2012-02-12
  • 打赏
  • 举报
回复
导出到服务器网站中EXCEL模板中就可以
wosizy 2012-02-12
  • 打赏
  • 举报
回复
客户端操作文件保存到服务器指定位置。
项目中写的 LZ可以稍微改下!


string dirpath = "D:\\....";//物理路径
if (Directory.Exists(dirpath) == false)//确定给定路径是否引用磁盘上的现有目录。
{
Directory.CreateDirectory(dirpath);
}
Random ro = new Random();//随机数生成器
int name = 1;
for (int i = 0; i < files.Count; i++)
{
System.Web.HttpPostedFile myFile = files[i];
string FileName = "";
string FileExtention = "";
FileName = System.IO.Path.GetFileName(myFile.FileName);//返回指定路径字符串的文件名和扩展名。
string stro = ro.Next(100, 100000000).ToString() + name.ToString();//产生一个随机数用于新命名的文件
string NewName = DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + stro;
if (FileName.Length > 0)//有文件才执行上传操作再保存到数据库
{
FileExtention = System.IO.Path.GetExtension(myFile.FileName);
string ppath = dirpath + @"\" + NewName + FileExtention;
myFile.SaveAs(ppath);//保存上载文件的内容。
}
name = name + 1;//用来重命名规则的变量
}
}
l840828 2012-02-12
  • 打赏
  • 举报
回复
初学者请教各位

62,046

社区成员

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

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

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

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