Example usage for org.apache.poi.hssf.usermodel HSSFCellStyle setFont

List of usage examples for org.apache.poi.hssf.usermodel HSSFCellStyle setFont

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFCellStyle setFont.

Prototype

public void setFont(HSSFFont font) 

Source Link

Usage

From source file:org.clickframes.renderer.RequirementsGenerator.java

License:Open Source License

public static HSSFCellStyle createHeaderColumnStyle(HSSFWorkbook wb) {
    HSSFCellStyle cellStyle = wb.createCellStyle();
    HSSFFont font = wb.createFont();//w  ww.j a va 2 s .  co  m
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    cellStyle.setFont(font);

    return cellStyle;
}

From source file:org.cyberoam.iview.servlets.ExcelFileGenerator.java

License:Open Source License

void getWorkBook(ResultSetWrapper rsw, ReportBean reportBean, HSSFWorkbook wb) {
    try {/*from w ww .jav  a 2s.c om*/
        ReportColumnBean[] reportColumns = (ReportColumnBean[]) ReportColumnBean
                .getReportColumnsByReportID(reportBean.getReportId()).toArray(new ReportColumnBean[0]);
        HSSFSheet newSheet = wb.createSheet(reportBean.getTitle());
        HSSFRow row;
        HSSFCell cell;

        HSSFCellStyle cellStyle = wb.createCellStyle();
        HSSFFont fontStyle = wb.createFont();

        fontStyle.setFontHeightInPoints((short) 10);
        fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

        cellStyle.setFont(fontStyle);

        // getting number of records & fields for report
        rsw.last();
        int rowCount = rsw.getRow();
        int colCount = reportColumns.length;

        //For Freezing the first row
        newSheet.createFreezePane(0, 1, 0, 1);

        // Adding column headings to Excel
        row = newSheet.createRow((short) 0);
        for (int cCount = 0; cCount < colCount; cCount++) {
            cell = row.createCell(cCount);
            cell.setCellStyle(cellStyle);
            cell.setCellValue(reportColumns[cCount].getColumnName());
        }
        //Adding data for each record to Excel
        rsw.first();
        for (int rcount = 1; rcount <= rowCount; rcount++) {
            row = newSheet.createRow(rcount);
            for (int cCount = 0; cCount < colCount; cCount++) {
                String data = rsw.getString(reportColumns[cCount].getDbColumnName());
                cell = row.createCell(cCount);
                newSheet.autoSizeColumn((short) cCount);
                if (reportColumns[cCount].getColumnFormat() == TabularReportConstants.BYTE_FORMATTING) {
                    data = ByteInUnit.getBytesInUnit(rsw.getLong(reportColumns[cCount].getDbColumnName()));
                } else if (reportColumns[cCount].getColumnFormat() == TabularReportConstants.PROTOCOL_FORMATTING
                        && data.indexOf(':') != -1) {
                    String xdata = ProtocolBean.getProtocolNameById(
                            Integer.parseInt(rsw.getString(reportColumns[cCount].getDbColumnName()).substring(0,
                                    data.indexOf(':'))));
                    data = xdata + rsw.getString(reportColumns[cCount].getDbColumnName())
                            .substring(data.indexOf(':'), data.length());
                }
                // Setting value to the cell
                if (cCount == 0 && (data == null || "".equalsIgnoreCase(data)))
                    data = "N/A";
                if (reportColumns[cCount].getColumnFormat() == TabularReportConstants.PERCENTAGE_FORMATTING) {
                    try {
                        cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                        cell.setCellValue(Double.parseDouble(data));
                    } catch (NumberFormatException e) {
                        cell.setCellValue(data);
                    }
                } else if (rsw.getMetaData().getColumnTypeName(cCount + 1).equalsIgnoreCase("numeric")
                        && reportColumns[cCount].getColumnFormat() != TabularReportConstants.BYTE_FORMATTING) {
                    try {
                        cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                        cell.setCellValue(Integer.parseInt(data));
                    } catch (NumberFormatException e) {
                        cell.setCellValue(data);
                    }
                } else {
                    cell.setCellValue(data);
                }
            }
            rsw.next();
        }

    } catch (Exception e) {
        CyberoamLogger.appLog.error("***Error in getWorkbook function***" + e, e);
    }
}

