Hi ,
The following code my excel export function . In this how do i include the cell size to so small and need to expand after export .
How do i fix this .
Sample code :
BranchFullName = cmbBranches.SelectedItem.Text; // This will Return Branch FullName
Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName); // This will Return BranchName( Actual Database Name )
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
//To Export all pages
GridView1.AllowPaging = false;
BindGrid(Cmb_BranchName, Microsoft.Security.Application.Encoder.HtmlEncode(txtStartDt.Text), Microsoft.Security.Application.Encoder.HtmlEncode(txtEndDt.Text));
GridView1.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
cell.BackColor = GridView1.HeaderStyle.BackColor;
}
foreach (GridViewRow row in GridView1.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = GridView1.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}
GridView1.RenderControl(hw);
//style to format numbers to string
string style = @"";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
Thanks in advance

Karthik KPosted May 8, 2025, 7:42 AM
@Daniel Wright,
that code doesnt't work .
Daniel WrightPosted May 8, 2025, 3:59 AM
To control the cell size for Excel export in ASP.NET, you can leverage the HTML formatting within the Excel file being generated. In your existing code snippet, you can modify the style section to include specific attributes for the cell size. Here's a breakdown of the steps to adjust the cell size to be small initially and expand upon export:
1. Update the Style Section:
Within your existing style block, add CSS properties to adjust the cell size. For instance, you can set a specific width for the cells. Here's an example of how you can adjust the cell size by adding the `width` property:
By setting a specific width, you can control the initial cell size in the exported Excel file.
2. Testing and Adjustments:
After making the changes, run your export function and verify if the cell size behaves as expected. You may need to adjust the width value based on your requirements until you achieve the desired initial cell size.
3. Dynamic Cell Expansion:
If you want the cells to expand after export, you can either set a fixed width for smaller cells and let Excel handle the expansion based on the content, or you can dynamically adjust the cell size based on the content of each cell. For dynamic expansion, you can utilize Excel's capabilities to automatically adjust cell sizes based on content by not specifying a fixed width in the style.
By following these steps and customizing the cell size through CSS properties like `width`, you can fine-tune the appearance of cells in your Excel export to start small and potentially expand based on the content. Remember to test your changes thoroughly to ensure the desired outcome. Let me know if you need further assistance or clarification on this matter!