資源簡介
一個操作EXCEL的類,提供了打開,讀取,導出EXCEL的方法,EXCEL表格合并,寫入等操作

代碼片段和文件信息
using?System;
using?System.IO;
using?System.Text;
using?System.Data;
using?System.Reflection;
using?System.Diagnostics;
using?System.Collections;
using?System.Collections.Generic;
using?System.Runtime.InteropServices;
namespace?Microsoft.Office.Helper
{
????public?class?ExcelHelper
????{
????????private?string?templetFile?=?String.Empty;
????????private?string?outputFile?=?String.Empty;
????????private?object?missing?=?Missing.Value;
????????private?DateTime?beforeTime; //Excel啟動之前時間
????????private?DateTime?afterTime; //Excel啟動之后時間
????????Excel.Application?app;
????????Excel.Workbook?workBook;
????????Excel.Worksheet?workSheet;
????????Excel.TextBox?textBox;
????????Excel.Range?range;
????????private?int?sheetCount?=?1; //WorkSheet數量
????????private?string?sheetPrefixName?=?“Sheet“;
????????#region?公共屬性
????????public?string?SheetPrefixName
????????{
????????????set?{?this.sheetPrefixName?=?value;?}
????????}
????????public?int?WorkSheetCount
????????{
????????????get?{?return?workBook.Sheets.Count;?}
????????}
????????public?string?OutputFilePath
????????{
????????????set?{?this.outputFile?=?value;?}
????????}
????????#endregion
????????#region?CreateExcelFile
????????///?
????????///?構造函數,將一個已有Excel工作簿作為模板,并指定輸出路徑
????????///?
????????///?Excel模板文件路徑
????????///?輸出Excel文件路徑
????????public?ExcelHelper(string?templetFilePath?string?outputFilePath)
????????{
????????????if?(templetFilePath?==?String.Empty)
????????????????throw?new?Exception(“Excel模板文件路徑不能為空!“);
????????????if?(outputFilePath?==?String.Empty)
????????????????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);
????????}
????????///?
????????///?構造函數,打開一個已有的工作簿
????????///?
????????///?Excel文件名
????????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;
????????????//
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????946176??2007-08-21?15:05??Microsoft.Office.Helper\bin\Debug\Interop.Excel.dll
?????文件?????155648??2007-08-21?15:05??Microsoft.Office.Helper\bin\Debug\Interop.Office.dll
?????文件??????32768??2010-03-22?00:15??Microsoft.Office.Helper\bin\Debug\Microsoft.Office.Helper.dll
?????文件??????48640??2010-03-22?00:15??Microsoft.Office.Helper\bin\Debug\Microsoft.Office.Helper.pdb
?????文件??????59981??2010-03-22?00:12??Microsoft.Office.Helper\ExcelHelper.cs
?????文件???????2199??2010-03-19?16:24??Microsoft.Office.Helper\Microsoft.Office.Helper.csproj
?????文件????????958??2010-03-19?16:20??Microsoft.Office.Helper\Microsoft.Office.Helper.sln
????..A..H.?????11776??2010-03-19?17:38??Microsoft.Office.Helper\Microsoft.Office.Helper.suo
?????文件??????32768??2010-03-22?00:15??Microsoft.Office.Helper\obj\Debug\Microsoft.Office.Helper.dll
?????文件??????48640??2010-03-22?00:15??Microsoft.Office.Helper\obj\Debug\Microsoft.Office.Helper.pdb
?????文件????????886??2010-03-22?00:15??Microsoft.Office.Helper\obj\Microsoft.Office.Helper.csproj.FileListAbsolute.txt
?????文件???????1347??2010-03-19?16:20??Microsoft.Office.Helper\Properties\AssemblyInfo.cs
?????目錄??????????0??2010-03-21?23:53??Microsoft.Office.Helper\obj\Debug\Refactor
?????目錄??????????0??2010-03-20?16:36??Microsoft.Office.Helper\obj\Debug\TempPE
?????目錄??????????0??2010-03-22?00:15??Microsoft.Office.Helper\bin\Debug
?????目錄??????????0??2010-03-22?00:15??Microsoft.Office.Helper\obj\Debug
?????目錄??????????0??2010-03-20?16:36??Microsoft.Office.Helper\bin
?????目錄??????????0??2010-03-20?16:36??Microsoft.Office.Helper\obj
?????目錄??????????0??2010-03-20?16:36??Microsoft.Office.Helper\Properties
?????目錄??????????0??2010-03-22?00:12??Microsoft.Office.Helper
-----------?---------??----------?-----??----
??????????????1341787????????????????????20
- 上一篇:WPF漂亮導航面板源碼20121022.zip
- 下一篇:上位機與PLC通訊軟件C#
評論
共有 條評論