Writing Data into Excel spreadsheet using Apache POI
<p>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:</p>

<pre><code>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
</code></pre>

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

<pre><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&lt;String, Object[]&gt; data = new HashMap&lt;String, Object[]&gt;();
        data.put("0", new Object[] {"Emp No.", "Name", "Salary"});

        for (int i=1;i&lt;13;i++){

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

        Set&lt;String&gt; 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();
    }

    }

}
</code></pre>