How to Export DataTable to HTML through DataGridView?

Spire.DataExport supports exporting data from DataTable into HTML. HTML (HyperText Markup Language) is a language for describing web pages. HTML documents are also called web pages which contain HTML tags and plain text. Use Spire.DataExport exporting DataTable to HTML to share your data on internet.

Download Spire.DataExport (or Spire.Office) with .NET Framework together. Only 2 Simple steps you can finish the whole datatable to HTML exporting process.

Step 1: Load Data Information

Before exporting data from DataTable, we should load data information from data source. And select which information we need export. Through DataGridVew, we even can preview and modify data information. So, in this step, our job is to prepare data which are about to be exported out.

[C#]
	
        private void btnLoad_Click(object sender, EventArgs e)
        {
            using (OleDbConnection oleDbConnection = new OleDbConnection())
            {
                oleDbConnection.ConnectionString = this.tbCS.Text;
                System.Data.OleDb.OleDbCommand oleDbCommand = new OleDbCommand();
                oleDbCommand.CommandText = this.tbCT.Text;
                oleDbCommand.Connection = oleDbConnection;
                using (OleDbDataAdapter da = new OleDbDataAdapter(oleDbCommand))
                {
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    dataGridView1.DataSource = dt;
                }
            }
        }
   

Effect Screenshot

DataTable to HTML

Step 2: Set Export into HTML

Spire.DataExport allows user to export data into most popular file formats including MS Excel, MS Word, HTML, PDF, XML, CSV, DBF, DIF, etc. Here we need set it as HTML format. Spire.DataExport will create a new HTML file and through DataGridView export data into HTML file. You also can rename the file as you like.

[C#]
        private void btnRun_Click(object sender, EventArgs e)
        {
            Spire.DataExport.HTML.HTMLExport HTMLExport = new Spire.DataExport.HTML.HTMLExport();
            HTMLExport.DataSource = Spire.DataExport.Common.ExportSource.DataTable;
            HTMLExport.DataTable = this.dataGridView1.DataSource as DataTable;
            HTMLExport.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView;
            HTMLExport.SaveToFile("20110223.html");
        }  

Effect Screenshot

DataTable to HTML