Cael
Cael

Reputation: 556

Writing Data into Excel spreadsheet using Apache POI

I have a Java program to add data into new row in Excel spreadsheet using Apache POI. I am able to print the first 10 row to the spreadsheet but when I tried to print the 11th row, it will somehow create a new row on the first row (without continuing from the previous index) like this:

11  Jane    300   //??
12  Jane    300   //??
Emp No. Name    Salary
1   Jane    300
2   Jane    300
3   Jane    300
4   Jane    300
5   Jane    300
6   Jane    300
7   Jane    300
8   Jane    300
9   Jane    300
10  Jane    300

Notice 11th and 12th on top? How do I fix them? This is my complete Java code:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;

public class Excel {
    public static void main(String[] args) throws Exception {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("Sample sheet");

        Map<String, Object[]> data = new HashMap<String, Object[]>();
        data.put("0", new Object[] {"Emp No.", "Name", "Salary"});

        for (int i=1;i<13;i++){

            data.put(i + "", new Object[] {i, "Jane", "300"}); //print in different rows
       }

        Set<String> keyset = data.keySet();
        int rownum = 0;
        for (String key : keyset) {
            Row row = sheet.createRow(rownum++);
            Object [] objArr = data.get(key);
            int cellnum = 0;
            for (Object obj : objArr) {
                Cell cell = row.createCell(cellnum++);
                if(obj instanceof Integer) 
                    cell.setCellValue((Integer)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 out = 
                new FileOutputStream(new File("new.xls"));
        workbook.write(out);
        out.close();
        System.out.println("Excel written successfully..");

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

    }

}

Upvotes: 1

Views: 2695

Answers (2)

Manikant Gautam
Manikant Gautam

Reputation: 3581

Your Keyset contains data in order

              [11, 12, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Try to sort these data accoording to your requirement.

Upvotes: 0

Thilo
Thilo

Reputation: 262464

That happens because you put your row numbers into a HashMap, which does not give them back in a useful order.

Map<String, Object[]> data = new HashMap<String, Object[]>();
data.put("0", new Object[] {"Emp No.", "Name", "Salary"});

Why is this a map? Better to use a

List<Object[]> rows = new ArrayList<>();
rows.add(new Object[] {"Emp No.", "Name", "Salary"});

A list will keep things in the same order you added them.

Upvotes: 1

Related Questions