通过html给xls赋值,并下载xls文件

一、this.Response.Write(sw.ToString());System.IO.StringWriter sw = new System.IO.StringWriter();

this.Response.Clear();
            string strFileName;
            strFileName = "报表" + ".xls";
            Response.Buffer = true;
            Response.ContentType = "application/x-zip-compressed";
            Response.AddHeader("Accept-Language", "zh-cn");
            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpContext.Current.Server.UrlEncode(strFileName));

System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(sw);

this.gvMutiQueResult.AllowPaging = false;
            if (Session["QueryResult"] != null)
            {
                this.gvMutiQueResult.DataSource = (DataTable)Session["QueryResult"];
                this.gvMutiQueResult.DataBind();
            }
            else
            {
                BindGV(); 绑定数据
            }

this.gvMutiQueResult.RenderControl(htmlWrite);
            this.Response.Write(sw.ToString());
            this.Response.End();
            Response.Write("<script type='text/javascript'>window.history.back();</script>");

二、Response.Write(html2Excel);string html2Excel

string strHtml; string fileName;

Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(fileName) + ".xls\"");
            Response.ContentType = "application/ms-excel";

string html2Excel = "<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\"><head><meta content=\"text/html;charset=UTF-8\" http-epuiv=\"content-type\">"
                +
                 string.Format(@"<!--[if gte mso 9]><xml>
            <x:ExcelWorkbook>
                <x:ExcelWorksheets>
                    <x:ExcelWorksheet>
                        <x:Name>{0}</x:Name>
                        <x:WorksheetOptions>
                            <x:Print>
                                <x:ValidPrinterInfo />
                            </x:Print>
                        </x:WorksheetOptions>
                    </x:ExcelWorksheet>
                </x:ExcelWorksheets>
            </x:ExcelWorkbook>
        </xml>
        <![endif]-->", fileName)
                 + "</head><body>"
                + strHtml +
                "</body></html>";

Response.Write(html2Excel);
            Response.End();

三、

//获取数据
                IDataBase sDB = DBFactory.GetDBInstance();
                DataTable dt = sDB.GetDataTable("select * from 表 where ID='" + ID.ToString() + "'");
                //输出
                if (dt.Rows.Count > 0)
                {

string DocFileName = dt.Rows[0]["DocFileName"].ToString();
                    string DocSize = dt.Rows[0]["DocSize"].ToString();
                    byte[] DocContent = (byte[])dt.Rows[0]["DocContent"];

Response.Clear();
                    Response.ClearHeaders();
                    Response.Buffer = false;
                    //string ContentType = dt.Rows[0]["DocType"].ToString();
                    Response.ContentType = "application/octet-stream";
                    Response.AddHeader("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlEncode(DocFileName, System.Text.Encoding.UTF8) + "\"");
                    Response.AddHeader("Content-Length", DocSize.ToString());
                    Response.BinaryWrite(DocContent);
                    Response.Flush();
                    Response.End();

}

四、Response.WriteFile(DownloadFile.FullName); System.IO.FileInfo DownloadFile = new System.IO.FileInfo(strFileName);

//下载开机提醒安装程序 rar
        private void DownloadDocument()
        {
            try
            {
                string strFileName = Server.MapPath("../../help/BootAlert.rar");
                System.IO.FileInfo DownloadFile = new System.IO.FileInfo(strFileName);
                Response.Clear();
                Response.ClearHeaders();
                Response.Buffer = false;
                Response.ContentType = "application/ms-excel";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8));
                Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
                Response.WriteFile(DownloadFile.FullName);
                Response.Flush();
                Response.End();
            }
            catch (Exception err)
            {
                throw err;
            }
        }

五、 byte[] binaryContent  Response.BinaryWrite(binaryContent);

newFileName = newFileName.Replace("\\\\", "\\").Replace("\\", "/");
                string outputFileName = newFileName.Substring(newFileName.LastIndexOf('/') + 1);
                byte[] binaryContent = System.IO.File.ReadAllBytes(newFileName);
                Response.Clear();
                Response.ClearHeaders();
                Response.Buffer = false;
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlEncode(outputFileName, System.Text.Encoding.UTF8) + "\"");
                Response.AddHeader("Content-Length", binaryContent.Count().ToString());
                Response.BinaryWrite(binaryContent);
                Response.Flush();
                Response.End();

六、Response.TransmitFile(strFilePath, 0, fileSize);long fileSize = info.Length

FileInfo info = new FileInfo(strFilePath);
                    long fileSize = info.Length;
                    Response.Clear();
                    Response.Buffer = true;
                    Response.ContentType = "application/x-zip-compressed";
                    Response.AddHeader("Accept-Language", "zh-cn");
                    Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpContext.Current.Server.UrlEncode(info.Name));
                    //不指明Content-Length用Flush的话不会显示下载进度   
                    Response.AddHeader("Content-Length", fileSize.ToString());
                    Response.TransmitFile(strFilePath, 0, fileSize);
                    Response.Flush();
                    Response.Close();
                    info.Delete();

