at.fh.swenga.firefighters.report.ExcelFireEngineReportView.java Source code

Java tutorial

Introduction

Here is the source code for at.fh.swenga.firefighters.report.ExcelFireEngineReportView.java

Source

package at.fh.swenga.firefighters.report;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.servlet.view.document.AbstractXlsxView;

import at.fh.swenga.firefighters.model.FireEngineModel;

public class ExcelFireEngineReportView extends AbstractXlsxView {

    @Override
    protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        List<FireEngineModel> fireEngines = (List<FireEngineModel>) model.get("fireEngines");

        // create a worksheet
        Sheet sheet = workbook.createSheet("FireEngine Report");

        // create style for header cells
        CellStyle style = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setFontName("Arial");
        style.setFillForegroundColor(HSSFColor.BLUE.index);
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        font.setColor(HSSFColor.WHITE.index);
        style.setFont(font);

        // create a new row in the worksheet
        Row headerRow = sheet.createRow(0);

        // create a new cell in the row
        Cell cell0 = headerRow.createCell(0);
        cell0.setCellValue("ID");
        cell0.setCellStyle(style);

        // create a new cell in the row
        Cell cell1 = headerRow.createCell(1);
        cell1.setCellValue("Modell");
        cell1.setCellStyle(style);

        // create a new cell in the row
        Cell cell2 = headerRow.createCell(2);
        cell2.setCellValue("Kennzeichen");
        cell2.setCellStyle(style);

        // create a new cell in the row
        Cell cell3 = headerRow.createCell(3);
        cell3.setCellValue("Leistung");
        cell3.setCellStyle(style);

        // create a new cell in the row
        Cell cell4 = headerRow.createCell(4);
        cell4.setCellValue("Baujahr");
        cell4.setCellStyle(style);

        // create a new cell in the row
        Cell cell5 = headerRow.createCell(5);
        cell5.setCellValue("Aktiv");
        cell5.setCellStyle(style);

        // create a new cell in the row
        Cell cell6 = headerRow.createCell(6);
        cell6.setCellValue("Funktion");
        cell6.setCellStyle(style);

        // create a new cell in the row
        Cell cell7 = headerRow.createCell(7);
        cell7.setCellValue("Feuerwehr");
        cell7.setCellStyle(style);

        // create multiple rows with fireEngines data
        int rowNum = 1;
        for (FireEngineModel fireEngine : fireEngines) {
            // create the row data
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(fireEngine.getId());
            row.createCell(1).setCellValue(fireEngine.getModel());
            row.createCell(2).setCellValue(fireEngine.getLicensePlate());
            row.createCell(3).setCellValue(fireEngine.getPerformance());
            row.createCell(4).setCellValue(fireEngine.getBuildYear());
            row.createCell(5).setCellValue(fireEngine.getActive());
            row.createCell(6).setCellValue(fireEngine.getAbbreviation().getAbbreviation());
            row.createCell(7).setCellValue(fireEngine.getFireBrigade().getName());
        }

        // adjust column width to fit the content
        sheet.autoSizeColumn((short) 0);
        sheet.autoSizeColumn((short) 1);
        sheet.autoSizeColumn((short) 2);
        sheet.autoSizeColumn((short) 3);
        sheet.autoSizeColumn((short) 4);
        sheet.autoSizeColumn((short) 5);
        sheet.autoSizeColumn((short) 6);
        sheet.autoSizeColumn((short) 7);

    }

}