65.9K
CodeProject is changing. Read more.
Home

Convert HTMLTable to Comma Separated Values

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1 vote)

Jan 4, 2011

CPOL
viewsIcon

31082

A simple class that converts HTMLTable to CSV

This is a simple class that takes an HTMLTable in the constructor and returns an array of strings in CSV format.
namespace WebLib
{
    using System.Collections.Generic;
    using System.Text;
    using System.Web.UI.HtmlControls;

    public class HTMLTableHelper
    {
        #region Fields

        private readonly HtmlTable htmlTable;

        #endregion
        
        #region Constructors

        public HTMLTableHelper(HtmlTable htmlTable)
        {
            this.htmlTable = htmlTable;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Converts HTML Table to Comma Seperated Values structure.
        /// </summary>
        /// <returns></returns>
        public string[] ConvertToCSV()
        {
            //Will hold all rows of table as CSV
            List<string> rows = new List<string>();

            //Loop through each row in the HTML table
            foreach (HtmlTableRow row in htmlTable.Rows)
            {
                StringBuilder rowCVS = new StringBuilder();
                //Loop through each cell in the row
                foreach (HtmlTableCell cell in row.Cells)
                {
                    //Appends cell Text to rowCVS
                    rowCVS.Append(cell.TagName.Trim());
                    //Adds comma after text
                    rowCVS.Append(","); 
                }
                //Removes last Comma from row
                rowCVS.Remove(rowCVS.Length - 1, 1);
                //Add cvs row to list of rows
                rows.Add(rowCVS.ToString());
            }

            return rows.ToArray();
        }

        #endregion
    }
}
In the hope that it might prove useful, Jethro Badenhorst