Code
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using Microsoft. Office. InterOP. Excel;
Using system. diagnostics;
Namespace exceltohtml
{
Public class excelreader
{
Private sheets worksheets;
Private worksheet currentworksheet;
/// <Summary>
/// Construct an Excel object to open an Excel file
/// </Summary>
/// <Param name = "FILENAME"> </param>
Public excelreader (string filename)
{
Applicationclass excelobj = new applicationclass ();
// Open the selected Excel File
Workbook theworkbook = excelobj. workbooks. open (filename, 0, true, 5, "", "", true, xlplatform. xlwindows, "\ t", false, false, 0, true, null, null );
// Get the worksheets of the Excel file
Worksheets = theworkbook. worksheets;
}
/// <Summary>
/// Set the current worksheet to get data from
/// </Summary>
/// <Param name = "worksheetindex"> worksheet number starts from 1 </param>
Public void setcurrentworksheet (INT worksheetindex)
{
Currentworksheet = (worksheet) worksheets. get_item (worksheetindex );
// Currentworksheet. cells. wraptext = true;
Currentworksheet. standardwidth = 250;
}
Public void saveashtml (string filename)
{
Try
{
Currentworksheet. saveas (filename, xlfileformat. xlhtml, type. Missing, type. Missing,
Type. Missing, Microsoft. Office. InterOP. Excel. xlsaveasaccessmode. xlexclusive, type. Missing );
}
Catch (exception e ){}
Finally
{
Killprocess ("Excel ");
}
}
/// <Summary>
/// Get the values of a range from the current Worksheet
/// E. g: getrange ("A1", "f1") will get 6 elements of the first row
/// </Summary>
/// <Param name = "startcell"> e. g: "A1" </param>
/// <Param name = "endcell"> e. g: "F6" </param>
/// <Returns> </returns>
Public array getrange (string startcell, string endcell)
{
Range = currentworksheet. get_range (startcell, endcell); // get the cells
Array array = (system. array) range. cells. get_value (null); // get the values of the cells
Return array;
}
Private void killprocess (string processname)
{
System. Diagnostics. Process myproc = new system. Diagnostics. Process ();
Try
{
Foreach (process thisproc in process. getprocessesbyname (processname ))
{
If (! Thisproc. closemainwindow ())
{
Thisproc. Kill ();
}
} // Next proc
}
Catch (exception exc)
{
String MSG = exc. message;
}
}
}
}