From source file:org.deployom.core.AuditService.java

License:Open Source License

public HSSFWorkbook saveAudit() {

    // Create book
    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFCreationHelper creationHelper = workbook.getCreationHelper();

    // Default Style
    HSSFCellStyle style = workbook.createCellStyle();
    style.setWrapText(true);//from w ww .  ja  v  a 2  s  . c  o  m
    HSSFFont font = workbook.createFont();
    font.setFontName("Courier New");
    style.setFont(font);
    style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    style.setBorderTop(HSSFCellStyle.BORDER_THIN);
    style.setBorderRight(HSSFCellStyle.BORDER_THIN);
    style.setBorderLeft(HSSFCellStyle.BORDER_THIN);

    // Header Style
    HSSFCellStyle styleHeader = workbook.createCellStyle();
    styleHeader.cloneStyleFrom(style);
    styleHeader.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    font = workbook.createFont();
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    font.setColor(HSSFColor.WHITE.index);
    styleHeader.setFillForegroundColor(IndexedColors.BLACK.getIndex());
    styleHeader.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    styleHeader.setFont(font);

    // Error Style
    HSSFCellStyle styleError = workbook.createCellStyle();
    styleError.cloneStyleFrom(style);
    styleError.setFillForegroundColor(IndexedColors.CORAL.getIndex());
    styleError.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    styleError.setWrapText(true);

    // Link Style
    HSSFCellStyle styleLink = workbook.createCellStyle();
    styleLink.cloneStyleFrom(style);
    font = workbook.createFont();
    font.setUnderline(HSSFFont.U_SINGLE);
    font.setColor(IndexedColors.BLUE.getIndex());
    styleLink.setFont(font);

    // Create Summary
    HSSFSheet summarySheet = workbook.createSheet("Summary");
    int summaryRownum = 0;
    int summaryCellnum = 0;

    //Create a new row in current sheet
    Row summaryRow = summarySheet.createRow(summaryRownum++);

    // 0
    Cell summaryCell = summaryRow.createCell(summaryCellnum++);
    summaryCell.setCellValue("Job");
    summaryCell.setCellStyle(styleHeader);

    // 1
    summaryCell = summaryRow.createCell(summaryCellnum++);
    summaryCell.setCellValue("Finished");
    summaryCell.setCellStyle(styleHeader);

    // 2
    summaryCell = summaryRow.createCell(summaryCellnum++);
    summaryCell.setCellValue("Errors");
    summaryCell.setCellStyle(styleHeader);

    for (Job job : releaseService.getJobs()) {

        // Open Job
        JobService jobService = new JobService(siteService.getSiteName(), job.getJobName());

        // Create Sheet
        HSSFSheet sheet = workbook.createSheet(job.getJobName());

        int rownum = 0;
        int cellnum = 0;
        int errors = 0;

        //Create a new row in current sheet
        Row row = sheet.createRow(rownum++);

        // 0
        Cell cell = row.createCell(cellnum++);
        cell.setCellValue("Host");
        cell.setCellStyle(styleHeader);

        // 1
        cell = row.createCell(cellnum++);
        cell.setCellValue("Service");
        cell.setCellStyle(styleHeader);

        // 2
        cell = row.createCell(cellnum++);
        cell.setCellValue("Command");
        cell.setCellStyle(styleHeader);

        // 3
        cell = row.createCell(cellnum++);
        cell.setCellValue("Executable");
        cell.setCellStyle(styleHeader);

        // 4
        cell = row.createCell(cellnum++);
        cell.setCellValue("Error");
        cell.setCellStyle(styleHeader);

        // 5
        cell = row.createCell(cellnum++);
        cell.setCellValue("Output");
        cell.setCellStyle(styleHeader);

        // Check all hosts
        for (Host host : jobService.getHosts()) {

            // Check all services
            for (Service service : host.getServices()) {

                // Get a Commands
                for (Command command : service.getCommands()) {

                    //Create a new row in current sheet
                    row = sheet.createRow(rownum++);
                    cellnum = 0;

                    // 0
                    cell = row.createCell(cellnum++);
                    cell.setCellValue(host.getHostName());
                    cell.setCellStyle(style);

                    // 1
                    cell = row.createCell(cellnum++);
                    cell.setCellValue(service.getServiceName());
                    cell.setCellStyle(style);

                    // 2
                    cell = row.createCell(cellnum++);
                    cell.setCellValue(command.getTitle());
                    cell.setCellStyle(style);

                    // 3
                    cell = row.createCell(cellnum++);
                    cell.setCellValue(command.getExec());
                    cell.setCellStyle(style);

                    // 4
                    cell = row.createCell(cellnum++);
                    cell.setCellValue("N");
                    cell.setCellStyle(style);

                    // 5
                    cell = row.createCell(cellnum++);
                    if (command.getOut().length() > 1024) {
                        cell.setCellValue(command.getOut().substring(0, 1024) + "...");
                    } else {
                        cell.setCellValue(command.getOut());
                    }
                    cell.setCellStyle(style);

                    // Error
                    if (command.isError() == true) {
                        row.getCell(0).setCellStyle(styleError);
                        row.getCell(1).setCellStyle(styleError);
                        row.getCell(2).setCellStyle(styleError);
                        row.getCell(3).setCellStyle(styleError);
                        row.getCell(4).setCellStyle(styleError);
                        row.getCell(5).setCellStyle(styleError);
                        row.getCell(4).setCellValue("Y");
                        errors++;
                    }
                }
            }
        }

        // Set Size
        sheet.setColumnWidth(0, 6000);
        sheet.setColumnWidth(1, 4000);
        sheet.setColumnWidth(2, 8000);
        sheet.setColumnWidth(3, 14000);
        sheet.setColumnWidth(4, 3000);
        sheet.setColumnWidth(5, 20000);

        // Summary
        summaryRow = summarySheet.createRow(summaryRownum++);
        summaryCellnum = 0;

        // 0
        summaryCell = summaryRow.createCell(summaryCellnum++);
        summaryCell.setCellValue(job.getJobName());
        summaryCell.setCellStyle(style);

        // Set Link
        HSSFHyperlink link = creationHelper.createHyperlink(Hyperlink.LINK_DOCUMENT);
        link.setAddress("" + job.getJobName() + "!A1");
        summaryCell.setHyperlink(link);
        summaryCell.setCellStyle(styleLink);

        // 1
        summaryCell = summaryRow.createCell(summaryCellnum++);
        summaryCell.setCellValue(jobService.getJob().getFinished());
        summaryCell.setCellStyle(style);

        // 2
        summaryCell = summaryRow.createCell(summaryCellnum++);
        summaryCell.setCellValue(errors);
        summaryCell.setCellStyle(style);

        // If errors found
        if (errors > 0) {
            summaryRow.getCell(0).setCellStyle(styleError);
            summaryRow.getCell(1).setCellStyle(styleError);
            summaryRow.getCell(2).setCellStyle(styleError);
        }
    }

    // Set Summary Size
    summarySheet.setColumnWidth(0, 6000);
    summarySheet.setColumnWidth(1, 10000);
    summarySheet.setColumnWidth(2, 4000);

    // Save
    try {
        FileOutputStream out = new FileOutputStream(new File(getFileName()));
        workbook.write(out);
        out.close();
        logger.log(Level.INFO, "{0} generated successfully..", getFileName());

        return workbook;
    } catch (FileNotFoundException ex) {
        logger.log(Level.WARNING, "Audit: {0}", ex);
    } catch (IOException ex) {
        logger.log(Level.WARNING, "Audit: {0}", ex);
    }

    return null;
}