多种下载文件方式 Response.BinaryWrite(byte[] DocContent);Response.WriteFile(System.IO.FileInfo DownloadFile .FullName);Response.Write(string html2Excel);的更多相关文章

  1. 文件读写(一)利用File静态类 System.IO.FileInfo、DirectoryInfo、DriveInfo

    提供用于创建.复制.删除.移动和打开单一文件的静态方法,并协助创建 FileStream 对象. 一.读文件: 1.返回字符串:File.ReadAllText() string readText = ...

  2. MVC下载文件方式

    MVC下载文件方式 http://www.cnblogs.com/liang--liang/archive/2012/10/20/2732745.html 方式一: public FileStream ...

  3. MVC下载文件方式 包括网络地址文件

    MVC下载文件方式 方式一: public FileStreamResult DownFile(string filePath, string fileName){      string absol ...

  4. ASP.NET 下载文件方式

    protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新的方法TransmitFile来解决使 ...

  5. 向linux服务器上传下载文件方式收集

    向linux服务器上传下载文件方式收集 1. scp [优点]简单方便,安全可靠:支持限速参数[缺点]不支持排除目录[用法] scp就是secure copy,是用来进行远程文件拷贝的.数据传输使用 ...

  6. java中多种写文件方式的效率对比实验

    一.实验背景 最近在考虑一个问题:“如果快速地向文件中写入数据”,java提供了多种文件写入的方式,效率上各有异同,基本上可以分为如下三大类:字节流输出.字符流输出.内存文件映射输出.前两种又可以分为 ...

  7. C#下载文件,Stream 和 byte[] 之间的转换

    stream byte 等各类转换 http://www.cnblogs.com/warioland/archive/2012/03/06/2381355.html using (System.Net ...

  8. .net 直接输出远程文件到浏览器和下载文件保存到本机

    利用了xmlhttp,实现代码比较简单具体实现如下: 首先bin文件引入,com->microsoft xml v3.0 具体代码如下: protected void Button1_Click ...

  9. .net 下载文件几种方式

    方式一:TransmitFile实现下载.将指定的文件直接写入 HTTP 响应输出流,而不在内存中缓冲该文件. protected void Button1_Click(object sender, ...

随机推荐

  1. Linux下的静态IP配置【weber出品】

    配置Linux下的静态IP地址 因为服务器的IP地址是固定的,不像我们自己家的笔记本的IP是动态的.所以我们要将这个地址给写成静态的. 直接编辑这个这个配置文件即可: vi /etc/sysconfi ...

  2. Swift--集合类型 数组 字典 集合

    数组 1.创建一个数组 var someInts = [Int]()空数组 someInts = []清空 var threeDoubles = Array(repeating: 0.0, count ...

  3. ios中模拟延时的几种方法

    - (void)delay1 {     // 延迟执行不要用sleep,坏处:卡住当前线程     [NSThread sleepForTimeInterval:3];     NSLog(@&qu ...

  4. ArrayList 练习

    ArrayList list = new ArrayList(); Random rd = new Random(); ; i <; i++) { , ); //是否包含当前数字 if (!li ...

  5. (一)HTML5 - pushState 无刷新更新地址

    可以解决什么问题: 可以实现网页的ajax加载,同时又能完成URL的改变而没有网页跳转刷新的迹象,就像是改变了网页的hash(#)一样. 优于hash: 避免了改变hash的问题,避免了用户不理解UR ...

  6. 最近公共祖先:LCA及其用倍增实现 +POJ1986

    Q:为什么我在有些地方看到的是最小公共祖先? A:最小公共祖先是LCA(Least Common Ancestor)的英文直译,最小公共祖先与最近公共祖先只是叫法不同. Q:什么是最近公共祖先(LCA ...

  7. jQuery1.9(辅助函数)学习之——.serialize();

    $("form").serialize();  返回一个String 描述: 将用作提交的表单元素的值编译成字符串,这个方法不接受任何参数. .serialize(); 方法使用标 ...

  8. asp.net运行机制图

    (郑重提示:此图版权归广州传智播客老邹所有啊!!!!) 详细版

  9. Python 在Visual studio 中做单元测试进行TDD开发

    Unit Tests Steve Dower edited this page on 14 Jul · 3 revisions Pages 38 Home Azure Remote Debugging ...

  10. windows设备驱动安装指南

    高观点下的设备驱动安装(overview) 一.windows是怎样安装设备的? 第一步:新设备的识别 在给一个新设备安装驱动之前,总线或集线器(hub)驱动会为连接到PC上的设备分配一个硬件ID(h ...