c#

位置:IT落伍者 >> c# >> 浏览文章

C#封装Excel常用操作类


发布日期:2022年04月19日
 
C#封装Excel常用操作类
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

}

}

               

上一篇:.Net Framework框架源码学习

下一篇:C# partial 说明