From source file:org.displaytag.export.excel.ExcelHssfView.java

License:Open Source License

/**
 * Templated method that is called for all header cells.
 *
 * @param wb the wb/* ww  w.  java 2s .  co  m*/
 * @param headerCell the header cell
 * @return the HSSF cell style
 */
public HSSFCellStyle createHeaderStyle(HSSFWorkbook wb, HeaderCell headerCell) {
    HSSFCellStyle headerStyle = getNewCellStyle();

    headerStyle.setFillPattern(CellStyle.FINE_DOTS);
    headerStyle.setFillBackgroundColor(HSSFColor.BLUE_GREY.index);
    HSSFFont bold = wb.createFont();
    bold.setBoldweight(Font.BOLDWEIGHT_BOLD);
    bold.setColor(HSSFColor.WHITE.index);
    headerStyle.setFont(bold);

    return headerStyle;
}

From source file:org.displaytag.render.HssfTableWriter.java

License:Open Source License

/**
 * @see org.displaytag.render.TableWriterTemplate#writeCaption(org.displaytag.model.TableModel)
 *//*  w w  w .  ja  va  2  s  .c o m*/
@Override
protected void writeCaption(TableModel model) throws Exception {
    HSSFCellStyle style = this.wb.createCellStyle();
    HSSFFont bold = this.wb.createFont();
    bold.setBoldweight(Font.BOLDWEIGHT_BOLD);
    bold.setFontHeightInPoints((short) 14);
    style.setFont(bold);
    style.setAlignment(CellStyle.ALIGN_CENTER);

    this.colNum = 0;
    this.currentRow = this.sheet.createRow(this.sheetRowNum++);
    this.currentCell = this.currentRow.createCell(this.colNum);
    this.currentCell.setCellStyle(style);
    String caption = model.getCaption();
    this.currentCell.setCellValue(new HSSFRichTextString(caption));
    this.rowSpanTable(model);
}

