Thursday, 12 September 2013

Apache POI and HashMap printing out in wrong order

Apache POI and HashMap printing out in wrong order

I am building a program that prints out info into an .xlsx file using
Apache POI and it seems to be working great, but a hiccup has developed as
I have added more lines to the print out process. I am using a HashMap
with an integer key and a string object array to store the info and then
it writes to the excel file. Everything works great until the line with
key 17. The program starts to mix up the lines and prints them out of
order. Here is the code and below that is how it prints out. Notice how
lines 17 and 18 get mixed and how the 2 blank line are left out. What am I
missing that could be causing this craziness? Thanks all!
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Estimate");
//Create a new row in current sheet
Row row = sheet.createRow(0);
//Create a new cell in current row
Cell cell = row.createCell(0);
//Set value to new value
cell.setCellValue("");
Map<Integer, Object[]>
data = new HashMap<Integer, Object[]>();
data.put(0, new Object[] {"", "SIZE 1 (in inches)", "SIZE 2
(in inches)", "PRICE"});
data.put(1, new Object[] {"P-BOARD:" });
data.put(2, new Object[] {""});
data.put(3, new Object[] {"","AMOUNT PER SQFT", "COUNT",
"PRICE"});
data.put(4, new Object[] {"SCREWS:"});
data.put(5, new Object[] {""});
data.put(6, new Object[] {"", "PRICE 50 lb BAG", "PRICE 25 lb
BAG"});
data.put(7, new Object[] {"MORTAR:"});
data.put(8, new Object[] {""});
data.put(9, new Object[] {"", "PRICE"});
data.put(10, new Object[] {"TROWEL"});
data.put(11, new Object[] {""});
data.put(12, new Object[] {"", "PRICE"});
data.put(13, new Object[] {"GROUT FLOAT"});
data.put(14, new Object[] {""});
data.put(15, new Object[] {"", "PRICE"});
data.put(16, new Object[] {"LARGE BUCKET"});
data.put(17, new Object[] {""});
data.put(18, new Object[] {""});
data.put(19, new Object[] {"TILE DIMENSION 1", "TILE DIMENSION
2", "BRAND", "PRICE"});
Set<Integer> keyset = data.keySet();
int rownum = 0;
for (Integer key : keyset)
{
Row row1 = sheet.createRow(rownum++);
Object [] objArr = data.get(key);
int cellnum = 0;
for (Object obj : objArr)
{
Cell cell1 = row1.createCell(cellnum++);
if(obj instanceof Date)
cell1.setCellValue((Date)obj);
else
if(obj instanceof Boolean)
cell1.setCellValue((Boolean)obj);
else
if(obj instanceof String)
cell1.setCellValue((String)obj);
else
if(obj instanceof Double)
cell1.setCellValue((Double)obj);
}
}

No comments:

Post a Comment