日期:2014-05-17  浏览次数:20532 次

EXCEL导出至服务器指定位置
    新手刚接触.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();
        }


------解决方案--------------------
客户端操作文件保存到服务器指定位置。
项目中写的  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)//有文件才执行上传操作再保存到数据库
     &nb