4

In my ASP.NET Web-form Project I have an Event which Export Data (List<Profit>) to Excel . I'm using the old fashion way , writing to HTML file with .XLS extenstionmode

Profit Entity has some string properties which has numeric data like "100000561234"

When I export to Excel , Those Columns appear like 1.00001E+11 in Excel columns, So the user has to Right-Click on Excel column and change format-cell.

Any help appreciated

4
  • 1
    How exactly are you doing the export to Excel? Whatever library you're using should have a mechanism to set the style on the cell as you export. If you're doing it the niaeve 'write an HTML file with an .XLS extension' way you're probably be out of luck. Commented Apr 11, 2012 at 8:32
  • @Rup, :Yes , I'm Writing to HTML file with .XLS extenstion Commented Apr 11, 2012 at 8:34
  • 2
    Right - in that case you can either try and get an HTML export from Excel with a cell formatted properly and look for the mso attributes that it puts on the cell to indicate the formatting (and declare the mso namespace at the top in the same way too) or you can switch to one of the Excel generation libraries in this question which will have support for formatting cells properly too. Commented Apr 11, 2012 at 8:37
  • 1
    I found EPPlus to be reliable and easy to use. epplus.codeplex.com Commented Apr 11, 2012 at 8:39

2 Answers 2

4

Simply Add the following line before your HTML code, it will resolve the issue. This style sheet will be applied on every cell and no need to format the cell as string manually.

Response.Write("<style> TD { mso-number-format:\@; } </style>"); 
Sign up to request clarification or add additional context in comments.

Comments

0

Simply Add the following code in VB.net from Code Behind

    For Each row As GridViewRow In grdvDummyExport.Rows

     For Each cell As TableCell In row.Cells
       Dim columnName As String = GetColumnNameOfTableCell(cell)
         If columnName = "Loan Number" Then
              'cell.CssClass = "textmode"
               cell.Style("mso-number-format") = "\@"
         End If

     Next
   Next


  Private Function GetColumnNameOfTableCell(ByVal tc As TableCell) 
    As String
    Return(CType((CType(tc,DataControlFieldCell)).ContainingField, BoundField)).DataField

  End Function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.