0
votes

I'm following the Apache POI – Spreadsheets tutorial from this site: https://www.tutorialspoint.com/apache_poi/apache_poi_spreadsheets.htm

Everything works fine, but when I try to add more than 10 rows, all rows after 9th will be inserted back from the 2nd row. I am new in JAVA, is there anything wrong with the loop to iterate the data or something else? Please help!

I used the entire code from the tutorial but added some more lines to create more rows:

package testing;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Testing {

    /**
     * @param args the command line arguments
     * @throws java.lang.Exception
     */



   public static void main(String[] args) throws Exception 
   {
      //Create blank workbook
      XSSFWorkbook workbook = new XSSFWorkbook(); 
      //Create a blank sheet
      XSSFSheet spreadsheet = workbook.createSheet( 
      " Employee Info ");
      //Create row object
      XSSFRow row;
      //This data needs to be written (Object[])
      Map < String, Object[] > empinfo = 
      new TreeMap < String, Object[] >();
      empinfo.put( "1", new Object[] { 
      "EMP ID", "EMP NAME", "DESIGNATION" });
      empinfo.put( "2", new Object[] { 
      "tp01", "Gopal", "Technical Manager" });
      empinfo.put( "3", new Object[] { 
      "tp02", "Manisha", "Proof Reader" });
      empinfo.put( "4", new Object[] { 
      "tp03", "Masthan", "Technical Writer" });
      empinfo.put( "5", new Object[] { 
      "tp04", "Satish", "Technical Writer" });
      empinfo.put( "6", new Object[] { 
      "tp05", "Krishna", "Technical Writer" });
      empinfo.put( "7", new Object[] { 
      "tp06", "Krishna", "Technical Writer" });
      empinfo.put( "8", new Object[] { 
      "tp07", "Krishna", "Technical Writer" });
      empinfo.put( "9", new Object[] { 
      "tp08", "Krishna", "Technical Writer" });
      empinfo.put( "10", new Object[] { 
      "tp09", "Krishna", "Technical Writer" });
      empinfo.put( "11", new Object[] { 
      "tp10", "Krishna", "Technical Writer" });

      //Iterate over data and write to sheet
      Set < String > keyid = empinfo.keySet();
      int rowid = 0;
      for (String key : keyid)
      {
         row = spreadsheet.createRow(rowid++);
         Object [] objectArr = empinfo.get(key);
         int cellid = 0;
         for (Object obj : objectArr)
         {
            Cell cell = row.createCell(cellid++);
            cell.setCellValue((String)obj);
         }
      }
      //Write the workbook in file system
      FileOutputStream out = new FileOutputStream( 
      new File("Writesheet.xlsx"));
      workbook.write(out);
      out.close();
      System.out.println( 
      "Writesheet.xlsx written successfully" );
   }
}

If I run the code, the generated *.xlsx file will be: example.xlsx

Anyway to make it works normally?

1

1 Answers

2
votes

Treemap sorts the keys that's why the final result is different than expected. Try using linkedhashmap which saves the insertion order.