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
- C#文件处理(下载、导出)
- C#导出csv文件,下载
- 文件下载(导出)
- C#导入导出与处理Excel文件
- C# 断点续传 上传、下载文件处理
- C# 断点续传 上传、下载文件处理
- C# 断点续传 上传、下载文件处理
- C# 断点续传 上传、下载文件处理
- C# 一般处理程序下载文件
- C# DataTable 导出CSV 文件并在客户端下载
- C# DataTable 导出CSV 文件并在客户端下载
- 文件上传、下载、导出
- 文件下载/导出
- C# 文件下载(WebClient)
- flex 导出文件&导出图片&文件下载
- mysql导出文件处理
- C#导出文件函数
- C#导出Excel文件
- extjs+html5
- HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth之完全详解
- 浅谈微软Surface的跨时代意义
- java 常用数据结构
- ECShop使用心得
- C#文件处理(下载、导出)
- 二维费用 hdu 2159 FATE(完全背包)HDU OJ 4501 小明系列故事——买年货【DP】
- Oracle的SGA与系统vm.nr_hugepages不匹配导致的故障
- 在VC中调用DirectShow播放视频
- 互联网快照备份查询!
- 求平方根sqrt()函数的底层算法效率问题
- 2.金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)->(一千零一拾一元整)输出
- PowerDesigner的Pallete如何设置
- [转载]自适应网页设计