Results 1 to 3 of 3

Thread: [RESOLVED] Writing integer to an Excel worksheet can't get rid of scientific notation

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2016
    Posts
    21

    Resolved [RESOLVED] Writing integer to an Excel worksheet can't get rid of scientific notation

    HI,

    I am using code similar to the following code to write values into cells in an Excel worksheet:

    Code:
     
    
    Imports Microsoft.Office.Interop
    Imports Microsoft.Office.Interop.Excel
    
    
    Dim xlApp As Excel.Application
    Dim xlWorkBook As Excel.Workbook
    Dim WS As Excel.Worksheet
    
    xlApp = New Excel.Application
    xlWorkBook = xlApp.Workbooks.Open(strReportFile)
    WS = xlWorkBook.Worksheets("TestReport")
    
    
    Dim myCell As Range = CType(WS.Cells(1,1), Range)
    
    Dim Value as string = "1234567890"
    
    myCell.Value = Value
    It works well except for when the string is a long number as shown above (1234567890).

    In this case it displays the number in scientific notation.

    The cells in the spreadsheet are formatted as "text" and if I type the number in the cell directly, it goes in as expected.

    Any idea why it puts in scientific notation when I do it programatically?

    Thanks in advance.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,242

    Re: Writing integer to an Excel worksheet can't get rid of scientific notation

    try formatting the cell:

    Code:
    myCell.NumberFormat = "General"

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2016
    Posts
    21

    Re: Writing integer to an Excel worksheet can't get rid of scientific notation

    Thanks that worked perfectly! For anyone else reading this, the cell formatting must be set AFTER writing the value. Excel seems to be changing the format type based on it's perception of what the value type should be.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width