Skip to main content
Nov 17, 2011 6:18 PM UTC
Answered

cfspreadsheet how to preserve leading zeroes

Is there a way to preserve leading zeroes in a field when displaying in excel other than adding a ' at the front of the field?  I have an id field defined in sql server as varchar but when creating an xls document using cfspreadsheet excel is interpreting the field as numeric and dropping the leading zeroes.  I cannot have the ' leading that field since it is imported into another system.

    This topic is closed to new replies. Start a new post to keep the conversation going.
    Correct answer
    Nov 18, 2011 2:58 PM UTC

    This has been resolved.  I tested both suggested solutions.  I tried formatting the columns with spreadsheetformatcolumn as suggested in http://stackoverflow.com/questions/3081202/how-to-format-spreadsheet-c olumns-using-coldfusion but that did not preserve the leading zeroes in the ID column.  I then tried the solution with POI found here http://www.bennadel.com/projects/poi-utility.htm by Ben Nadel and that did preserve the leading zeroes.  Thanks for everyone's assistance and thanks to Ben for sharing his POI utility.

    - Julie

    15 replies

    Participating Frequently
    Nov 25, 2024 10:31 PM UTC

    This worked for me. Leading zero will display is the excel column. e.g. 01234

    <cfset SpreadSheetSetCellValue(sheetName, variables.Query.ColumnName, Rowcount, Columncount, "string")>

    Translate
    Nov 18, 2011 4:21 PM UTC

    I did make the adjustment but I must have made an error somewhere else.  I'll play with it when I have time as an alternate solution to the POI one that is working now.  Thanks for your help.

    Translate
    Inspiring
    Nov 18, 2011 4:38 PM UTC

    It is probably just the column index. Though it is unfortunate SpreadSheetFormatColumn does not work properly,  because that workaround should not be necessary.  Anyway, glad you found something that works.

     

    -Leigh

    Translate
    Nov 18, 2011 3:54 PM UTC

    Leigh I did try the exact code and that worked on 9.0.1.

    Translate
    Inspiring
    Nov 18, 2011 4:18 PM UTC

    Okay. That suggests there may be a problem in your adapted query code. Perhaps that is why it did not work. Did you adjust the  forceTextColumnNumber index to point to the correct column?

    -Leigh

    Translate
    Nov 18, 2011 3:37 PM UTC

    Leigh I am running CF9.0.1,

    Translate
    Inspiring
    Nov 18, 2011 3:42 PM UTC

    Hm... and you ran the example "as is" and did not get these results? 

    NUMBERSASTEXT TEXT

    01050094071094340000 abc

    743059E6 def

    Translate
    Nov 18, 2011 3:45 PM UTC

    No I changed the code to run with my query. I can try running it as is if you are curious.

    Translate
    Correct answer
    Nov 18, 2011 2:58 PM UTC

    This has been resolved.  I tested both suggested solutions.  I tried formatting the columns with spreadsheetformatcolumn as suggested in http://stackoverflow.com/questions/3081202/how-to-format-spreadsheet-c olumns-using-coldfusion but that did not preserve the leading zeroes in the ID column.  I then tried the solution with POI found here http://www.bennadel.com/projects/poi-utility.htm by Ben Nadel and that did preserve the leading zeroes.  Thanks for everyone's assistance and thanks to Ben for sharing his POI utility.

    - Julie

    Translate
    Inspiring
    Nov 18, 2011 3:19 PM UTC

    but that did not preserve the leading zeroes in the ID column. 

    Weird. Worked exactly as advertised for me. Are you using CF9.0.1 or just 9.0.0?

     

    -Leigh

    Translate
    Inspiring
    Nov 17, 2011 7:36 PM UTC

    How are you generating the spreadsheet? Have you tried using SpreadSheetFormatColumn?

    http://stackoverflow.com/questions/3081202/how-to-format-spreadsheet-columns-using-coldfusion

    Translate
    Nov 17, 2011 7:58 PM UTC

    I have not used the spreadsheetformatcolumn.  The code I am using is below:

    <cfspreadsheet

            action = "write"

            filename="\test.xls"

            query="sample"

            overwrite="true">

    I had researched another thread that indicated column formatting of color etc was working but type was not.

    Translate
    Inspiring
    Nov 17, 2011 8:18 PM UTC

    cfspreadsheet does not offer control over formatting, and unfortunately there are still some formatting bugs with the other functions. 

    Did you try using the workaround in the link above? It works in CF9.0.1.

    -Leigh

    Translate
    Inspiring
    Nov 17, 2011 7:27 PM UTC

    There was a similar thread on HouseofFusion - http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:63142

    I think the recommended solution was to try using POI to generate your spreadsheets.

    Translate