ASP.NET - DataGrid Export to Excel - Alignment Problem

Joined
Aug 30, 2006
Messages
2
Reaction score
0
In the UI, the DataGrid width for each column have been defined,
but when it is exported to the Excel doc, alignment for each column is messed up in the Excel doc.

The problem is how to align the columns in Excel doc properly, exactly like the UI DataGrid columns.

Please advise.

HTML - Define the width of columns
Code:
 <asp:TemplateColumn HeaderText="No">
   <HeaderStyle Font-Bold="True" Width="1%"></HeaderStyle>
 ......
 <asp:TemplateColumn HeaderText="No">
    <HeaderStyle Font-Bold="True" Width="20%"></HeaderStyle>
 ......
 <asp:TemplateColumn HeaderText="No">
    <HeaderStyle Font-Bold="True" Width="20%"></HeaderStyle>
 ......
 ......

Code Behind - Export the whole DataGrid to the Excel doc.
Code:
   private void btnExportToDoc_Click(object sender, System.EventArgs e)
    {
    	string strTime = DateTime.Now.ToUniversalTime().ToString()+"_"+DateTime.Now.Millisecond.ToString();
    	strTime = strTime.Replace("/", "_");
    	strTime = strTime.Replace(":", "_");
    	strTime = strTime.Replace(" ", "_");
    	
    	Response.Clear();
    	Response.Buffer = true;
    	Response.AddHeader("content-disposition", "attachment; filename=FileDoc_"+strTime+".xls");
    	Response.ContentType = "application/vnd.ms-excel";
    	Response.Charset = "";
    	this.EnableViewState = false;
   	   
    	System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
    	System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
    	
    	// Get the HTML for the control.
    	dgProdReleaseValid.RenderControl(oHtmlTextWriter);
    	// Write the HTML back to the browser.
    	Response.Write(oStringWriter.ToString());
  
    	Response.End();  
    }
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top