ExcelOperations类库
来源:互联网 发布:悠游域名批量查询工具 编辑:程序博客网 时间:2024/06/08 08:08
using System;
using System.Collections.Generic;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using System.Diagnostics;
using System.Windows.Forms;
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 = System.Reflection.Missing.Value;
public ExcelClass()
{
if (m_ExcelApplication == null)
{
m_ExcelApplication = new Excel.ApplicationClass();
}
}
~ExcelClass()
{
try
{
if (m_ExcelApplication != null)
m_ExcelApplication.Quit();
}
catch (Exception ex)
{
Debug.Write(ex.ToString());
}
}
/// <summary>
/// 获取或设置当前工作表
/// </summary>
public int CurrentWorksheetIndex
{
set
{
if (value <= 0 || value > m_Workbook.Worksheets.Count)
throw new Exception("索引超出范围");
else
{
object index = value;
m_Worksheet = m_Workbook.Worksheets[index] as Excel._Worksheet;
}
}
}
/// <summary>
/// 打开一个Excel工作薄
/// </summary>
/// <param name="fileName"></param>
public void OpenWorkbook(string fileName)
{
m_Workbook = m_ExcelApplication.Workbooks.Open(fileName, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing);
if (m_Workbook.Worksheets.Count > 0)
{
object index = 1;
m_Worksheet = m_Workbook.Worksheets[index] as Excel._Worksheet;
}
}
/// <summary>
/// 保存数据
/// </summary>
public void Save()
{
if (m_Workbook != null)
{
m_Workbook.Save();
}
}
/// <summary>
/// 关闭文档
/// </summary>
/// <param name="isSave"></param>
public void Close(bool isSave)
{
this.ClearClipboard();
object obj_Save = isSave;
if (m_Workbook != null)
m_Workbook.Close(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 cell1 = cellIndex;
Excel.Range range = m_Worksheet.get_Range(cell1, missing);
if (range != null)
{
range.Value2 = value;
}
}
}
/// <summary>
/// 合并单元格
/// </summary>
/// <param name="cellIndex1"></param>
/// <param name="cellIndex2"></param>
public void Merge(string cellIndex1, string cellIndex2)
{
if (m_Worksheet != null)
{
object cell1 = cellIndex1;
object cell2 = cellIndex2;
Excel.Range range = m_Worksheet.get_Range(cell1, cell2);
range.Merge(true);
}
}
/// <summary>
/// 将当前工作表中的表格数据复制到剪切板
/// </summary>
public void Copy()
{
if (m_Worksheet != null)
{
try
{
m_Worksheet.UsedRange.Select();
}
catch { }
m_Worksheet.UsedRange.Copy(missing);
}
}
/// <summary>
/// 清空剪切板
/// </summary>
public void ClearClipboard()
{
Clipboard.Clear();
}
#region IDisposable 成员
public void Dispose()
{
try
{
if (m_ExcelApplication != null)
{
this.Close(false);
m_ExcelApplication.Quit();
m_ExcelApplication = null;
}
}
catch (Exception ex)
{
Debug.Write(ex.ToString());
}
}
#endregion
}
}
using System.Collections.Generic;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using System.Diagnostics;
using System.Windows.Forms;
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 = System.Reflection.Missing.Value;
public ExcelClass()
{
if (m_ExcelApplication == null)
{
m_ExcelApplication = new Excel.ApplicationClass();
}
}
~ExcelClass()
{
try
{
if (m_ExcelApplication != null)
m_ExcelApplication.Quit();
}
catch (Exception ex)
{
Debug.Write(ex.ToString());
}
}
/// <summary>
/// 获取或设置当前工作表
/// </summary>
public int CurrentWorksheetIndex
{
set
{
if (value <= 0 || value > m_Workbook.Worksheets.Count)
throw new Exception("索引超出范围");
else
{
object index = value;
m_Worksheet = m_Workbook.Worksheets[index] as Excel._Worksheet;
}
}
}
/// <summary>
/// 打开一个Excel工作薄
/// </summary>
/// <param name="fileName"></param>
public void OpenWorkbook(string fileName)
{
m_Workbook = m_ExcelApplication.Workbooks.Open(fileName, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing);
if (m_Workbook.Worksheets.Count > 0)
{
object index = 1;
m_Worksheet = m_Workbook.Worksheets[index] as Excel._Worksheet;
}
}
/// <summary>
/// 保存数据
/// </summary>
public void Save()
{
if (m_Workbook != null)
{
m_Workbook.Save();
}
}
/// <summary>
/// 关闭文档
/// </summary>
/// <param name="isSave"></param>
public void Close(bool isSave)
{
this.ClearClipboard();
object obj_Save = isSave;
if (m_Workbook != null)
m_Workbook.Close(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 cell1 = cellIndex;
Excel.Range range = m_Worksheet.get_Range(cell1, missing);
if (range != null)
{
range.Value2 = value;
}
}
}
/// <summary>
/// 合并单元格
/// </summary>
/// <param name="cellIndex1"></param>
/// <param name="cellIndex2"></param>
public void Merge(string cellIndex1, string cellIndex2)
{
if (m_Worksheet != null)
{
object cell1 = cellIndex1;
object cell2 = cellIndex2;
Excel.Range range = m_Worksheet.get_Range(cell1, cell2);
range.Merge(true);
}
}
/// <summary>
/// 将当前工作表中的表格数据复制到剪切板
/// </summary>
public void Copy()
{
if (m_Worksheet != null)
{
try
{
m_Worksheet.UsedRange.Select();
}
catch { }
m_Worksheet.UsedRange.Copy(missing);
}
}
/// <summary>
/// 清空剪切板
/// </summary>
public void ClearClipboard()
{
Clipboard.Clear();
}
#region IDisposable 成员
public void Dispose()
{
try
{
if (m_ExcelApplication != null)
{
this.Close(false);
m_ExcelApplication.Quit();
m_ExcelApplication = null;
}
}
catch (Exception ex)
{
Debug.Write(ex.ToString());
}
}
#endregion
}
}
0 0
- ExcelOperations类库
- 类库
- 类库
- JavaScript类库
- .net类库
- MyGUI类库
- .Net类库
- .NET类库
- CxImage类库
- .NETFramework类库
- .NET类库
- 。NET类库
- as3swf 类库
- c# - 类库
- AS3 类库
- C++ 类库
- CPP类库
- Office类库
- vulakn教程--Drawing a Triangle--Pipeline--Fixed function
- Jquery easyui 批量处理datagrid的数据
- jQuery禁用和启用页面元素
- DecryptEncrypt类库
- win7下centOS7的安装和无线上网设置(博通驱动)
- ExcelOperations类库
- Revit开发NewFamilyInstance技巧之创建梁
- Android studio 读源码技巧
- lightoj1259 【素数预处理】
- 区间dp模型(石子归并,括号匹配,整数划分)入门经典三道题
- ftp类库
- 【POJ3744】Scout YYF I-概率DP+矩阵加速优化
- 51nod 1315
- IT项目管理知识体系42过程