通过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. CentOS6.5 PHP基础环境搭建 [个人整理-亲测可用]

    ** * CentOS6.5 搭建基础PHP环境(yum安装) * http://www.aiplaypc.com/160.html **   #安装需要的包,有依赖关系,自动帮你解决 yum ins ...

  2. 武汉科技大学ACM :1010: 华科版C语言程序设计教程(第二版)例题7.8

    Problem Description 输入一个用年月日表示的日期,求该日期是该年的第几天.输入某年的第几天,输出这一天是该年的几月几号,茂茂解不出,需要你的帮助. Input 开始有个整数k,表示询 ...

  3. Jquery简略API使用

    都是个人随手笔记,既然开通了博客园就分享给大家.谨做为参考,具体大家自己测试以及使用 ★ $() ★ JQ的一个万能获取对象的函数(获取跟CSS获取元素是一样的)$(function(){}); 替代 ...

  4. C++第三课(2013.10.03 )

    函数的默认参数: 1.函数的默认参数必须放在形参的右边而且在默认形参的右边不能出现没有无默认参数的形参 2.如果函数的声明给出了默认的参数,那么函数的实现就不能定义默认参 3.声明成员函数时没有给出默 ...

  5. 兼容IE的渐变

    filter: progid:DXImageTransform.Microsoft.gradient(GradientType=, startColorstr=#1471da, endColorstr ...

  6. uC/OS 的任务调度解析 (转)

    uC/OS 的任务调度解析 1.任务调度器启动之后(初始化,主要是TCB的初始化),就可以创建任务,开始任务调度了,实际上第一个任务准确的说不是进行任务切换,而是进行启动当前最高优先级任务.uC/OS ...

  7. Okhttp3日志采集功能

    原文地址以示尊重:http://www.jianshu.com/p/d836271b1ae4 日志采集是一个APP必备的功能,可以方便开发人员快速定位问题,解决问题,那么我们在使用okhttp的时候应 ...

  8. SQL语句优化技术分析

    一.操作符优化 1.IN 操作符 用IN写出来的SQL的优点是比较容易写及清晰易懂,这比较适合现代软件开发的风格.但是用IN的SQL性能总是比较低的,从Oracle执行的步骤来分析用IN的SQL与不用 ...

  9. CoreGraphics 之CGAffineTransform仿射变换(3)

    CoreGraphics 之CGAffineTransform仿射变换(3)   CoreGraphics 的 仿射变换 可以用于 平移.旋转.缩放变换路径 或者图形上下文. (1)平移变换将路径或图 ...

  10. 安装Hadoop集群的最快的软件

    Quick Hadoop是一款安装Hadoop集群的桌面软件,只需要点两下鼠标,一分钟之内安装Hadoop到集群上,超快! 还在每台主机的Shell里一行一行地敲安装Hadoop的命令?别苦逼了! 用 ...