C#文件处理(下载、导出)

来源:互联网 发布:c语言编译预处理 编辑:程序博客网 时间:2024/05/19 16:22
using System;using System.Configuration;using System.IO;using Microsoft.Office.Interop.Excel;using TMS.IMEX.Extensions;using log4net;namespace TMS.IMEX{    public class IMEXExportBase    {        private static readonly ILog m_log = LogManager.GetLogger(typeof(IMEXExportBase));        #region 一览导出文件处理        /// <summary>        /// 一览导出文件处理        /// </summary>        /// <param name="dt"></param>        /// <param name="pathModuleFile"></param>        /// <param name="filePath"></param>        /// <param name="fileName"></param>        public bool ProcessGeneralExportFile(System.Data.DataTable dt, string pathModuleFile, string filePath, string fileName)        {            // 写日志            m_log.Debug("IMEXExportBase->ProcessGeneralExportFile Start");            try            {                int rows = dt.Rows.Count;                int cols = dt.Columns.Count;                string[,] dataArray = new string[rows,cols];                for (int i = 0; i < rows; i++)                {                    for (int j = 0; j < cols; j++)                    {                        dataArray[i, j] = dt.Rows[i][j].ToString();                    }                }                m_log.Debug("Application 实例化");                Application app = new Application();                m_log.Debug("Workbook 实例化");                Workbook book =                    (Workbook)                    app.Workbooks.Open(pathModuleFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing,                                       Type.Missing, Type.Missing,                                       Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,                                       Type.Missing, Type.Missing, Type.Missing);                m_log.Debug("Worksheet 实例化");                Worksheet sheet = (Worksheet) book.Sheets[1];                m_log.Debug("sheet.Range 数据加载");                sheet.Range["A2", app.Cells[rows + 1, cols]].Value = dataArray;                // 如果路径不存在 创建路径                CreateDirectory(filePath);                string newfilePath = filePath +"\\"+ fileName;                //如果文件存在就删除文件                DeleteFile(newfilePath);                m_log.Debug("book._SaveAs Start");                book._SaveAs(newfilePath, XlFileFormat.xlWorkbookNormal, null, null, false, false,                             XlSaveAsAccessMode.xlNoChange, null, null, null, null);                m_log.Debug("book._SaveAs End");                //关闭Excel进程                book.Close(null, null, null);                System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);                System.Runtime.InteropServices.Marshal.ReleaseComObject(book);                app.Quit();                System.Runtime.InteropServices.Marshal.ReleaseComObject(app);                sheet = null;                book = null;                app = null;                GC.Collect();                return true;            }            catch (Exception ex)            {                m_log.Error("IMEXExportBase->ExportExcel" + ex.Message);                return false;            }            finally            {                // 写日志                m_log.Debug("IMEXExportBase->ProcessGeneralExportFile End");            }        }        #endregion        #region 创建工作目录        /// <summary>        /// 创建工作目录        /// </summary>        /// <param name="pathDir"></param>        /// <returns></returns>        public void CreateDirectory(string pathDir)        {            if (!Directory.Exists(pathDir))            {                Directory.CreateDirectory(pathDir);            }        }        #endregion        #region 删除文件        /// <summary>        /// 删除文件        /// </summary>        /// <param name="filePath"></param>        /// <returns></returns>        public void DeleteFile(string filePath)        {            if (!string.IsNullOrEmpty(filePath)&&File.Exists(filePath))                    File.Delete(filePath);        }        #endregion    }}


下载文件

        #region 模板导出        /// <summary>        /// 模板导出        /// </summary>        /// <returns></returns>        public FileStreamResult TemplateExport()        {            m_log.Debug("RCV0060Controller->TemplateExport Start");            try            {                string phyPath = ConfigurationSettings.AppSettings[IMEXConfig.IMMAWBQuoteTemplatePath];                string absoluFilePath = Server.MapPath(phyPath);                return File(new FileStream(absoluFilePath, FileMode.Open, FileAccess.Read), "application/octet-stream",                            Server.UrlEncode("MAWB报价导入模板.xlsx"));            }            catch (Exception ex)            {                m_log.Error("RCV0060Controller->TemplateExport Error:", ex);                throw;            }            finally            {                m_log.Debug("RCV0060Controller->TemplateExport End");            }        }        #endregion


 

原创粉丝点击