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

6 篇文章 0 订阅
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


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值