1

How to add a zero in the last digit of excel file ?

I print below value by code below

DecimalFormat df = new DecimalFormat("#.00"); 
System.out.println(s.getNetAmount());
System.out.println(df.format(s.getNetAmount()));

and the result was

691.200
691.20

But when I write it into excel file, I expect to get 691.20, but I get 691.2.

This is how I write to excel file

      public void write(List<? extends List> list) throws Exception {

            if (list != null) {
                try {
                    writeFile(header(), detail(list.get(0)));
                } catch (BatchRunException ex) {
                    throw new BatchRunException(" Fail..." + ex);
                }
            }
        }

 private void writeFile(String header, List<String> detail) throws IOException, BatchRunException {

        String fullPath = outputPath + fileNameFormat;

        File file = new File(fullPath);
        File path = new File(outputPath);

        if (!path.exists()) {
            path.mkdirs();
        }

        if (!file.exists()) {
            file.createNewFile();
        }

        try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
            bw.write(header);
            bw.write(NEW_LINE);

            for (String s : detail) {

                bw.write(s);
                bw.write(NEW_LINE);
            }


        } catch (IOException ex) {
            throw new BatchRunException(" Fail..." + ex);
        }
    }

    private String header() {
        StringBuilder bf = new StringBuilder();

        bf.append("Employee Name").append(SEPERATOR);
        bf.append("Amount").append(SEPERATOR);
        return bf.toString();
    }

    private List<String> detail(List<SettlementList> dList) {
        List<String> list = new ArrayList();
        BigDecimal a = new BigDecimal("0");

        for (SettlementList s : dList) {
            StringBuilder bf = new StringBuilder();

            bf.append(s.Name()).append(SEPERATOR);
            bf.append(s.getNetAmount()).append(SEPERATOR); // here the error
            list.add(bf.toString());    
        }

        return list;
    }
AI.
  • 934
  • 2
  • 14
  • 30

2 Answers2

2

It's because of Cell style and not DecimalFormat. Try setting the cell style, e.g.:

HSSFCell cell = row.createCell(0);
HSSFCellStyle style = workbook.createCellStyle();
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
cell.setCellValue(s.getNetAmount().doubleValue());

Update

It looks like you are writing into csv file and trying to open it with xls. In this case, Excel will format the cells based on the data. So, for rows with values like 691.200, Excel will interpret these as numeric values and format it accordingly (making it 691.2). There are two ways to solve it:

  1. Write to xls instead of csv with Apache POI library (here is an example) and apply the cell format as per the code snippet above
  2. Write to csv with these numbers as Strings, open it in excel and apply Text formatting for corresponding columns.
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
0

format your cell with this configuration

select your cell and right click on it , then click on format cell and do this configuration then clik ok