John Joe
John Joe

Reputation: 12803

Add 0 in front of the value in excel by java

Is there a way to add 0 in front of the value ? In database record, I have a value with 00037.When it written to excel, I get 37, instead of 00037.

  public static final String REF_NO = "REF_NO";
  custom.setRptDailyTxnRefNo(rs.getString(REF_NO));
  System.out.print(rs.getString(REF_NO)) 

Please let me know if you need more code.

The file is in .xls.

Upvotes: 0

Views: 215

Answers (2)

John Joe
John Joe

Reputation: 12803

Apart from the answer given by @Xtreme, I found another one which does not require '

String abc = 0003;
formatToCVSValue(abc);

public static String formatToCVSValue(String s)
    {
        if(s==null || !hasLength(s))
            return "";
        else
        {
            return "\"=\"\""+s+"\"\"\"";
        }
    }

When open in excel, you will see exactly 0003 instead of '0003

Upvotes: 0

XtremeBaumer
XtremeBaumer

Reputation: 6435

So I just found an easier solution than changing the cellformat (which also works fine). You can simply write '00037 in excel to have 00037 displayed. Just add the apostrophe at the beginning

Upvotes: 2

Related Questions