/** Sets values of items into cells of output file */ private void setValues() { int lastRow = 0; for (Container c : finalContainers) { List<Item> writeItems = c.getList(); for (int i = 0; i < writeItems.size(); i++) { // place in excell file from where to write // items of the next container lastRow++; Row row = outputSheet.createRow(lastRow); Item item = writeItems.get(i); Object[] values = new Object[] { // array to store item's values item.getName(), // 0 item.getPrice(), // 1 item.getNumOfItems(), // 2 item.getItemsInPack(), // 3 item.getNumOfPacks(), // 4 item.getNetWeightOfPack(), // 5 item.getWeightOfPack(), // 6 item.getVolumeOfPack(), // 7 item.getSumNetWeight(), // 8 item.getSumWeight(), // 9 item.getSumVolume() }; // 10 // setting name separately, because of String type value Cell name = row.createCell(0); name.setCellValue((String) values[0]); for (int j = 1; j < values.length; j++) { Cell cell = row.createCell(j); // checking type of number of packs variable it should be int if (j == 4) cell.setCellValue((double) values[j]); else cell.setCellValue((double) values[j]); } // checking if end of items list in the containers is reached // and adding +2 empty rows if (i == (writeItems.size() - 1)) { lastRow += 1; // +2 empty spaces for better appearance setReport(c, lastRow); lastRow += 4; } } } }
/** * Forms report in excel file. Sum weight. Sum volume etc. * * @param sheet Sheet where write the items * @param c Container from where take the info * @param lastRow last row after all items were written method uses lastRow + 2 to write report in * next 2 rows after all items of the container were written */ private void setReport(Container c, int lastRow) { String[] reportHeadings = { "Суммарный вес", // 6 "Суммарный объем", // 7 "Остаток вес", // 8 "Остаток объем" // 9 }; double[] values = { c.getWeight(), c.getVolume(), c.getWeightLimit() - c.getWeight(), c.getVolumeLimit() - c.getVolume() }; int dataCell = 5; int valuesIndex = 0; // index of the array of doubles(weigh, volume, etc) lastRow++; Row headings = outputSheet.createRow(lastRow); lastRow += 2; Row data = outputSheet.createRow(lastRow); for (String report : reportHeadings) { Cell heading = headings.createCell(dataCell); heading.setCellValue(report); Cell dataCellValue = data.createCell(dataCell); dataCellValue.setCellValue(values[valuesIndex]); dataCell++; valuesIndex++; } }