2
Answers

How to set cell size for excel Export in asp.net ?

Photo of Karthik K

Karthik K

10h
43
1

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 = @"<style> .textmode { mso-number-format:\@;font-size: 15px; } </style>";
     Response.Write(style);
     Response.Output.Write(sw.ToString());
     Response.Flush();
     Response.End();
 }

 

Thanks in advance 

Answers (2)