Convert EXCEL to HTML

Source: Internet
Author: User
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;
}
}
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.