It is often necessary to use Excel COM in the development, and write this class for simplification, it is still not convenient.
1
Using
System;
2
3
Namespace
ExcelHandle
4
{5 / ** //// 6 /// ExcelHandle's summary description.
7 /// summary> 8 public class Excelhandle 9 {10 11 / ** //// 12 /// Excel 13 /// summary> 14 public excel.application curexcel = null; 15 16 / ** //// 17 /// Workbook 18 /// summary> 19 public excel._Workbook Curbook = NULL; 20 21 / ** //// 22 /// Table 23 /// //////////////////////// 29 /// 29 /// Constructive function 30 /// summary> 31 public excelhandle () 32 {33 // 34 // Todo: Add constructor logic 35 // 36 37 this.dtbefore = system.datetime.now; 38 39 curexcel = new Excel.Application (); 40 41 this.dtafter = system.datetime.now; 42 43 this.timestamp = system.datetime.now.toshortdateString (). Replace (",", "") system.datetime.now. ToshostTimeString () .Replace (":", "") system.datetime.now.second.tostring () system.datetime.now.millisecond.tostring (); 44 45} 46 47 / ** //// 48 /// Constructor 49 /// summary> 50 /// Loaded Excel file name param> 51 public ExcelHandle (String StrfilePath) 52 {53 54 this.dtbefore = System.datetime.now; 55 56 Curexcel = new Excel.Application (); 57 58 this.dtafter = system.datetime.now; 5960 copubook =
(Excel._Workbook) CurExcel.Workbooks.Open (strFilePath, mValue, false, mValue, mValue, mValue, mValue, mValue, mValue, mValue, mValue, mValue, mValue, mValue, mValue); 61 62 this.timestamp = System. DateTime.now.toshortdateString (). Replace ("-", "") system.datetime.now.toshortTimeString (). Replace (":", ") system.datetime.now.sem.datetime.now.second.tostring () System.datetime.now.millisecond.toString (); 63 64} 65 66 / ** //// 67 /// Release memory space 68 /// summary> 69 public void dispose () 70 { 71 try 72 {73 System.Runtime.InteropServices.Marshal.ReleaseComObject (CurSheet); 74 CurSheet = null; 75 76 CurBook.Close (false, mValue, mValue); 77 System.Runtime.InteropServices.Marshal.ReleaseComObject (CurBook); 78 CURBOOK = NULL; 79 80 Curexcel.quit (); 81 System.Runtime.InteropServices.Marshal.ReleaseComobject (CUREXCEL); 82 Curexcel = NULL; 83 84 gc.collect (); 85 gc.waitforpendingfinalizers (); 86 87} 89 {90 this.MessageWarning ("A error occurred while releaseing Excel memory space:" , ex); 91} 92 finally 93 {94 foreach (System.Diagnostics.Process pro in System.Diagnostics.Process.GetProcessesByName ( "Excel")) 95 if (pro.StartTime> this.dtBefore && pro.StartTime
97} 98 System.GC.SuppressFinalize (this); 99} 100101102 private string filepath; 103 private string timestamp; 104 private System.DateTime dtBefore; 105 private System.DateTime dtAfter; 106107108 / ** //// 109 /// Excel file name 110 /// summary> 111 public string filepath112 {113 get114 {115 return this.filepath; 116} 117 set118 {119 this.filepath = value; 120} 121} 122123 / ** // /// 124 //: Do not open the Excel interface 125 /// summary> 126 public bool visible127 {128 set129 {130 curexcel.visible = value; 131} 132} 133134 / ** //// 135 /// Taking time string as a name of the saved file 136 /// summary> 137 public string timestamp138 {139 get140 {141 returnims.timestamp; 142 } 143 set144 {145 this.TimeStamp = Value; 146} 147} 148149150 / ** //// 151 /// loading Excel file 152 /// summary> 153 public void loading () 154 {155 IF (Curbook == Null && this.filepath! = null) 156 CURBOOK = (Excel._Workbook) Curexcel.Workbooks.open (this.Filepath, Mvalue, False, Mvalue, Mvalue, Mvalue, Mvalue, Mvalue, Mvalue, Mvalue, Mvalue, Mvalue, Mvalue, Mvalue, Mvalue, Mvalue; 157} 158159 / ** //// 160 /// Loading Excel file 161 /// summary> 162 ///
Excel file name param> 163 public void Load (string strFilePath) 164 {165 if (CurBook == null) 166 CurBook = (Excel._Workbook) CurExcel.Workbooks.Open (strFilePath, mValue, false, mValue, mValue, mValue , Mvalue, Mvalue, Mvalue, Mvalue, Mvalue, Mvalue, Mvalue, Mvalue; 167} 168169 / ** //// 170 /// New Worksheet 171 /// summary> 172 / // sheet name param> 173 public void NewWorkSheet (string strWorkSheetName) 174 {175 CurSheet = (Excel._Worksheet) CurBook.Sheets.Add (CurBook.Sheets [1], mValue, Mvalue, mvalue); 176 Cursheet.name = strWorksheetName; 177} 178179 / ** //// 180 /// Insert the specified value of 181 /// summary> 182 //// < Param name = "strcell"> cell, such as "A4" param> 183 /// text, digital equivalent param> 184 public void Writecell (String strchool, Object objvalue) 185 {186 CURSHEET.GET_RANGE (Strcell, M Value) .Value2 = objvalue; 187} 188189 / ** //// 190 /// Insert the specified value in the specified RANGE 191 /// summary> 192 /// Range start cell param> 193 /// Range end cell param> 194 /// text, digital equivalent < / param> 195 public void WriteRange (string strStartCell, string strEndCell, object objValue) 196 {197 CurSheet.get_Range (strStartCell, strEndCell) .Value2 = objValue; 198} 199200201 / ** ////
202 /// Merged unit, and insert the specified value 203 /// param> 205 /// param> 205 /// param> 206 /// param> 207 public void WriteAfterMerge (string strStartCell, string strEndCell, object objValue) 208 {209 CurSheet.get_Range (strStartCell, strEndCell ) .Merge (mvalue); 210 cursheet.get_range (strStartcell, mvalue) .value2 = objvalue; 211} 212213 / ** //// 214 /// Insert a DataTable in a DataTable in a continuous cell 215 /// summary> 216 /// Started cell param> 217 /// Storage data DataTable param> 218 public void WRITETABLE (STRING STRSTARTATABLE DTDATA) 219 {220 Object [,] arrdata = new object [dtdata.rows.count, dtdata.columns.count]; 221222 for (int i = 0; i 232 /// Insert a DataTable in the continuous cell and makes a hyperlink 233 /// summary> 234 /// start cell identifier param> 235 /// DataTable param> 236 ////
转载请注明原文地址:https://www.9cbs.com/read-129477.html