Click here to Skip to main content
15,911,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following code to bind datagrid

DataGrid dgGrid = new DataGrid();       
dgGrid.DataSource = dt;
dgGrid.DataBind();



this grid i'm writing to an excel file using system .io.file as below

 String path = Application["UpldPath"].ToString() + filename;
      System.IO.StringWriter tw = new System.IO.StringWriter();
      System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

              DataGrid dgGrid = new DataGrid();
              dgGrid.DataSource = dt;
              dgGrid.DataBind();

 //Get the HTML for the control.
               dgGrid.RenderControl(hw);

//Write the HTML back to the browser
               string renderedGridView = tw.ToString();
               File.WriteAllText(path, renderedGridView);


this is writing the excel to the specific path. But large numbers are coming with scientific notation.

How to format the excel column while writing?

Plz advice.
Posted

You need to put explicit style on the specific column while doing the export to tell that it is a string column not an integer.

Following link show this in detail
excel export formatting[^]
 
Share this answer
 
Comments
Member 10112611 17-Sep-13 4:51am    
Great!!! this is working ...Thanks a lot....u saved my time !!!!
Member 10112611 20-Sep-13 7:33am    
Can i use the same to write to a predefined path instead of downloading ?
I have same problem occured when i have generate excelsheet.....

I have remove edit/delete button in my excel sheet that time i have used

this code to remove particular column


dt.Columns.RemoveAt(0);

dt.Columns.RemoveAt(1);
            
dt.Columns.RemoveAt(dt.Columns.Count - 1);


hope it's help you...:-)
 
Share this answer
 
Comments
Member 10112611 20-Sep-13 7:35am    
is this to remove a col? or for changing the format?
[no name] 20-Sep-13 7:58am    
remove column...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900