Prasad_Joshi
Prasad_Joshi

Reputation: 662

Excel data is writing in First row only- java

I have written a code for writing data into excel using java , however , it does not increment the roe value, resulting to over-writing of data at row no.1 itself.I know problem is with rownum count-increment.

import java.util.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.*; 
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import java.io.*;

public class ExcelWriter {
static int rownum;
@Test
public void ExcelWriters(Integer i, String CableName, String Count,
    String EndCableId, String FirstCableId) {
    rownum = i;
    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet("WhileTrue");
    Map < Integer, Object[] > data = new HashMap < Integer, Object[] > (); {
        data.put(i, new Object[] {
            "Sr.no",
            "Cable",
            "Total Count",
            "EndCableId",
            "FirstCableId"
        });
        data.put(i, new Object[] {
            i,
            CableName,
            Count,
            EndCableId,
            FirstCableId
        });

        Set < Integer > keyset = data.keySet();

        for (Integer key: keyset) {
            HSSFRow row = sheet.createRow(rownum++);
            Object[] objArr = data.get(key);
            int cellnum = 0;
            for (Object obj: objArr) {
                HSSFCell cell = row.createCell(cellnum++);

                if (obj instanceof Date)
                    cell.setCellValue((Date) obj);
                else if (obj instanceof Boolean)
                    cell.setCellValue((Boolean) obj);
                else if (obj instanceof String)
                    cell.setCellValue((String) obj);
                else if (obj instanceof Double)
                    cell.setCellValue((Double) obj);
            }

        }

        try {
            FileOutputStream outputStream =

                new FileOutputStream((new File("FilePath")));
            workbook.write(outputStream);
            outputStream.close();
            System.out.println("Excel written Successfully");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

}

Upvotes: 0

Views: 819

Answers (2)

Ayush Goyal
Ayush Goyal

Reputation: 424

I only work at poi library once so I have not that much knowledge in this but by seeing your code what i think problem is-

After using this piece of code

data.put(i, new Object[] {
        "Sr.no",
        "Cable",
        "Total Count",
        "EndCableId",
        "FirstCableId"
    });

you have to increment the value of the i because when you are putting two data row to one excel row in same place

Upvotes: 0

Mustapha Belmokhtar
Mustapha Belmokhtar

Reputation: 1219

by using data.put(i,new Object[]{...} twice, the map holds only the last value for the given key i , (i.e) you overwrite your columns' names.

change the first put(i,YOUR_COL_NAMES); to put(0,YOUR_COL_NAMES);

Upvotes: 1

Related Questions