This is a migrated thread and some comments may be shown as answers.

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

2 Answers 647 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Dhamodharan
Top achievements
Rank 1
Dhamodharan asked on 23 Jan 2014, 09:28 AM
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 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 23 Jan 2014, 12:37 PM
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
0
Dhamodharan
Top achievements
Rank 1
answered on 24 Jan 2014, 08:18 AM
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
Tags
General Discussions
Asked by
Dhamodharan
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Dhamodharan
Top achievements
Rank 1
Share this question
or