Skip to content

Generating Excel Formulas with Spring MVC and POI

I figured I’d lay this out there. It’s not awesome code, just quick and dirty. But it works.

	@Override
	protected void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {

	    //CREATE THE SHEET
		String periodDate = (String)model.get(PAYROLL_PERIOD_KEY);
	    HSSFSheet sheet = workbook.createSheet(periodDate);
	    sheet.setDefaultColumnWidth((short) 12);

	    //GETCELL: getCell(SHEET, ROW, COLUMN);
	    short currentRow = 0;

	    //WRITE ROW FOR HEADER
	    HSSFCell header0 = getCell(sheet, currentRow, NAME_COLUMN);
	    setText(header0, "NAME");

	    HSSFCell header1 = getCell(sheet, currentRow, TOTAL_BILLABLE_COLUMN);
	    setText(header1, "TOTAL BILLABLE");

	    HSSFCell header2 = getCell(sheet, currentRow, OVERTIME_COLUMN);
	    setText(header2, "OVERTIME");

	    HSSFCell header3 = getCell(sheet, currentRow, SUPPORT_COLUMN);
	    setText(header3, "SUPPORT");

	    HSSFCell header4 = getCell(sheet, currentRow, VACATION_COLUMN);
	    setText(header4, "VACATION");

	    HSSFCell header5 = getCell(sheet, currentRow, SICK_COLUMN);
	    setText(header5, "SICK");

	    Collection<payrollReportConsultantDisplay> timesheetList =
	    	(Collection<payrollReportConsultantDisplay>) model.get(PAYROLL_LIST_KEY);

	    Iterator<payrollReportConsultantDisplay> timesheetIterator = timesheetList.iterator();
	    while (timesheetIterator.hasNext())  {
	      currentRow++;
	      PayrollReportConsultantDisplay timesheet = timesheetIterator.next();
	      HSSFRow row = sheet.createRow(currentRow);
	      row.createCell(NAME_COLUMN).setCellValue(
	    		  new HSSFRichTextString(timesheet.getDisplayName()));
	      if(StringUtils.isNotEmpty(timesheet.getTotalBillableHours())) {
		      row.createCell(TOTAL_BILLABLE_COLUMN).setCellValue(
		    		  new Double(timesheet.getTotalBillableHours()).doubleValue());
	      }
	      if(StringUtils.isNotEmpty(timesheet.getOvertimeHours())) {
		      row.createCell(OVERTIME_COLUMN).setCellValue(
		    		  new Double(timesheet.getOvertimeHours()).doubleValue());
	      }
	      if(StringUtils.isNotEmpty(timesheet.getInternalSupportHours())) {
		      row.createCell(SUPPORT_COLUMN).setCellValue(
		    		  new Double(timesheet.getInternalSupportHours()).doubleValue());
	      }
	      if(StringUtils.isNotEmpty(timesheet.getVacationHours())) {
		      row.createCell(VACATION_COLUMN).setCellValue(
		    		  new Double(timesheet.getVacationHours()).doubleValue());
	      }
	      if(StringUtils.isNotEmpty(timesheet.getSickHours())) {
		      row.createCell(SICK_COLUMN).setCellValue(
		    		  new Double(timesheet.getSickHours()).doubleValue());
	      }
	    }
            //Provide a formula for totals
	    HSSFRow row = sheet.createRow(++currentRow);
	    row.createCell(NAME_COLUMN).setCellValue(new HSSFRichTextString("Totals:"));
	    row.createCell(TOTAL_BILLABLE_COLUMN).setCellFormula("SUM(B1:B" + (currentRow -1) + ")");
	    row.createCell(OVERTIME_COLUMN).setCellFormula("SUM(C1:C" + (currentRow -1) + ")");
	    row.createCell(SUPPORT_COLUMN).setCellFormula("SUM(D1:D" + (currentRow -1) + ")");
	    row.createCell(VACATION_COLUMN).setCellFormula("SUM(E1:E" + (currentRow -1) + ")");
	    row.createCell(SICK_COLUMN).setCellFormula("SUM(F1:F" + (currentRow -1) + ")");
	}

Hat tip to Zabada Technologies.
Hat tip to softartisans.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

One Comment

  1. Dan wrote:

    You could also use a hands free time captures software like MetriQ.

    Sunday, May 18, 2008 at 5:37 pm | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*