0

I have a string equal to 0100, when I generate the excel file i find this field equal to 100 . i want to keep the first value

<field name="sube" type="string" />
DevA
  • 11
  • 1

1 Answers1

0

This is just some background on what I suspect is happening here.

The behaviour you see is actually Excel that guess what the data type for a column should be and since there are no alpha numeric or any special characters in your 'sube' column it will try and convert those values to a numeric value and therefor it removes the leading zero because a leading zero in a decimal (base 10) number does not change the actual value of the number.

One solution could be to tell the BeanWriter to always quote your output data. This will then force Excel to treat all your columns and data as "text" and should not then try and interpret and guess the data type for the columns.

See the CSV Stream format properties documentation for more options, but you can set alwaysQuote = "true"

alwaysQuote - boolean - if set to true, field text is always quoted. By default, a field is only quoted if it contains a delimeter, a quotation mark or new line characters. BeanWriter, Marshaller

In your BeanIO mapping xml file add this

<stream name="myStream" format="csv">  
  <parser>
    <property name="alwaysQuote" value="true" />
  </parser>

  <!-- The rest of the mapping file content -->
</stream>

Another option is to take control of Excel on how you import the csv file to treat that specific column as text and not a number. To do that use the 'Data Import Wizard' from Excel. See this, this and this for a few possible options. There are plenty more if you google it.

nicoschl
  • 2,346
  • 1
  • 17
  • 17