博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FileUtils 文件下载 文件导出
阅读量:4364 次
发布时间:2019-06-07

本文共 2471 字,大约阅读时间需要 8 分钟。

public class FileUtils    {        ///         /// 文件下载        ///         /// 页面参数        /// 文件源路径        /// 文件命名        public static void FileDownload(System.Web.UI.Page page, string filePath, string saveFileName)        {            try            {                if (!string.IsNullOrEmpty(filePath))                {                    string fileExtension = filePath.Substring(filePath.LastIndexOf('.'));//后缀                    //saveFileName = filePath.Substring(filePath.LastIndexOf(@"\"));//文件名                    page.Response.Clear();                    page.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(saveFileName)));                    page.Response.Charset = "utf-8";                    page.Response.ContentEncoding = System.Text.Encoding.Default;                    page.Response.WriteFile(filePath);                    //page.Response.Flush();                    page.Response.End();                   //HttpContext.Current.ApplicationInstance.CompleteRequest();                }            }            catch {   }        }        ///         /// 文件导出        ///         /// 对象页面        /// 文件保存服务器路径        /// 数据源        /// 导出后的文件名称        public static void ExportFile(System.Web.UI.Page page, string filePath, DataTable dt, string exportFileName)        {            try            {                if (File.Exists(filePath))                {                    File.Delete(filePath);                }                StreamWriter sw = new StreamWriter(new FileStream(filePath, FileMode.CreateNew), System.Text.Encoding.GetEncoding("GB2312"));                sw.Write(exportFileName);                sw.WriteLine();                int i = 0;                for (i = 0; i <= dt.Columns.Count - 1; i++)                {                    sw.Write(dt.Columns[i].ColumnName);                    sw.Write('\t');                }                sw.WriteLine();                foreach (DataRow dr in dt.Rows)                {                    for (i = 0; i <= dt.Columns.Count - 1; i++)                    {                        sw.Write(dr[i].ToString());                        sw.Write('\t');                    }                    sw.WriteLine();                }                sw.Close();                FileDownload(page, filePath, exportFileName);            }            catch { }        }    }

 

转载于:https://www.cnblogs.com/dragon-L/p/3777221.html

你可能感兴趣的文章
了解你的Linux系统:必须掌握的20个命令
查看>>
js setInterval 启用&停止
查看>>
knockoutJS学习笔记04:监控属性
查看>>
Linux下启动/关闭Oracle
查看>>
session和cookie的区别
查看>>
alert弹出窗口,点击确认后关闭页面
查看>>
oracle问题之数据库恢复(三)
查看>>
单点登陆(SSO)
查看>>
HR,也确实“尽职尽责”
查看>>
MaxComputer 使用客户端配置
查看>>
20190823 顺其自然
查看>>
阅读《余生有你,人间值得》有感
查看>>
每日英语
查看>>
SpringCloud+feign 基于Springboot2.0 负载均衡
查看>>
【BZOJ5094】硬盘检测 概率
查看>>
大庆金桥帆软报表案例
查看>>
Proxy模式
查看>>
读书多些会怎样
查看>>
HDU 2188------巴什博弈
查看>>
tp5任务队列使用supervisor常驻进程
查看>>