I have a question that I hope someone can answer. I am using .Net 3.5 Winforms and the SpreadSheetGear 2010 component and need to know if there is a free or low cost method to convert an Excel Workbook to HTML? Is there a good XSLT transform or some low cost or open source component? I can save the Excel file to OpenXMLWorkbook programmactically but the component does not allow to save to html. Any help would be appreciated. Thanks.
Use ASP.Net and follow the code demonstrated by the Excel to DataGrid Samples. You can always include a reference to System.Web
in your project.
But, even without ASP.Net, and sticking with the .Net client profile, generating an HTML table from a DataTable
is simple. Use the AntiXss Library (download) to encode the data to html. Then construct your HTML with a StringBuilder
. Here's an extension method you can use to convert any DataTable
to an html <table>
:
using AntiXss = Microsoft.Security.Application;
...
public static String ToHTML(this DataTable dt)
{
StringBuilder html = new StringBuilder("<table><thead><tr>");
foreach (DataColumn col in dt.Columns)
{
html.AppendFormat("<td>{0}</td>",
AntiXss.Encoder.HtmlEncode(col.ColumnName));
}
html.Append("</tr></thead><tbody>");
foreach (DataRow row in dt.Rows)
{
html.Append("<tr>");
foreach (var data in row.ItemArray)
{
html.AppendFormat("<td>{0}</td>",
AntiXss.Encoder.HtmlEncode(data.ToString()));
}
html.Append("</tr>");
}
html.Append("</tbody></table>");
return html.ToString();
}
And to get html for an entire workbook, another extension method:
public static List<String> ToHTML(this IWorkBook wb)
{
List<String> tables = new List<String>();
DataSet ds = wb.GetDataSet(GetDataFlags.FormattedText);
foreach (DataTable dt in ds.Tables)
{
tables.Add(dt.ToHTML());
}
return tables;
}
-
-
@GregWilliams - I've updated my answer with a solution that doesn't require
System.Web
. – gilly3 Aug 1 '12 at 22:38