Format excel cells with 2 decimal values in Response.BinaryWrite()

3 posts, 0 answers
  1. Dhamodharan
    Dhamodharan avatar
    300 posts
    Member since:
    Sep 2010

    Posted 23 Jan 2014 Link to this post

    Hi,
         I am using Response.BinaryWrite() to export a excel file. By default the whole numbers are display the same but
    i have to format cells by default of 2 decimalspoints. 

    I need a help to over come this issue and suggest me is there any telerik controls can be used.
     





    Thanks
    Dhamodharan
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 23 Jan 2014 Link to this post

    Hi Dhamodharan,

    I'm not sure where you are applying Export. In the case of Export in RadGrid, you can try the following code snippet to format a column text on Export.

    ASPX:
    <asp:Button ID="Button1" runat="server" Text="Export To Excel" OnClick="Button1_Click" />
    <telerik:RadGrid ID="RadGrid1" runat="server" OnPreRender="RadGrid1_PreRender">
       . . .
    </telerik:RadGrid>

    C#:
    bool IsExport = false;
     protected void Button1_Click(object sender, EventArgs e)
     {
         IsExport = true;   
         RadGrid1.MasterTableView.ExportToExcel();
     }
     
     protected void RadGrid1_PreRender(object sender, EventArgs e)
     {
         if (IsExport)
         {       
             foreach (GridDataItem items in RadGrid1.MasterTableView.Items)
             {
                double val = Convert.ToDouble(items["ColumnUniqueName"].Text);
                 string newvalue= string.Format("{0:f2}", val);              
                 items["ColumnUniqueName"].Text = newvalue;
             }
         }
     }

    Thanks,
    Princy
  3. Dhamodharan
    Dhamodharan avatar
    300 posts
    Member since:
    Sep 2010

    Posted 24 Jan 2014 Link to this post

    Hi Princy,
          Thanks for your quick reply. I am not using any radgrid Control to export. the data values are retrived from a file  and forced to export using the following codes.

      Dim fs As FileStream

            fs = File.Open(strPath, FileMode.Open)
           
            Dim bytBytes(fs.Length) As Byte

            fs.Read(bytBytes, 0, fs.Length)
            fs.Close() 
            Response.Buffer = False
            Response.AddHeader("Content-Disposition", "attachment;filename=" & filename.Value)
            ' To open the csv file
            Response.ContentType = "application/CSV"
            Response.BinaryWrite(bytBytes)
            Response.End()

     By default the whole numbers are display the same but
    i have to format cells by default of 2 decimalspoints. 

    I need a help to over come this issue and suggest me is there any telerik controls can be used.


    Thanks
Back to Top