Public class excelhelper
{
# Region member variables
Private string templetfile = NULL;
Private string outputfile = NULL;
Private object missing = missing. value;
Private datetime Beforetime; // time before Excel start
Private datetime aftertime; // time after Excel startup
Microsoft. Office. InterOP. Excel. Application app;
Microsoft. Office. InterOP. Excel. Workbook workbook;
Microsoft. Office. InterOP. Excel. worksheet;
Microsoft. Office. InterOP. Excel. Range range;
Microsoft. Office. InterOP. Excel. Range range1;
Microsoft. Office. InterOP. Excel. Range range2;
Microsoft. Office. InterOP. Excel. textbox;
Private int sheetcount = 1; // Number of worksheet instances
Private string sheetprefixname = "page ";
# Endregion
# Region Public attributes
/// <Summary>
/// Worksheet prefix name. For example, if the prefix is "page", the names of worksheet are "Page-1, page-2 ..."
/// </Summary>
Public String sheetprefixname
{
Set {This. sheetprefixname = value ;}
}
/// <Summary>
/// Number of worksheet instances
/// </Summary>
Public int worksheetcount
{
Get {return Workbook. Sheets. Count ;}
}
/// <Summary>
/// Microsoft. Office. InterOP. Excel template file path
/// </Summary>
Public String templetfilepath
{
Set {This. templetfile = value ;}
}
///
// output Excel file path
///
Public String outputfilepath
{< BR> set {This. outputfile = value ;}< BR >}< BR ># endregion
Public static void makeexceldocs (string sourcepath, string destinpath, string newname)
{< br> destinpath = destinpath + "\" + newname + ". xls ";
// ensure that the target does not exist.
file. delete (destinpath);
// copy the file.
file. copy (sourcepath, destinpath);
// try to copy the same file again, which shoshould succeed.
file. copy (sourcepath, destinpath, true);
}
Public static void openexceldocs (string filename, string [] content)
{
Microsoft. Office. InterOP. Excel. Application Excel = new Microsoft. Office. InterOP. Excel. Application (); // reference an Excel Object
Microsoft. Office. InterOP. Excel. Workbook book = excel. workbooks. Open (filename, type. Missing, type. Missing
, Type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing); // reference an Excel Workbook
Microsoft. Office. InterOP. Excel. worksheet sheet = (Microsoft. Office. InterOP. Excel. worksheet) book. Sheets. get_item (1); // reference the Excel work page
Excel. Visible = false;
String [] newcontent = stringhelper. Modify (content );
Sheet. cells [10, 3] = newcontent [0];
Sheet. cells [11, 3] = newcontent [1];
Sheet. cells [12, 3] = newcontent [2];
Sheet. cells [13, 3] = newcontent [3];
Sheet. cells [14, 3] = newcontent [4];
Sheet. cells [15, 3] = newcontent [5];
Sheet. cells [16, 3] = newcontent [6];
Sheet. cells [17, 3] = newcontent [7];
Book. Save ();
Book. Close (type. Missing, type. Missing, type. Missing );
Excel. Quit ();
Intptr T = new intptr (Excel. hwnd );
Int K = 0;
Getwindowthreadprocessid (T, out k );
System. Diagnostics. PROCESS p = system. Diagnostics. process. getprocpolicyid (k );
P. Kill ();
// Sheet = NULL;
// Book = NULL;
// Excel = NULL
// System. runtime. interopservices. Marshal. releasecomobject (sheet );
// System. runtime. interopservices. Marshal. releasecomobject (book );
// System. runtime. interopservices. Marshal. releasecomobject (Excel );
}
[dllimport ("user32.dll", charset = charset. auto)]
Public static extern int getwindowthreadprocessid (intptr hwnd, out int ID );
Public static void openexceldocs2 (string filename, double [] content)
{
Microsoft. office. interOP. excel. application Excel = new Microsoft. office. interOP. excel. application (); // reference an Excel Object
Microsoft. office. interOP. excel. workbook book = excel. workbooks. open (filename, type. missing, type. missing, type. missing, type. missing, type. missing
, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing); // reference an Excel Workbook
Microsoft. office. interOP. excel. worksheet sheet = (Microsoft. office. interOP. excel. worksheet) book. sheets. get_item (1); // reference the Excel work page
excel. visible = false;
Sheet. cells [24, 3] = content [1];
Sheet. cells [25, 3] = content [0];
Book. Save ();
Book. Close (type. Missing, type. Missing, type. Missing );
Excel. Quit ();// ApplicationProgramLaunched, but the process is still running
intptr T = new intptr (Excel. hwnd); // a good way to kill a process, which is very effective
int K = 0;
getwindowthreadprocessid (T, out k);
system. diagnostics. PROCESS p = system. diagnostics. process. getprocpolicyid (k);
P. kill ();
// Sheet = NULL;
// Book = NULL;
// Excel = NULL;// Process cannot be killed
// System. runtime. interopservices. Marshal. releasecomobject (sheet );// Objects can be released, but processes cannot be killed.
// System. runtime. interopservices. Marshal. releasecomobject (book );
// System. runtime. interopservices. Marshal. releasecomobject (Excel );
}
}