Exporting from an HTML table to a Spreadsheet

Discussion in 'ASP .Net' started by Carlitos, Feb 4, 2008.

  1. Carlitos

    Carlitos Guest

    Hi there,

    I have a asp.net page that dynamically constructs a table based on data
    retrieved from a database. This is an excerpt of the code used:

    ' Create a row
    Dim row As HtmlTableRow = New HtmlTableRow
    ' Create a column and add it to the row
    Dim cell As HtmlTableCell = New HtmlTableCell
    cell.InnerText = sValue
    row.Cells.Add(cell)
    ' ... other columns created and added...
    tblSummaryTable.Rows.Add(row) ' tblSummaryTable already exists on page,
    running at server
    ' ... loop to add more rows as needed...

    Now I need to export this table to a spreadsheet when the user clicks a
    button on the page.

    I have tried a variety of routines, suggestions, etc. None of them have
    worked. I only get an empty spreadsheet, sometimes with very wide column (I
    don't know why).

    Almost all examples I have seen involve the use of datasets/datagrids. For
    reasons not relevant to the question, I cannot use datagrids for this.

    The question is: Can an HTML table be exported or converted to a spreadsheet
    directly (or through a routine, whatever)?

    Many thanks,

    --
    Carlitos
    Carlitos, Feb 4, 2008
    #1
    1. Advertising

  2. This sample is for a GridView, but it should work for any servercontrol:

    // you also need this to suppress "not inside Form tag" errors on
    RenderControl
    public override void VerifyRenderingInServerForm(Control control)
    {
    // no-op, just override so base doesn't do anything
    }
    protected void lnkExport_Click(object sender, EventArgs e)
    {

    Response.Clear();
    Response.AddHeader("content-disposition",
    "attachment;filename=OvertureReport.xls");
    Response.Charset = "";
    // If you want the option to open the Excel file without saving
    than
    // uncomment the line below
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "application/vnd.xls";
    StringWriter stringWrite = new StringWriter();
    HtmlTextWriter htmlWrite =
    new HtmlTextWriter(stringWrite);
    gvDomains.RenderControl(htmlWrite);
    Response.Write(stringWrite.ToString());
    Response.End();

    }

    --
    Site: http://www.eggheadcafe.com
    UnBlog: http://petesbloggerama.blogspot.com
    MetaFinder: http://www.blogmetafinder.com


    "Carlitos" wrote:

    > Hi there,
    >
    > I have a asp.net page that dynamically constructs a table based on data
    > retrieved from a database. This is an excerpt of the code used:
    >
    > ' Create a row
    > Dim row As HtmlTableRow = New HtmlTableRow
    > ' Create a column and add it to the row
    > Dim cell As HtmlTableCell = New HtmlTableCell
    > cell.InnerText = sValue
    > row.Cells.Add(cell)
    > ' ... other columns created and added...
    > tblSummaryTable.Rows.Add(row) ' tblSummaryTable already exists on page,
    > running at server
    > ' ... loop to add more rows as needed...
    >
    > Now I need to export this table to a spreadsheet when the user clicks a
    > button on the page.
    >
    > I have tried a variety of routines, suggestions, etc. None of them have
    > worked. I only get an empty spreadsheet, sometimes with very wide column (I
    > don't know why).
    >
    > Almost all examples I have seen involve the use of datasets/datagrids. For
    > reasons not relevant to the question, I cannot use datagrids for this.
    >
    > The question is: Can an HTML table be exported or converted to a spreadsheet
    > directly (or through a routine, whatever)?
    >
    > Many thanks,
    >
    > --
    > Carlitos
    Peter Bromberg [C# MVP], Feb 4, 2008
    #2
    1. Advertising

  3. Yes, you can use this free control to export your HTML to Excel, or you can
    read about how the source code works and do it yourself:
    http://SteveOrr.net/articles/ExportPanel.aspx

    --
    I hope this helps,
    Steve C. Orr,
    MCSD, MVP, CSM, ASPInsider
    http://SteveOrr.net
    http://iPhonePlaza.net


    "Carlitos" <> wrote in message
    news:...
    > Hi there,
    >
    > I have a asp.net page that dynamically constructs a table based on data
    > retrieved from a database. This is an excerpt of the code used:
    >
    > ' Create a row
    > Dim row As HtmlTableRow = New HtmlTableRow
    > ' Create a column and add it to the row
    > Dim cell As HtmlTableCell = New HtmlTableCell
    > cell.InnerText = sValue
    > row.Cells.Add(cell)
    > ' ... other columns created and added...
    > tblSummaryTable.Rows.Add(row) ' tblSummaryTable already exists on page,
    > running at server
    > ' ... loop to add more rows as needed...
    >
    > Now I need to export this table to a spreadsheet when the user clicks a
    > button on the page.
    >
    > I have tried a variety of routines, suggestions, etc. None of them have
    > worked. I only get an empty spreadsheet, sometimes with very wide column
    > (I
    > don't know why).
    >
    > Almost all examples I have seen involve the use of datasets/datagrids.
    > For
    > reasons not relevant to the question, I cannot use datagrids for this.
    >
    > The question is: Can an HTML table be exported or converted to a
    > spreadsheet
    > directly (or through a routine, whatever)?
    >
    > Many thanks,
    >
    > --
    > Carlitos
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider], Feb 4, 2008
    #3
  4. Carlitos

    gnewsgroup Guest

    On Feb 4, 1:23 pm, "Steve C. Orr [MCSD, MVP, CSM, ASP Insider]"
    <> wrote:
    > Yes, you can use this free control to export your HTML to Excel, or you can
    > read about how the source code works and do it yourself:http://SteveOrr.net/articles/ExportPanel.aspx
    >
    > --
    > I hope this helps,
    > Steve C. Orr,
    > MCSD, MVP, CSM, ASPInsiderhttp://SteveOrr.nethttp://iPhonePlaza.net
    >


    I have been using Steve's free ExportPanel to export web page to
    Word. Really nice, and I strongly recommend. Thank Steve for sharing
    for free.
    gnewsgroup, Feb 4, 2008
    #4
  5. Carlitos

    Carlitos Guest

    Thanks, Peter, for your response. Unfortunately, that routine I have tried
    it in several different forms with no desirable results. You say it should
    work with any server control, but for some reason, it does not do it for me.
    Probably I am missing something. But anyway, I really appreciate your reply.

    Thanks!
    --
    Carlitos


    "Peter Bromberg [C# MVP]" wrote:

    > This sample is for a GridView, but it should work for any servercontrol:
    >
    > // you also need this to suppress "not inside Form tag" errors on
    > RenderControl
    > public override void VerifyRenderingInServerForm(Control control)
    > {
    > // no-op, just override so base doesn't do anything
    > }
    > protected void lnkExport_Click(object sender, EventArgs e)
    > {
    >
    > Response.Clear();
    > Response.AddHeader("content-disposition",
    > "attachment;filename=OvertureReport.xls");
    > Response.Charset = "";
    > // If you want the option to open the Excel file without saving
    > than
    > // uncomment the line below
    > Response.Cache.SetCacheability(HttpCacheability.NoCache);
    > Response.ContentType = "application/vnd.xls";
    > StringWriter stringWrite = new StringWriter();
    > HtmlTextWriter htmlWrite =
    > new HtmlTextWriter(stringWrite);
    > gvDomains.RenderControl(htmlWrite);
    > Response.Write(stringWrite.ToString());
    > Response.End();
    >
    > }
    >
    > --
    > Site: http://www.eggheadcafe.com
    > UnBlog: http://petesbloggerama.blogspot.com
    > MetaFinder: http://www.blogmetafinder.com
    >
    >
    > "Carlitos" wrote:
    >
    > > Hi there,
    > >
    > > I have a asp.net page that dynamically constructs a table based on data
    > > retrieved from a database. This is an excerpt of the code used:
    > >
    > > ' Create a row
    > > Dim row As HtmlTableRow = New HtmlTableRow
    > > ' Create a column and add it to the row
    > > Dim cell As HtmlTableCell = New HtmlTableCell
    > > cell.InnerText = sValue
    > > row.Cells.Add(cell)
    > > ' ... other columns created and added...
    > > tblSummaryTable.Rows.Add(row) ' tblSummaryTable already exists on page,
    > > running at server
    > > ' ... loop to add more rows as needed...
    > >
    > > Now I need to export this table to a spreadsheet when the user clicks a
    > > button on the page.
    > >
    > > I have tried a variety of routines, suggestions, etc. None of them have
    > > worked. I only get an empty spreadsheet, sometimes with very wide column (I
    > > don't know why).
    > >
    > > Almost all examples I have seen involve the use of datasets/datagrids. For
    > > reasons not relevant to the question, I cannot use datagrids for this.
    > >
    > > The question is: Can an HTML table be exported or converted to a spreadsheet
    > > directly (or through a routine, whatever)?
    > >
    > > Many thanks,
    > >
    > > --
    > > Carlitos
    Carlitos, Feb 4, 2008
    #5
  6. Carlitos

    siccolo Guest

    siccolo, Feb 4, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roberto Lopes
    Replies:
    1
    Views:
    2,188
    mahere
    Dec 27, 2007
  2. Gobi
    Replies:
    8
    Views:
    16,759
    mbalhoot
    May 18, 2011
  3. HokBen
    Replies:
    0
    Views:
    743
    HokBen
    Nov 9, 2006
  4. =?Utf-8?B?c2NobWlkdGU=?=

    Exporting HTML table to Excel

    =?Utf-8?B?c2NobWlkdGU=?=, Mar 20, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    1,422
    Mark Rae
    Mar 22, 2007
  5. M P

    Exporting HTML Table to Excel?

    M P, Jul 6, 2004, in forum: ASP General
    Replies:
    2
    Views:
    201
    Greg Griffiths
    Jul 18, 2004
Loading...

Share This Page