导出excel.docVIP

  • 4
  • 0
  • 约1.6万字
  • 约 19页
  • 2016-11-27 发布于河南
  • 举报
导出excel

需添加项目引用: 1. .NET-System.Data.OracleClient.dll 2. COM-Microsoft Excel 11.0 Object Library 代码如下: using System; using System.IO; using System.Data; using System.Reflection; using System.Diagnostics; using System.Configuration; using System.Collections; using Excel; namespace thscjy { ?/// ?/// 套用模板输出Excel,生成xls文件和html文件 ?/// Author: Liu Wen ?/// Date Created: 2006-8 ?/// ?public class ExportExcel ?{ ??#region variable member 成员变量 ??protected string templateFile = null; ??protected string excelFile = null; ??protected string htmlFile = null; ??protected object missing = Missing.Value; ??Excel.ApplicationClass app; ??Excel.Workbook book; ??Excel.Worksheet sheet; ??Excel.Range range; ??private DateTime beforeTime;??//Excel启动之前时间 ??private DateTime afterTime;??//Excel启动之后时间 ??//private int processID; ??#endregion ??/// ??/// 构造函数,将一个已有Excel工作簿作为模板,并指定输出路径 ??/// ??/// Excel模板文件路径 ??/// Excel输出文件路径 ??/// Html输出文件路径 ??public ExportExcel(string templateFile, string excelFile, string htmlFile) ??{ ???if (templateFile == null) ????throw new Exception(Excel模板文件路径不能为空!); ???if (excelFile == null) ????throw new Exception(Excel输出文件路径不能为空!); ???if (htmlFile == null) ????throw new Exception(Html输出文件路径不能为空!); ???if (!File.Exists(templateFile)) ????throw new Exception(指定路径的Excel模板文件不存在!); ???this.templateFile = templateFile; ???this.excelFile = excelFile; ???this.htmlFile = htmlFile; ???//创建一个Application对象 ???beforeTime = DateTime.Now; ???app = new ApplicationClass(); ???//app.Visible = true; ???//processID = Process.GetCurrentProcess().Id; ???afterTime = DateTime.Now; ???//打开模板文件,得到WorkBook对象 ???try ???{ ????book = app.Workbooks.Open(templateFile,missing,missing,missing,missing,missing, ?????missing,missing,missing,missing,missing,missing,missing,missing,missing); ???} ???catch (Exception e) ???{ ????throw e; ???} ???//得到WorkSheet对象 ???sheet = (Excel.Worksheet)book.Sheets.get_Item(1); ??} ??#region 插入报表参数 ??/// ??/// 录入报表的参数(TJ统计用) ??/// ??/// 填报单位 ??/// 年月 ??/// 填报人 ??/// 填报日期 ?

文档评论(0)

1亿VIP精品文档

相关文档