How to Convert HTML table to PowerPoint by Free API

Oliver32
Posted by in C# category on for Beginner level | Points: 250 | Views : 10104 red flag

This article will explain in detail to convert HTML to Word, Excel and PowerPoint. In the article, we will look into a solution for converting HTML table to PowerPoint using free API in C#.

Introduction

This article will explain in detail to convert HTML to Word, Excel and PowerPoint. It is easy to find the solution for the first two conversions on free resources site. But the solution for converting HTML to PowerPoint seems to be lack, while spending a lot of time and effort is still pretty lucky to find a solution for converting HTML table to PowerPoint. Hope this article helps other developers. In this solution, two free apis(Html Agility Pack and Spire.Presentation) are used, below will introduce it in detail.

Application Overview

Solution is divided into two parts.

First, Html Agility Pack is used to convert HTML table to DataTable and a method for it is defined. The following is related code and detailed comments.

public DataTable ConvertHTMLTableToDataTable(string htmlfile)

{

//Create an HTMLDocument

var doc = new HtmlAgilityPack.HtmlDocument();

//Load an html file

doc.Load(htmlfile);

//Get the nodes of tags<table>

var nodes = doc.DocumentNode.SelectNodes("//table/tr");

//Create an DataTable and named it as MyTable

var table = new DataTable("MyTable");

//Get the nodes of the tags<th>

var headers = nodes[0].Elements("th").Select(th => th.InnerText.Trim());

//Add columns for dataTable

foreach (var header in headers)

{

table.Columns.Add(header);

}

//Get the nodes of the tags<td>

var rows = nodes.Skip(1).Select(tr => tr.Elements("td").Select(td => td.InnerText.Trim()).ToArray());

//Add rows for dataTable

foreach (var row in rows)

{

table.Rows.Add(row);

}

return table;

}

Next, load a html file to call ConvertHTMLtoDataTable method to get its dataTable.

DataTable dt = ConvertHTMLtoDataTable("C:\\test.html");

The screenshot for HTML file:


The screenshot for DataTable:


The second stage, Spire.Presentation is used to create a PPTX document with the data in above dataTable, detailed as follows.

//Create a PowerPoint document

Presentation presentation = new Presentation();

//Define column width

Double[] widths = new double[] { 100,80, 80, 80, 80 };

//Define row height

Double[] heights = new double[] { 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15 };

//Add new table to PPT

ITable table = presentation.Slides[0].Shapes.AppendTable(presentation.SlideSize.Size.Width

/ 2 - 220, 80, widths, heights);

//Add header of table

for (int j = 0; j < dt.Columns.Count; j++)

{

table[j, 0].TextFrame.Text = dt.Columns[j].ToString();

}

//Fill the table with data in dataTable

for (int i = 1; i <= dt.Rows.Count; i++)

{

for (int j = 0; j < dt.Columns.Count; j++)

{

table[j, i].TextFrame.Text = dt.Rows[i - 1][j].ToString();

//set the Font

table[j,i].TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont("Arial Narrow");

}

}

//set the style of table

table.StylePreset = TableStylePreset.None;

//save thedocument

presentation.SaveToFile("table.pptx", FileFormat.Pptx2010);

The screenshot for PPTX document:




Page copy protected against web site content infringement by Copyscape

About the Author

Oliver32
Full Name: Oliver D
Member Level: Starter
Member Status: Member
Member Since: 4/3/2014 3:26:03 AM
Country: United States



Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)