From source file:org.displaytag.render.HssfTableWriter.java

License:Open Source License

/**
 * Obtain the style used to render a header or footer.
 * @return The style used to render a header or footer.
 *///  w w w.ja  va 2 s . c  o  m
private HSSFCellStyle getHeaderFooterStyle() {
    HSSFCellStyle style = this.wb.createCellStyle();
    // style.setFillPattern(HSSFCellStyle.FINE_DOTS);
    // style.setFillBackgroundColor(HSSFColor.BLUE_GREY.index);
    HSSFFont bold = this.wb.createFont();
    bold.setBoldweight(Font.BOLDWEIGHT_BOLD);
    // bold.setColor(HSSFColor.WHITE.index);
    style.setBorderBottom(CellStyle.BORDER_THIN);
    style.setBottomBorderColor(IndexedColors.BLACK.getIndex());

    style.setFont(bold);
    return style;
}

From source file:org.egov.infra.web.displaytag.export.EGovExcelReadOnlyView.java

License:Open Source License

/**
 * @see org.displaytag.export.BinaryExportView#doExport(OutputStream)
 *//*from  w w  w. j a  v a2 s  .co m*/
@Override
public void doExport(final OutputStream out) throws JspException {
    try {
        final HSSFWorkbook wb = new HSSFWorkbook();
        wb.writeProtectWorkbook("egov", "egov");// To make the workbook read-only
        this.sheet = wb.createSheet("-");

        int rowNum = 0;
        int colNum = 0;

        if (this.header) {
            // Create an header row
            final HSSFRow xlsRow = this.sheet.createRow(rowNum++);

            final HSSFCellStyle headerStyle = wb.createCellStyle();
            headerStyle.setFillPattern(HSSFCellStyle.FINE_DOTS);
            headerStyle.setFillBackgroundColor(HSSFColor.BLUE_GREY.index);
            final HSSFFont bold = wb.createFont();
            bold.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            bold.setColor(HSSFColor.WHITE.index);
            headerStyle.setFont(bold);

            final Iterator iterator = this.model.getHeaderCellList().iterator();

            while (iterator.hasNext()) {
                final HeaderCell headerCell = (HeaderCell) iterator.next();

                String columnHeader = headerCell.getTitle();

                if (columnHeader == null) {
                    columnHeader = StringUtils.capitalize(headerCell.getBeanPropertyName());
                }

                final HSSFCell cell = xlsRow.createCell(colNum++);
                cell.setCellValue(escapeColumnValue(columnHeader));
                cell.setCellStyle(headerStyle);
            }
        }

        // get the correct iterator (full or partial list according to the exportFull field)
        final RowIterator rowIterator = this.model.getRowIterator(this.exportFull);

        // iterator on rows
        while (rowIterator.hasNext()) {
            final Row row = rowIterator.next();
            final HSSFRow xlsRow = this.sheet.createRow(rowNum++);
            colNum = 0;

            // iterator on columns
            final ColumnIterator columnIterator = row.getColumnIterator(this.model.getHeaderCellList());

            while (columnIterator.hasNext()) {
                final Column column = columnIterator.nextColumn();

                // Get the value to be displayed for the column
                final Object value = column.getValue(this.decorated);

                final HSSFCell cell = xlsRow.createCell(colNum++);

                if (value instanceof Number) {
                    final Number num = (Number) value;
                    cell.setCellValue(num.doubleValue());
                } else if (value instanceof Date) {
                    cell.setCellValue((Date) value);
                } else if (value instanceof Calendar) {
                    cell.setCellValue((Calendar) value);
                } else {
                    cell.setCellValue(escapeColumnValue(value));
                }
            }
        }

        for (short i = 0; i < colNum; i++) {
            this.sheet.autoSizeColumn(i, true);
        }
        wb.write(out);
    } catch (final Exception e) {
        throw new ExcelGenerationException(e);
    }
}

