下载excel文件应该在客户端计算机上 [英] DOWNLOAD of excel file should be on client machine

查看:127
本文介绍了下载excel文件应该在客户端计算机上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string path = "D:\\SYNC\\Reports.xls";
        FileInfo filexist = new FileInfo(path);
        if (filexist.Exists)
        {
            File.Delete(path);
        }
        else if (!Directory.Exists("D:\\SYNC\\"))
        {
            Directory.CreateDirectory("D:\\SYNC\\");
        }

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter hw = new HtmlTextWriter(sw))
            {
                bid.MainCompCode = Session["Comp"].ToString();
                bid.MainLocCode = Session["Loc"].ToString();
                bid.UserName = Session["Userid"].ToString();
                bid.Procedure = "gridfetch";
                bid.ScreenName = Request.QueryString["proc"].ToString();
                bid.Date4Filter = DateTime.Today.AddDays(-1).Date;
                adddatapage = Request.QueryString["page"].ToString();
                bid.CompCode = "";
                DataTable dt = new DataTable();
                dt = cn.fillgrid(bid);
                GridView gr = new GridView();
                gr.DataSource = dt;
                //gr.Columns[0].Visible = false;
                //gr.Columns[1].Visible = false;

                gr.DataBind();


                //Change the Header Row back to white color
               
                //Applying stlye to gridview header cells
                for (int i = 0; i < gr.HeaderRow.Cells.Count; i++)
                {
                    if (i != 0 && i != 1)
                    {
                        gr.HeaderRow.Style.Add("background-color", "#FFFFFF");
                        gr.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
                    }
                }
                int j = 1;
                //This loop is used to apply stlye to cells based on particular row
                foreach (GridViewRow gvrow in gr.Rows)
                {
                    //gvrow.BackColor = Color.White;
                    if (j <= gr.Rows.Count)
                    {
                        if (j % 2 != 0)
                        {
                            for (int k = 0; k < gvrow.Cells.Count; k++)
                            {
                                if (k != 0 && k != 1)
                                {
                                    gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
                                }
                            }
                        }
                    }
                    j++;
                }
                FileStream fs = new FileStream("D:\\SYNC\\Reports.xls",FileMode.OpenOrCreate,FileAccess.ReadWrite);

                StreamWriter writer = new StreamWriter(fs);
                gvCountry.RenderControl(hw);
                writer.WriteLine(sw.ToString());

                writer.Close();
            }
        }
        msg("File downloaded at D:\\SYNC\\Reports.xls");
        bindgrid();





我的应用程序在服务器ip上运行,当我下载数据时,它正在检查服务器本地文件夹下载,而不是客户端机器文件夹下载....



My application running on server ip, when i am downloading data, it is checking server local folder to download,instead of client machine folder for download....

推荐答案

出于安全原因,您无法从Web应用程序(从浏览器)访问客户端文件系统。只有Internet Explorer可以使用其ActiveX组件。除此之外,您可以使用基于Silgerlight / Adob​​e Flash的应用程序访问它,该应用程序在带有浏览器插件的沙箱中运行。您可以使用set some property将excel文件流写入http响应对象。然后浏览询问客户端需要存储的位置。客户端可以将客户端的文件系统路径和文件保存在那里。
For security reason you cannot access client file system from your web application(from browser). Only Internet Explorer can do it with its ActiveX component. Besides that You can access it with Silgerlight/Adobe flash based application which run in a sandbox with browser plugin. What you can do you write your excel file stream to http response object with set some property. Then browse ask the client where it need to store. Client can client his file system path and file will be saved there.


writer.Close();
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=" + PDF_Name + ".xls");
            Response.ContentType = "application/vnd.ms-excel";
            Response.BinaryWrite(msReport.ToArray());
            Response.End();



writer.close()

看看这一行

Response.BinaryWrite(msReport.ToArray());

msReport是内存流的对象,尝试将你的excel放入内存steram的对象,然后添加上面的代码,它将在客户端机器上下载excel。


Write this code after writer.close()
have a look at this line
Response.BinaryWrite(msReport.ToArray());
msReport is object of memory stream, try to get your excel into object of memory steram and then add above code which will download excel on client machine.


这篇关于下载excel文件应该在客户端计算机上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