How to create a HTML string with multiple cell range in C#,VB.NET?
Syncfusion Excel (XlsIO) library is a WinForms Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files. Using this library, you can get HTML formatted string in cell value and append it to one by one to create a HTML string from multiple range in C# and VB.NET.
Steps to create a HTML string from multiple range programmatically:
Step 1: Create a new C# console application project.
Step 2: Install the Syncfusion.XlsIO.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
Step 3: Include the following namespace in Program.cs file.
C#
using Syncfusion.XlsIO;
VB.NET
Imports Syncfusion.XlsIO
Step 4: Include the following code snippet in main method of Program.cs file to set freeze panes.
C#
//Instantiate the ExcelEngine using (ExcelEngine excelEngine = new ExcelEngine()) { //Open existing workbook with data entered Assembly assembly = typeof(Program).GetTypeInfo().Assembly; Stream fileStream = assembly.GetManifestResourceStream("MultipleHTMLString.Sample.xlsx"); //Instantiate IApplication object IApplication application = excelEngine.Excel; //Load the existing excel file into IWorkbook IWorkbook workbook = application.Workbooks.Open(fileStream); //Access the first worksheet in Excel workbook IWorksheet worksheet = workbook.Worksheets[0]; //Access the worksheet migrant range IMigrantRange range = worksheet.MigrantRange; //Create HTML string from multiple range string htmlString = "<table>"; for (int row = 1; row <= worksheet.UsedRange.LastRow; row++) { htmlString += "<tr>"; for (int col = 1; col <= worksheet.UsedRange.LastColumn; col++) { range.ResetRowColumn(row, col); htmlString += "<td>" + range.HtmlString + "</td>"; } htmlString += "</tr>"; } htmlString += "</table>"; //Save the string in html document StreamWriter writer = File.CreateText("Data.html"); writer.Write(htmlString); writer.Flush(); }
VB.NET
'Instantiate the ExcelEngine Using excelEngine As ExcelEngine = New ExcelEngine() 'Open existing workbook with data entered Dim assembly As Assembly = GetType(Form1).GetTypeInfo().Assembly Dim fileStream As Stream = assembly.GetManifestResourceStream("MultipleHtmlString.Sample.xlsx") 'Instantiate IApplication object Dim application As IApplication = excelEngine.Excel 'Load the existing excel file into IWorkbook Dim workbook As IWorkbook = application.Workbooks.Open(fileStream) 'Access the first worksheet in Excel workbook Dim worksheet As IWorksheet = workbook.Worksheets(0) 'Access the worksheet migrant range Dim range As IMigrantRange = worksheet.MigrantRange 'Create HTML string from multiple range Dim htmlString As String = "<table>" For row As Integer = 1 To worksheet.UsedRange.LastRow htmlString += "<tr>" For col As Integer = 1 To worksheet.UsedRange.LastColumn range.ResetRowColumn(row, col) htmlString += "<td>" & range.HtmlString & "</td>" Next htmlString += "</tr>" Next htmlString += "</table>" 'Save the string in html document Dim writer As StreamWriter = File.CreateText("Data.html") writer.Write(htmlString) writer.Flush() End Using
A complete working example to create HTML String from multiple Excel cell range can be downloaded from HTMLString-from-MultipleRange.zip.
By executing the program, you will get the output HTML as follows.
Take a moment to peruse the documentation, where you will find other options like move or copy a worksheet, show or hide worksheet and worksheet tabs, page setup settings and more with code examples.
Click here to explore the rich set of Syncfusion Excel (XlsIO) library features.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.