/** * This function re-computes the total number of hours for the selected period. It is ran every * time the user edits a text field specifying the number of hours for a particular top-level * project. Percentage labels are also updated. */ private void recomputeTotal() { double total = 0; for (Row row : rows.values()) { try { row.hours = Double.parseDouble(row.hoursTF.getText()); total += row.hours; row.hoursTF.setForeground(normalColour); } catch (NumberFormatException e) { row.hoursTF.setForeground(errorColour); totalLabel.setText("ERROR"); totalLabel.setForeground(errorColour); return; } } totalLabel.setText(decimalFormat.format(total)); totalLabel.setForeground(normalColour); for (Row row : rows.values()) { String percentS = decimalFormat.format(total == 0 ? 0 : 100 * row.hours / total); row.percentL.setText("(" + percentS + "%)"); } pack(); }
private void saveExcelPoject(File file) throws IOException { Workbook wb = new XSSFWorkbook(); Sheet sheet = wb.createSheet("timeplan"); // Заголовок в 0 строке Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("Филиал"); cell = row.createCell(1); cell.setCellValue("Город"); Calendar cal = Calendar.getInstance(); cal.set(2017, 0, 5); // Начальная дата проекта SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yy"); for (int i = 0; i < 3 * 52; i++) { // Счетчик по неделям cell = row.createCell(i + 2); cell.setCellValue(sdf.format(cal.getTime())); cal.add(Calendar.WEEK_OF_YEAR, 1); // Следующая неделя } // sheet.setColumnWidth(0, 256); // Цвета ячеек CellStyle[] styles = new CellStyle[6]; styles[0] = wb.createCellStyle(); styles[0].setFillForegroundColor(HSSFColor.RED.index); styles[0].setFillPattern(FillPatternType.SOLID_FOREGROUND); styles[1] = wb.createCellStyle(); styles[1].setFillForegroundColor(HSSFColor.GREEN.index); styles[1].setFillPattern(FillPatternType.SOLID_FOREGROUND); styles[2] = wb.createCellStyle(); styles[2].setFillForegroundColor(HSSFColor.BLUE.index); styles[2].setFillPattern(FillPatternType.SOLID_FOREGROUND); styles[3] = wb.createCellStyle(); styles[3].setFillForegroundColor(HSSFColor.ROSE.index); styles[3].setFillPattern(FillPatternType.SOLID_FOREGROUND); styles[4] = wb.createCellStyle(); styles[4].setFillForegroundColor(HSSFColor.LIGHT_BLUE.index); styles[4].setFillPattern(FillPatternType.SOLID_FOREGROUND); styles[5] = wb.createCellStyle(); styles[5].setFillForegroundColor(HSSFColor.LIGHT_GREEN.index); styles[5].setFillPattern(FillPatternType.SOLID_FOREGROUND); short rowIdx = 0; for (Region region : this.regions) { row = sheet.createRow(++rowIdx); cell = row.createCell(0); cell.setCellValue(region.filial); cell = row.createCell(1); cell.setCellValue(region.name); cal = Calendar.getInstance(); cal.set(2017, 0, 5); // Начальная дата проекта for (int i = 0; i < 3 * 52; i++) { // Счетчик по неделям short color = region.getDateColorIndex(cal.getTime()); if (color >= 0) { cell = row.createCell(i + 2); cell.setCellStyle(styles[color]); } cal.add(Calendar.WEEK_OF_YEAR, 1); // Следующая неделя } } try (FileOutputStream fileOut = new FileOutputStream(file)) { wb.write(fileOut); } }