Convert EXCEL TO HTML
代码
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, 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 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;
}
}
}
}
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, 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 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;
}
}
}
}