471,315 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,315 software developers and data experts.

Converting excel worksheets to separate html files programmaticall

I have been tasked with producing a server application that will convert
excel files to html.

I have no experience of developing under Windows, (but plenty of UNIX/C++
experience) but I picked up a copy of Visual Studio .Net and managed to
produce the attached C# code expecting it to save each worksheet as a
separate html file. Unfortunately I just end up with multiple copies of the
entire workbook.

Can anyone explain why this isn't saving individual worksheets?

Thanks,

David Holt.

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)wsEnumer ator.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);
}
}

Nov 29 '05 #1
0 1765

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Allison Bailey | last post: by
1 post views Thread by apgoodb | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.