Dynamic
Dynamic

Reputation: 1663

How to Export an HTML Table to Excel/Word/PDF/CSV?

I have a html table which is populated dynamically. I need to put 4 buttons below it which enables exporting the table structure and data to Excel, Word, PDF and CSV.

What is the best/easiest approach to implement this?

I don't think relying on the client installed application is a great idea so I am thinking to provide this feature using backend code (c#).

Thanks.

Upvotes: 1

Views: 1898

Answers (1)

Dynamic
Dynamic

Reputation: 1663

I used this method:

private void PrepareResponseHeader()
{
    HttpResponse.Clear();
    HttpResponse.Buffer = true;
    HttpResponse.AddHeader("content-disposition",
                           String.Format("attachment;filename={0}.{1}", this.FileName, this.GetFileExtension()));
    HttpResponse.Charset = "";
    HttpResponse.ContentType = this.GetContentType();
}

and this:

protected void WriteAndEnd(string value)
{
    HttpResponse.Output.Write(value);
    HttpResponse.Flush();
    HttpResponse.End();
}

PrepareResponseHeader();
WriteAndEnd(htmlData);

Upvotes: 1

Related Questions