From source file:org.etudes.mneme.impl.AttachmentServiceImpl.java

License:Apache License

HSSFWorkbook createAsmtStatsSheet(HSSFWorkbook workbook, List<Submission> submissions) {
    if (submissions == null || submissions.size() == 0)
        return null;

    Map<String, Integer> userRowMap = new HashMap();
    HSSFSheet sheet = workbook.createSheet("Submission responses");

    HSSFRow headerRow = sheet.createRow((short) 0);

    HSSFCellStyle style = workbook.createCellStyle();
    HSSFFont font = workbook.createFont();
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    style.setFont(font);
    // Printing header row and question text
    HSSFCell cell0 = headerRow.createCell((short) (0));
    cell0.setCellStyle(style);/*  w  w w .  ja v a2  s  .c  o m*/
    cell0.setCellValue(this.messages.getFormattedMessage("asmt_name", null));
    HSSFCell cell1 = headerRow.createCell((short) (1));
    cell1.setCellStyle(style);
    cell1.setCellValue(this.messages.getFormattedMessage("asmt_uname", null));
    HSSFCell cell2 = headerRow.createCell((short) (2));
    cell2.setCellStyle(style);
    cell2.setCellValue(this.messages.getFormattedMessage("asmt_started", null));
    HSSFCell cell3 = headerRow.createCell((short) (3));
    cell3.setCellStyle(style);
    cell3.setCellValue(this.messages.getFormattedMessage("asmt_finished", null));
    HSSFCell cell4 = headerRow.createCell((short) (4));
    cell4.setCellStyle(style);
    cell4.setCellValue(this.messages.getFormattedMessage("asmt_status", null));
    HSSFCell cell5 = headerRow.createCell((short) (5));
    cell5.setCellStyle(style);
    cell5.setCellValue(this.messages.getFormattedMessage("asmt_ascore", null));
    HSSFCell cell6 = headerRow.createCell((short) (6));
    cell6.setCellStyle(style);
    cell6.setCellValue(this.messages.getFormattedMessage("asmt_final", null) + " "
            + this.messages.getFormattedMessage("asmt_outof", null) + " "
            + submissions.get(0).getAssessment().getPoints() + ")");
    HSSFCell cell7 = headerRow.createCell((short) (7));
    cell7.setCellStyle(style);
    cell7.setCellValue(this.messages.getFormattedMessage("asmt_released", null));

    for (Submission sub : submissions) {
        HSSFRow row;

        int rowNum = sheet.getLastRowNum() + 1;
        row = sheet.createRow(rowNum);
        try {
            User user = this.userDirectoryService.getUser(sub.getUserId());
            row.createCell((short) 0).setCellValue(user.getSortName());
            row.createCell((short) 1).setCellValue(user.getDisplayId());
        } catch (UserNotDefinedException e) {
            M_log.warn("createAsmtStatsSheet: " + e.toString());
        }
        if (sub.getStartDate() != null && !sub.getIsNonSubmit())
            row.createCell((short) 2).setCellValue(formatDate(sub.getStartDate()));
        if (sub.getSubmittedDate() != null && !sub.getIsNonSubmit())
            row.createCell((short) 3).setCellValue(formatDate(sub.getSubmittedDate()));
        row.createCell((short) 4).setCellValue(getSubmissionStatus(sub));

        if (sub.getAnswersAutoScore() != null)
            row.createCell((short) 5).setCellValue(sub.getAnswersAutoScore().floatValue());
        if (sub.getTotalScore() != null)
            row.createCell((short) 6).setCellValue(sub.getTotalScore().floatValue());
        row.createCell((short) 7).setCellValue(sub.getIsReleased().booleanValue());

    }

    return workbook;
}

