Question: In an aspx page: 

```
<asp:table id = "table1" runat ="server"></table>

```

I have created a table using Javascript from the cs file like this: 

```
StringBuilder sb = new StringBuilder() 
sb.Append('<script>') ;
sb.Append(document.write('<table><tr><td>hghj</td></tr></table>')) 
Table cell ;
Table row;
 cell.Control.Add(new LiteralControl(sb.ToString())) ;
row.Control.Add(cell);
table1.Control.Add(row);

```

Now I want to export that table to excel, so I used the following code. It opened the excel file. But no data is seen.

```
Response.ContentType = "application/vnd.ms-excel";        
Response.AddHeader("Content-Disposition", "attachment; filename=ExcelFile.xls");
Response.ContentEncoding = Encoding.UTF8;        
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
table1.RenderControl(hw);        
Response.Write(tw.ToString());        
Response.End(); 

```

Answer: 
I had a somewhat similar issue and the following solution worked for me. Hopefully it helps OP and others. In my case, the table data was dynamically being populated at run-time but when I tried to export the data to excel, I was getting a blank excel file. I solved this issue by putting my table generation code outside `if (!IsPostBack){}`. This is what my code structure looks like after that change:

```
protected void Page_Load(object sender, EventArgs e)
{
    FunctionToPopulateTableDataAtRuntime();

    if (!IsPostBack)
    {
        // other parts of the code
    }
}

private void FunctionToPopulateTableDataAtRuntime()
{
    // code to dynamically populate table data at run-time
    // this code is somewhat similar to that of OP
    // but it does not make use of JavaScript whatsoever
}

private void ExportTableToExcel()
{
    // the code for this function is somewhat similar to what OP has
    Response.ContentType = "application/vnd.ms-excel";
    Response.AddHeader("Content-Disposition", "attachment; filename=test.xls;");
    Response.ContentEncoding = Encoding.UTF8;
    StringWriter stringWrite = new StringWriter();
    HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
    myTable.RenderControl(htmlWrite);
    Response.Write(stringWrite.ToString());
    Response.End();
}    

protected void btnExportToExcel_Click(object sender, EventArgs e)
{
    ExportTableToExcel();
}

```

This is what the code-front (aspx) looks like:

```
<asp:Table 
    ID="myTable"
    runat="server">
</asp:Table>
<asp:Button 
    ID="btnExportToExcel" 
    runat="server" 
    Text=" Export to Excel " 
    OnClick="btnExportToExcel_Click"/>

```
```
public override void VerifyRenderingInServerForm(Control control)
{
    /* Verifies that the control is rendered */
}

```