熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#封裝Excel常用操作類

2013-11-13 09:57:05  來源: .NET編程 
 using System;
using SystemCollectionsGeneric;
using SystemText;

  using Excel = MicrosoftOfficeInteropExcel;
using SystemDiagnostics;
using SystemWindowsForms;

  namespace OfficeManager
{
    public class ExcelClass : IDisposable
    {
        private Excel_Application m_ExcelApplication = null;
        private Excel_Workbook m_Workbook = null;
        public Excel_Worksheet m_Worksheet = null;
        private object missing = SystemReflectionMissingValue;

  public ExcelClass()
        {
            if (m_ExcelApplication == null)
            {
                m_ExcelApplication = new ExcelApplicationClass();
            }
        }

  ~ExcelClass()
        {
            try
            {
                if (m_ExcelApplication != null)
                    m_ExcelApplicationQuit();
            }
            catch (Exception ex)
            {
                DebugWrite(exToString());
            }
        }
        /// <summary>
        /// 獲取或設置當前工作表
        /// </summary>
        public int CurrentWorksheetIndex
        {
            set
            {
                if (value <= || value > m_WorkbookWorksheetsCount)
                    throw new Exception(索引超出范圍);
                else
                {
                    object index = value;
                    m_Worksheet = m_WorkbookWorksheets[index] as Excel_Worksheet;
                }
            }
        }
        /// <summary>
        /// 打開一個Excel工作薄
        /// </summary>
        /// <param name=fileName></param>
        public void OpenWorkbook(string fileName)
        {
            m_Workbook = m_ExcelApplicationWorkbooksOpen(fileName missing missing missing missing missing
                missing missing missing missing missing missing missing missing missing);

  if (m_WorkbookWorksheetsCount > )
            {
                object index = ;
                m_Worksheet = m_WorkbookWorksheets[index] as Excel_Worksheet;

  }
        }
        /// <summary>
        /// 保存數據
        /// </summary>
        public void Save()
        {
            if (m_Workbook != null)
            {
                m_WorkbookSave();
            }
        }
        /// <summary>
        /// 關閉文檔
        /// </summary>
        /// <param name=isSave></param>
        public void Close(bool isSave)
        {
            thisClearClipboard();

  object obj_Save = isSave;
            if (m_Workbook != null)
                m_WorkbookClose(obj_Save missing missing);
        }
        /// <summary>
        /// 設置當前工作表中某單元格的值
        /// </summary>
        /// <param name=cellIndex></param>
        /// <param name=value></param>
        public void SetCellValue(string cellIndex object value)
        {
            if (m_Worksheet != null)
            {
                object cell = cellIndex;
                ExcelRange range = m_Worksheetget_Range(cell missing);
                if (range != null)
                {
                    rangeValue = value;
                }
            }
        }
        /// <summary>
        /// 合並單元格
        /// </summary>
        /// <param name=cellIndex></param>
        /// <param name=cellIndex></param>
        public void Merge(string cellIndex string cellIndex)
        {
            if (m_Worksheet != null)
            {
                object cell = cellIndex;
                object cell = cellIndex;
                ExcelRange range = m_Worksheetget_Range(cell cell);
                rangeMerge(true);
            }
        }
        /// <summary>
        /// 將當前工作表中的表格數據復制到剪切板
        /// </summary>
        public void Copy()
        {
            if (m_Worksheet != null)
            {
                try
                {
                    m_WorksheetUsedRangeSelect();
                }
                catch { }
                m_WorksheetUsedRangeCopy(missing);
            }
        }
        /// <summary>
        /// 清空剪切板
        /// </summary>
        public void ClearClipboard()
        {
            ClipboardClear();
        }

  #region IDisposable 成員

  public void Dispose()
        {
            try
            {
                if (m_ExcelApplication != null)
                {
                    thisClose(false);
                    m_ExcelApplicationQuit();
                    m_ExcelApplication = null;
                }
            }
            catch (Exception ex)
            {
                DebugWrite(exToString());
            }
        }

  #endregion
    }
}


From:http://tw.wingwit.com/Article/program/net/201311/12085.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.