From source file:org.etudes.mneme.impl.AttachmentServiceImpl.java

License:Apache License

/**
 * Creates Fill Blanks tab for answers Item Analysis
 *
 * @param fb_questions List of Fill Blanks questions
 * @param workbook Workbook object/*ww  w  . j  a va 2s  .co m*/
 * @param assessment Assessment object
 */
void createFillBlanksTab(List<Question> fb_questions, HSSFWorkbook workbook, Assessment assessment) {
    if (fb_questions == null || fb_questions.size() == 0)
        return;

    String assmtId = assessment.getId();
    HSSFSheet sheet = null;
    HSSFRow row;

    boolean headerRowDone = false;
    for (Iterator it = fb_questions.iterator(); it.hasNext();) {
        Question q = (Question) it.next();
        Map<String, Integer> fbqMap = new HashMap<String, Integer>();

        List<Answer> answers = this.submissionService.findSubmissionAnswers(assessment, q,
                FindAssessmentSubmissionsSort.userName_a, null, null);
        if (answers == null || answers.size() == 0)
            return;

        if (!headerRowDone) {
            sheet = workbook.createSheet("FillBlanks");

            HSSFRow headerRow = sheet.createRow((short) 0);

            HSSFCellStyle style = workbook.createCellStyle();
            HSSFFont font = workbook.createFont();
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            style.setFont(font);
            // Printing header row
            HSSFCell cell0 = headerRow.createCell((short) (0));
            cell0.setCellStyle(style);
            cell0.setCellValue(this.messages.getFormattedMessage("item_analysis_question", null));

            headerRowDone = true;
        }

        for (Answer answer : answers) {
            TypeSpecificAnswer a = answer.getTypeSpecificAnswer();

            if (a instanceof FillBlanksAnswerImpl) {
                FillBlanksAnswerImpl fb = (FillBlanksAnswerImpl) a;
                String[] fbAnswers = fb.getAnswers();
                for (int i = 0; i < fbAnswers.length; i++) {
                    String fbAnswer;
                    if (fb.correctFillAnswer(fbAnswers[i], i))
                        fbAnswer = "*" + fbAnswers[i] + "*";
                    else
                        fbAnswer = fbAnswers[i];
                    if (fbqMap.get(fbAnswer) != null) {
                        int count = fbqMap.get(fbAnswer).intValue();
                        count++;
                        fbqMap.put(fbAnswer, count);
                    } else
                        fbqMap.put(fbAnswer, new Integer(1));
                }
            }
        }

        int rowNum = sheet.getLastRowNum() + 1;
        row = sheet.createRow(rowNum);

        String quest_desc = stripHtml(((FillBlanksQuestionImpl) q.getTypeSpecificQuestion()).getText());
        row.createCell((short) 0).setCellValue(quest_desc);
        int j = 1;
        if (fbqMap != null && fbqMap.size() > 0) {
            Iterator itsec = fbqMap.entrySet().iterator();
            while (itsec.hasNext()) {
                Map.Entry pairs = (Map.Entry) itsec.next();
                if (pairs.getValue() != null) {
                    row.createCell((short) j).setCellValue("(" + pairs.getValue() + ") " + pairs.getKey());
                    j++;
                }
            }
        }

    }
}

