zoukankan      html  css  js  c++  java
  • FileUtils 文件下载 文件导出

        public class FileUtils
        {
            /// <summary>
            /// 文件下载
            /// </summary>
            /// <param name="page">页面参数</param>
            /// <param name="filePath">文件源路径</param>
            /// <param name="saveFileName">文件命名</param>
            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 {   }
            }
    
            /// <summary>
            /// 文件导出
            /// </summary>
            /// <param name="page">对象页面</param>
            /// <param name="filePath">文件保存服务器路径</param>
            /// <param name="dt">数据源</param>
            /// <param name="exportFileName">导出后的文件名称</param>
            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('	');
                    }
                    sw.WriteLine();
    
                    foreach (DataRow dr in dt.Rows)
                    {
                        for (i = 0; i <= dt.Columns.Count - 1; i++)
                        {
                            sw.Write(dr[i].ToString());
                            sw.Write('	');
                        }
                        sw.WriteLine();
                    }
                    sw.Close();
                    FileDownload(page, filePath, exportFileName);
                }
                catch { }
            }
        }
  • 相关阅读:
    第十六节:类与对象基本概念
    dedecms源码分析:(1)index.php
    第十二节:控制结构foreachbreakcontinueswitch
    PHP的输出缓冲区(转)
    C 结构体小结
    指针参数在内存中的传递
    C typedef用途小结
    C语言考试2 题目整理
    MinGW 环境下 给hello.exe 自定义一个图标
    JavaEE程序设计快速入门小结
  • 原文地址:https://www.cnblogs.com/dragon-L/p/3777221.html
Copyright © 2011-2022 走看看