asp.net中操作Excel全集[1]

 

         首先在 Web.Config 中添加模拟帐号

 

<identity impersonate="true" userName="administrator"password="password" />

 

         其次 要添加 EXCEL com 组件。如果是EXCEL2003,则添加 EXcel library 11.0 版本。我把用到的引用头文件列举如下:

 

        private Excel.Application objApp;

       private Excel.Sheets objSheets;

       private Excel._Worksheet objSheet;

       private Excel.Range range;

      objApp = new Excel.Application();

      objSheet = (Excel._Worksheet)objSheets.get_Item(1);

 

          我接着把大家编程中用到的函数列举如下:

 

using System;

using System.IO;

using System.Text;

using System.Data;

using System.Reflection;

using System.Diagnostics;

using System.Collections;

 

 

namespace TEST.EXCEL

 

{

 /// <summary>

 ///     明:Excel输出打印模块

 ///     暂时不提供操作Excel对象样式方法,样式可以在Excel模板中设置好

 ///     者:

 /// 创建日期:

 /// 修  改:Super

 /// </summary>

 public class ExcelHelper

 {

  #region 成员变量

  private string templetFile = null;

  private string outputFile = null;

  private object missing = Missing.Value;

  private DateTime beforeTime;   //Excel启动之前时间

  private DateTime afterTime;    //Excel启动之后时间

  Excel.Application app;

  Excel.Workbook workBook;

  Excel.Worksheet workSheet;

  Excel.Range range;

  Excel.Range range1;

  Excel.Range range2;

  Excel.TextBox textBox;

  private int sheetCount = 1;   //WorkSheet数量

  private string sheetPrefixName = "";

  #endregion

 

  #region 公共属性

  /// <summary>

  /// WorkSheet前缀名,比如:前缀名为“页”,那么WorkSheet名称依次为“页-1,页-2...

  /// </summary>

  public string SheetPrefixName

  {

   set { this.sheetPrefixName = value; }

  }

 

  /// <summary>

  /// WorkSheet数量

  /// </summary>

  public int WorkSheetCount

  {

   get { return workBook.Sheets.Count; }

  }

 

  /// <summary>

  /// Excel模板文件路径

  /// </summary>

  public string TempletFilePath

  {

   set { this.templetFile = value; }

  }

 

  /// <summary>

  /// 输出Excel文件路径

  /// </summary>

  public string OutputFilePath

  {

   set { this.outputFile = value; }

  }

  #endregion

 

  #region 公共方法

 

  #region ExcelHelper

  /// <summary>

  /// 构造函数,将一个已有Excel工作簿作为模板,并指定输出路径

  /// </summary>

  /// <param name="templetFilePath">Excel模板文件路径</param>

  /// <param name="outputFilePath">输出Excel文件路径</param>

  public ExcelHelper(string templetFilePath,string outputFilePath)

  {

   if(templetFilePath == null)

    throw new Exception("Excel模板文件路径不能为空!");

 

   if(outputFilePath == null)

    throw new Exception("输出Excel文件路径不能为空!");

 

   if(!File.Exists(templetFilePath))

    throw new Exception("指定路径的Excel模板文件不存在!");

 

   this.templetFile = templetFilePath;

   this.outputFile = outputFilePath;

 

   //创建一个Application对象并使其可见

   beforeTime = DateTime.Now;

   app = new Excel.ApplicationClass();

   app.Visible = true;

   afterTime = DateTime.Now;

 

   //打开模板文件,得到WorkBook对象

   workBook = app.Workbooks.Open(templetFile,missing,missing,missing,missing,missing,

    missing,missing,missing,missing,missing,missing,missing);

 

   //得到WorkSheet对象

   workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(1);

 

  }

 

  /// <summary>

  /// 构造函数,打开一个已有的工作簿

  /// </summary>

  /// <param name="fileName">Excel文件名</param>

  public ExcelHelper(string fileName)

  {

   if(!File.Exists(fileName))

    throw new Exception("指定路径的Excel文件不存在!");

 

   //创建一个Application对象并使其可见

   beforeTime = DateTime.Now;

   app = new Excel.ApplicationClass();

   app.Visible = true;

   afterTime = DateTime.Now;

 

   //打开一个WorkBook

   workBook = app.Workbooks.Open(fileName,

    Type.Missing, Type.Missing, Type.Missing, Type.Missing,

    Type.Missing, Type.Missing, Type.Missing, Type.Missing,

    Type.Missing, Type.Missing, Type.Missing, Type.Missing);

 

   //得到WorkSheet对象

   workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(1);

 

  }

 

  /// <summary>

  /// 构造函数,新建一个工作簿

  /// </summary>

  public ExcelHelper()

  {

   //创建一个Application对象并使其可见

   beforeTime = DateTime.Now;

   app = new Excel.ApplicationClass();

   app.Visible = true;

   afterTime = DateTime.Now;

 

   //新建一个WorkBook

   workBook = app.Workbooks.Add(Type.Missing);

 

   //得到WorkSheet对象

   workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(1);

 

  }

  #endregion

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值