From source file:org.etudes.mneme.impl.AttachmentServiceImpl.java

License:Apache License

/**
 * Creates Fill Inline tab for answers Item Analysis
 *
 * @param fb_questions List of Fill Blanks questions
 * @param workbook Workbook object//from  w ww  .ja v a 2s. com
 * @param assessment Assessment object
 */
void createFillInlineTab(List<Question> fi_questions, HSSFWorkbook workbook, Assessment assessment) {
    if (fi_questions == null || fi_questions.size() == 0)
        return;

    String assmtId = assessment.getId();
    HSSFSheet sheet = null;
    HSSFRow row;

    boolean headerRowDone = false;
    for (Iterator it = fi_questions.iterator(); it.hasNext();) {
        Question q = (Question) it.next();
        Map<String, Integer> fbqMap = new HashMap<String, Integer>();

        List<Answer> answers = this.submissionService.findSubmissionAnswers(assessment, q,
                FindAssessmentSubmissionsSort.userName_a, null, null);
        if (answers == null || answers.size() == 0)
            return;

        List<ArrayList<String>> selectionLists = new ArrayList<ArrayList<String>>();
        List<String> correctAnswers = new ArrayList<String>();

        if (!headerRowDone) {
            sheet = workbook.createSheet("InlineDropdown");

            HSSFRow headerRow = sheet.createRow((short) 0);

            HSSFCellStyle style = workbook.createCellStyle();
            HSSFFont font = workbook.createFont();
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            style.setFont(font);
            // Printing header row
            HSSFCell cell0 = headerRow.createCell((short) (0));
            cell0.setCellStyle(style);
            cell0.setCellValue(this.messages.getFormattedMessage("item_analysis_question", null));

            int i = 1;
            int k = 0;
            ((FillInlineQuestionImpl) q.getTypeSpecificQuestion()).parseSelectionLists(selectionLists,
                    correctAnswers);
            for (List<String> selList : selectionLists) {
                for (String str : selList) {
                    HSSFCell cell1 = headerRow.createCell((short) (i));
                    cell1.setCellStyle(style);
                    cell1.setCellValue(str);
                    fbqMap.put(Integer.toString(k) + str, 0);
                    i++;
                }
                k++;
            }
            headerRowDone = true;
        }

        for (Answer answer : answers) {
            TypeSpecificAnswer a = answer.getTypeSpecificAnswer();

            if (a instanceof FillInlineAnswerImpl) {
                FillInlineAnswerImpl fb = (FillInlineAnswerImpl) a;
                String[] fbAnswers = fb.getAnswers();
                for (int i = 0; i < fbAnswers.length; i++) {
                    String fbAnswer = fbAnswers[i];
                    if (fbqMap.get(Integer.toString(i) + fbAnswer) != null) {
                        int count = fbqMap.get(Integer.toString(i) + fbAnswer).intValue();
                        count++;
                        fbqMap.put(Integer.toString(i) + fbAnswer, count);
                    }
                }
            }
        }

        int rowNum = sheet.getLastRowNum() + 1;
        row = sheet.createRow(rowNum);

        String quest_desc = stripHtml(((FillInlineQuestionImpl) q.getTypeSpecificQuestion()).getText());
        row.createCell((short) 0).setCellValue(quest_desc);
        int j = 1;
        if (fbqMap != null && fbqMap.size() > 0) {
            int k = 0;
            for (List<String> selList : selectionLists) {
                for (String str : selList) {
                    if (str.equals(correctAnswers.get(k)))
                        row.createCell((short) j)
                                .setCellValue("*" + fbqMap.get(Integer.toString(k) + str) + "*");
                    else
                        row.createCell((short) j).setCellValue(fbqMap.get(Integer.toString(k) + str));
                    j++;
                }
                k++;
            }
        }

    }
}