当前位置:软件学习 > Excel >>

C#导入导出Excel通用类(SamWang)

  1 /******************************************************************
  2  *
  3  *
  4  * 描    述:
  5  *             导入导出Excel通用类
  6  * 版    本:  V1.0     
  7  * 环    境:  VS2005
  8 ******************************************************************/
  9 using System;
 10 using System.Collections.Generic;
 11 using System.Text;
 12 using System.Windows.Forms;
 13 using Excel = Microsoft.Office.Interop.Excel;
 14 using System.Data;
 15 using System.Drawing;
 16 using System.Collections;
 17 using System.Diagnostics;
 18 using System.Data.OleDb;
 19
 20 namespace LingDang.CRM.UI.Client
 21 {
 22     public class ExcelIO:IDisposable
 23     {
 24         #region Constructors
 25         private ExcelIO()
 26         {
 27             status = IsExistExecl() ? 0 : -1;
 28         }
 29
 30         public static ExcelIO GetInstance()
 31         {
 32             //if(instance == null)
 33             //{
 34             //    lock (syncRoot)
 35             //    {
 36             //         if(instance == null)
 37             //         {
 38             //            instance = new ExcelIO();
 39             //         }
 40             //    }
 41             //}
 42             //return instance;
 43             return new ExcelIO();
 44         }
 45         #endregion
 46
 47         #region Fields
 48         private static ExcelIO instance;
 49         private static readonly object syncRoot = new object();
 50         private string returnMessage;
 51         private Excel.Application xlApp;
 52         private Excel.Workbooks workbooks = null;
 53         private Excel.Workbook workbook = null;
 54         private Excel.Worksheet worksheet = null;
 55         private Excel.Range range = null;
 56         private int status = -1;
 57         private bool disposed = false;//是否已经释放资源的标记
 58         #endregion           
 59    
 60         #region Properties
 61         /// <summary>
 62         /// 返回信息
 63         /// </summary>
 64         public string ReturnMessage
 65         {
 66             get { return returnMessage; }
 67         }
 68
 69         /// <summary>
 70         /// 状态:0-正常,-1-失败 1-成功
 71         /// </summary>
 72         public int Status
 73         {
 74             get { return status;}
 75         }
 76         #endregion
 77
 78         #region Methods
 79         /// <summary>
 80         /// 判断是否安装Excel
 81         /// </summary>
 82         /// <returns></returns>
 83         protected bool IsExistExecl()
 84         {
 85             try
 86             {
 87                 xlApp = new Excel.Application();
 88                 if (xlApp == null)
 89                 {
 90                     returnMessage = "无法创建Excel对象,可能您的计算机未安装Excel!";
 91                     return false;
 92                 }
 93             }
 94             catch (Exception ex)
 95             {
 96                

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,