Excel files as HTML using C#

  • Thread starter Thread starter kewalmehra
  • Start date Start date
K

kewalmehra

Hi All,

I have a requirement of saving Excel files as HTML using C#. I have
managed to write code for saving as HTLM file. however this seems to be

not working in the once perticular case .


For e.g I hav the following values are in column say A1 & A2.


A1A2
D B
B O
A N
G D
V V
A A
L L


0 1
1 2
1 1


When I save this as HTML, I am getting saved file as below ( Note the
change vertical display to horizontal) . Any suggestions / comments on

this is highly appreciated. Thanks a lot for your time.


DBAGVAL BONDVAL
0 1
1 2
1 1


Regards,
Kew
 
How are you doing the actual saving? Are you calling Excel Interop
with a save as command to HTML or are you pulling the values from Excel
into your program then saving it as an html file?

Viewing the HTML in a browser is one thing, making sure your
*generated* HTML is correct is another.
 
Hi Greg,

Thanks a lot for your time and reply. I am using the Excel interop
with save as commend to HTML. Something similar below.... any
suggestions?

using Microsoft.Office.Interop.Excel;


public void ConvertExcelFile(String excelFile)
{
Microsoft.Office.Interop.Excel.Application excel = null;
Microsoft.Office.Interop.Excel.Workbook xls = null;
try
{
excel = new Microsoft.Office.Interop.Excel.Application();
object missing = Type.Missing;
object trueObject = true;
excel.Visible = false;
excel.DisplayAlerts = false;


xls = excel.Workbooks.Open(excelFile, missing, trueObject, missing,

missing, missing, missing, missing, missing, missing, missing, missing,

missing, missing, missing);


object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;

IEnumerator wsEnumerator =
excel.ActiveWorkbook.Worksheets.GetEnumerator();
int i = 1;
while (wsEnumerator.MoveNext())
{
Microsoft.Office.Interop.Excel.Worksheet wsCurrent =
(Microsoft.Office.Interop.Excel.Worksheet)wsEnumerator.Current;
String outputFile = excelFile + "." + i.ToString() + ".html";
wsCurrent.SaveAs(outputFile, format, missing, missing, missing,
missing, missing, missing, missing, missing);
++i;
}
excel.Quit();
}
catch (COMException ex)
{
MessageBox.Show("Error accessing Excel document.\n\n" +
ex.Message);
}
 
Hi Greg,

Thanks a lot for your time and reply. I am using the Excel interop
with save as commend to HTML. Something similar below.... any
suggestions?

using Microsoft.Office.Interop.Excel;


public void ConvertExcelFile(String excelFile)
{
Microsoft.Office.Interop.Excel.Application excel = null;
Microsoft.Office.Interop.Excel.Workbook xls = null;
try
{
excel = new Microsoft.Office.Interop.Excel.Application();
object missing = Type.Missing;
object trueObject = true;
excel.Visible = false;
excel.DisplayAlerts = false;


xls = excel.Workbooks.Open(excelFile, missing, trueObject, missing,

missing, missing, missing, missing, missing, missing, missing, missing,

missing, missing, missing);


object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;

IEnumerator wsEnumerator =
excel.ActiveWorkbook.Worksheets.GetEnumerator();
int i = 1;
while (wsEnumerator.MoveNext())
{
Microsoft.Office.Interop.Excel.Worksheet wsCurrent =
(Microsoft.Office.Interop.Excel.Worksheet)wsEnumerator.Current;
String outputFile = excelFile + "." + i.ToString() + ".html";
wsCurrent.SaveAs(outputFile, format, missing, missing, missing,
missing, missing, missing, missing, missing);
++i;
}
excel.Quit();
}
catch (COMException ex)
{
MessageBox.Show("Error accessing Excel document.\n\n" +
ex.Message);
}
 
Hi Greg,

Thanks a lot for your time and reply. I am using the Excel interop
with save as commend to HTML. Something similar below.... any
suggestions?

using Microsoft.Office.Interop.Excel;


public void ConvertExcelFile(String excelFile)
{
Microsoft.Office.Interop.Excel.Application excel = null;
Microsoft.Office.Interop.Excel.Workbook xls = null;
try
{
excel = new Microsoft.Office.Interop.Excel.Application();
object missing = Type.Missing;
object trueObject = true;
excel.Visible = false;
excel.DisplayAlerts = false;


xls = excel.Workbooks.Open(excelFile, missing, trueObject, missing,

missing, missing, missing, missing, missing, missing, missing, missing,

missing, missing, missing);


object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;

IEnumerator wsEnumerator =
excel.ActiveWorkbook.Worksheets.GetEnumerator();
int i = 1;
while (wsEnumerator.MoveNext())
{
Microsoft.Office.Interop.Excel.Worksheet wsCurrent =
(Microsoft.Office.Interop.Excel.Worksheet)wsEnumerator.Current;
String outputFile = excelFile + "." + i.ToString() + ".html";
wsCurrent.SaveAs(outputFile, format, missing, missing, missing,
missing, missing, missing, missing, missing);
++i;
}
excel.Quit();
}
catch (COMException ex)
{
MessageBox.Show("Error accessing Excel document.\n\n" +
ex.Message);
}


Regards,
Kewal
 
Have you looked at the resulting HTML that is generated from the Excel
output write to see if the HTML is properly formatted?

If the data sets are not too large, you may consider using Excel
Interop to read the values back from the Excel sheet and create your
own HTML output files, we do. Your method leaves it up to Excel to
figure out the how's and why's to output the data into HTML.
 
Back
Top