Skip to main content
November 17, 2011
Answered

cfspreadsheet how to preserve leading zeroes

  • November 17, 2011
  • 7 replies
  • 5802 views

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 has been closed for replies.
    Correct answer

    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

    7 replies

    Participating Frequently
    November 25, 2024

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

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

    November 18, 2011

    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.

    Inspiring
    November 18, 2011

    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

    November 18, 2011

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

    Inspiring
    November 18, 2011

    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

    November 18, 2011

    Leigh I am running CF9.0.1,

    Inspiring
    November 18, 2011

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

    NUMBERSASTEXT TEXT

    01050094071094340000 abc

    743059E6 def

    November 18, 2011

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

    Correct answer
    November 18, 2011

    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

    Inspiring
    November 18, 2011

    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

    Inspiring
    November 17, 2011

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

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

    November 17, 2011

    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.

    Inspiring
    November 17, 2011

    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

    Inspiring
    November 17, 2011

    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.