How to Create HTML String with Multiple Cell Using WinForms Excel?
Syncfusion® Excel (XlsIO) library is a WinForms Excel library used to create, read, and edit Excel documents. It also converts Excel documents to PDF files. Using this library, you can get an HTML-formatted string in a cell value and append it one by one to create an HTML string from multiple ranges in C# and VB.NET.
Steps to create an HTML string from multiple ranges 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 the Program.cs file:
C#
using Syncfusion.XlsIO;
VB.NET
Imports Syncfusion.XlsIO
Step 4: Include the following code snippet in the main method of the 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 the Excel workbook IWorksheet worksheet = workbook.Worksheets[0]; // Access the worksheet migrant ranges 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 ab 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 the Excel workbook Dim worksheet As IWorksheet = workbook.Worksheets(0) 'Access the worksheet migrant ranges 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 an HTML document Dim writer As StreamWriter = File.CreateText("Data.html") writer.Write(htmlString) writer.Flush() End Using
A complete working example to create an HTML string from multiple Excel cell ranges 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 can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and, protect the Excel documents, and most importantly, the PDF, CSV and Image conversions 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.
Conclusion
I hope you enjoyed learning about how to create a HTML string with multiple cell range Using WinForms Excel.
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!