4

毎回異なる列数を持つJasperDesignとして動的にレポートを作成します。
列数が多すぎて 1 ページに表示できない場合があります。この余分な列と最初の 2 つの列を、印刷時に新しいページに配置したいと考えています。

私はこれを試しましたcolumnHeaderband.setSplitType(JRBand.SPLIT_TYPE_IMMEDIATE);

JasperReports 5.4.5Java 1.7を使用しています

誰かがそれを行う方法を知っていますか?

public class CarPlanDynamicReportCreator {
    private static JRDataSource dataSource;
    private static List<Map<String, String>> listOfExtractedMap;
    private static final Boolean EMPTY_BAND = true;
    private static List<String> fieldNameList;
    private static List<String> remarkList;

    public static void compile(DefaultTableModel tableModel, String title, String name, String id) throws JRException {
        dataSource = createReportDataSource(tableModel);
        JasperDesign jasperDesign = getJasperDesign(tableModel);
        Map<String, Object> parameters = retrieveParameter(title, name, id);

        JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource);
        Report report = new Report(jasperPrint);
        report.viewWithMaximumWindowSize();
    }

    public static JasperDesign getJasperDesign(DefaultTableModel tableModel) throws JRException {
        //JasperDesign
        JasperDesign jasperDesign = new JasperDesign();
        jasperDesign.setName("The dynamically generated report");
        jasperDesign.setPageWidth(842);
        jasperDesign.setPageHeight(595);
        jasperDesign.setOrientation(JRReport.ORIENTATION_LANDSCAPE);
        jasperDesign.setColumnWidth(802);
        jasperDesign.setColumnSpacing(2);
        jasperDesign.setLeftMargin(20);
        jasperDesign.setRightMargin(20);
        jasperDesign.setTopMargin(20);
        jasperDesign.setBottomMargin(20);
        jasperDesign.setTitleNewPage(false);
        jasperDesign.setFloatColumnFooter(true);
        jasperDesign.setSummaryNewPage(false);

        jasperDesign.addStyle(JasperDesignUtil.retrieveNormalStyle());
        jasperDesign.addStyle(JasperDesignUtil.retrieveBoldStyle());
        jasperDesign.addStyle(JasperDesignUtil.retrieveItalicStyle());

        //Parameters
        JasperDesignUtil.addParameters(jasperDesign, tableModel);

        //Fields
        JasperDesignUtil.addFields(jasperDesign, tableModel, fieldNameList);

        //Variables
        JasperDesignUtil.addVariables(jasperDesign, tableModel);

        //Page header
        JasperDesignUtil.addPageHeader(jasperDesign, tableModel, !EMPTY_BAND);

        //Column header
        JasperDesignUtil.addColumnHeader(jasperDesign, tableModel, !EMPTY_BAND);

        //Detail band
        JasperDesignUtil.addDetail(jasperDesign, tableModel, listOfExtractedMap, !EMPTY_BAND);

        //Page footer
        JasperDesignUtil.addPageFooter(jasperDesign, tableModel, !EMPTY_BAND);

        //Summary
        JasperDesignUtil.addSummary(jasperDesign, tableModel, remarkList, !EMPTY_BAND);

        return jasperDesign;
    }

    private static JRDataSource createReportDataSource(DefaultTableModel tableModel) {
        Map[] reportRows = initializeMapArray(tableModel);
        return new JRMapArrayDataSource(reportRows);
    }

    public static Map<String, Object> retrieveParameter(String title, String name, String id) {
        Map<String, Object> parameterMap = GenericsUtil.makeMap();
        parameterMap.put("TITLE", title);
        parameterMap.put("NAME", patient);
        parameterMap.put("ID", patientId);

        return parameterMap;
    }
}

私のユーティリティクラス:

public class JasperDesignUtil {
    private static JRDesignBand band;
    private static JRDesignField field;
    private static JRDesignTextField textField;
    private static JRDesignStaticText staticText;
    private static JRDesignExpression expression;

    // add Parameters
    public static void addParameters(JasperDesign jasperDesign, DefaultTableModel tableModel) throws JRException {
        JRDesignParameter parameter = new JRDesignParameter();
        parameter.setName("TITLE");
        parameter.setValueClass(java.lang.String.class);
        JRDesignExpression expression = new JRDesignExpression();
        expression.setText("");
        expression.setValueClass(java.lang.String.class);
        parameter.setDefaultValueExpression(expression);
        jasperDesign.addParameter(parameter);

        parameter = new JRDesignParameter();
        parameter.setName("NAME");
        parameter.setValueClass(java.lang.String.class);
        expression = new JRDesignExpression();
        expression.setText("");
        expression.setValueClass(java.lang.String.class);
        parameter.setDefaultValueExpression(expression);
        jasperDesign.addParameter(parameter);

        parameter = new JRDesignParameter();
        parameter.setName("ID");
        parameter.setValueClass(java.lang.String.class);
        expression = new JRDesignExpression();
        expression.setText("");
        expression.setValueClass(java.lang.String.class);
        parameter.setDefaultValueExpression(expression);
        jasperDesign.addParameter(parameter);
    }

    // Add Fields
    public static void addFields(JasperDesign jasperDesign, DefaultTableModel tableModel, List<String> fieldsName) throws JRException {
        if (!fieldsName.isEmpty()) {
            for (String name : fieldsName) {
                field = new JRDesignField();
                field.setName(name.replaceAll("\n", " "));
                field.setValueClass(String.class);
                jasperDesign.addField(field);
            }
        }
    }

    // Add Variables
    public static void addVariables(JasperDesign jasperDesign, DefaultTableModel tableModel) throws JRException {
        variable = new JRDesignVariable();
        variable.setName("COLUMNS");
        variable.setValueClass(java.lang.Integer.class);
        variable.setCalculation(JRVariable.CALCULATION_COUNT);
        expression = new JRDesignExpression();
        expression.setValueClass(java.lang.Integer.class);
        expression.setText("$V{COLUMN_COUNT}+1");
        variable.setInitialValueExpression(expression);
        variable.setResetType(JRVariable.RESET_TYPE_PAGE);
        jasperDesign.addVariable(variable);
    }

    // Add ColumnHeader
    public static void addColumnHeader(JasperDesign jasperDesign, DefaultTableModel tableModel, Boolean emptyBand) throws JRException {
        if (emptyBand)
            jasperDesign.setColumnHeader(retrieveEmptyBand(jasperDesign, 5));
        else {
            band = new JRDesignBand();
            band.setHeight(25);
            band.setSplitType(JRBand.SPLIT_TYPE_STRETCH);
            int headerPosition = 0;
            int columnCounter = 0;

            for (JRField field : jasperDesign.getFields()) {
                String fieldValue = field.getName();
                String[] values = fieldValue.split(" ");
                if (values.length != 1) {
                    String date = values[0];
                    String time = values[1];
                    fieldValue = "<html>".concat(date).concat("<BR>").concat(time).concat("</html>");
                } else
                    fieldValue = "<html>".concat(fieldValue).concat("</html>");

                int fieldLength = fieldValue.length();
                headerPosition = columnCounter == 0 ? headerPosition : (columnCounter == 1 ? headerPosition + 100 : headerPosition + fieldLength);

                JRDesignStaticText headerStaticText = new JRDesignStaticText();
                headerStaticText.setX(headerPosition);
                headerStaticText.setY(0);
                headerStaticText.setWidth(columnCounter == 0 ? 100 : (columnCounter == 1 ? 34 : fieldLength + 7));
                headerStaticText.setHeight(band.getHeight());
                headerStaticText.setMode(JRElement.MODE_TRANSPARENT);
                headerStaticText.setText(fieldValue);
                headerStaticText.setPrintRepeatedValues(true);
                headerStaticText.setPositionType(JRElement.POSITION_TYPE_FIX_RELATIVE_TO_TOP);
                headerStaticText.setFontSize(8);
                headerStaticText.setBold(true);
                headerStaticText.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_CENTER);
                headerStaticText.setVerticalAlignment(JRAlignment.VERTICAL_ALIGN_MIDDLE);
                headerStaticText.setMarkup("html");
                headerStaticText.getLineBox().setLeftPadding(2);
                headerStaticText.getLineBox().setRightPadding(2);

                columnCounter++;
                band.addElement(headerStaticText);
            }
            jasperDesign.setColumnHeader(band);
        }
    }

    // Add Detail
    public static void addDetail(JasperDesign jasperDesign, DefaultTableModel tableModel, List<Map<String, String>> maps, Boolean emptyBand) throws JRException {
        if (emptyBand)
            ((JRDesignSection) jasperDesign.getDetailSection()).addBand(retrieveEmptyBand(jasperDesign, 5));
        else {
            band = new JRDesignBand();
            band.setHeight(16);
            int position = 0;
            Map<String, String> headerMap = maps.get(0);
            int columnCounter = 0;
            for (Map.Entry<String, String> entry : headerMap.entrySet()) {
                //creating cell field on the detail band
                String cellValue = "$F{".concat(entry.getKey().replaceAll("\n", " ")).concat("}");
                int cellLength = cellValue.length();
                position = columnCounter == 0 ? position : (columnCounter == 1 ? position + 100 : position + 27);

                JRDesignTextField cellTextField = new JRDesignTextField();
                cellTextField.setX(position);
                cellTextField.setY(0);
                cellTextField.setWidth(columnCounter == 0 ? 100 : (columnCounter == 1 ? 34 : 27));
                cellTextField.setHeight(band.getHeight());
                cellTextField.setPrintRepeatedValues(true);
                cellTextField.setPositionType(JRElement.POSITION_TYPE_FIX_RELATIVE_TO_TOP);
                cellTextField.setFontSize(8);
                cellTextField.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_CENTER);
                cellTextField.setHorizontalAlignment(columnCounter == 0 ? JRAlignment.HORIZONTAL_ALIGN_LEFT : JRAlignment.HORIZONTAL_ALIGN_CENTER);
                cellTextField.setVerticalAlignment(JRAlignment.VERTICAL_ALIGN_MIDDLE);
                cellTextField.setStretchType(JRElement.STRETCH_TYPE_RELATIVE_TO_TALLEST_OBJECT);
                cellTextField.setStretchWithOverflow(true);
                cellTextField.setPositionType(JRElement.POSITION_TYPE_FLOAT);

                JRDesignExpression cellExpression = new JRDesignExpression();
                cellExpression.setValueClass(String.class);
                cellExpression.setText(cellValue);
                cellTextField.setExpression(cellExpression);

                if (columnCounter == 0)
                    cellTextField.getLineBox().getRightPen().setLineWidth(1);
                if (columnCounter != 0) {
                    cellTextField.setBold(true);
                    if (columnCounter % 2 != 0) {
                        cellTextField.setBackcolor(UIWebLook.secondary_4);
                        cellTextField.setMode(JRElement.MODE_OPAQUE);
                    }
                }
                band.addElement(cellTextField);
                columnCounter++;
            }
            band.addElement(retrieveLine(0, 0, jasperDesign.getColumnWidth(), 0, JRGraphicElement.FILL_SOLID));
            ((JRDesignSection) jasperDesign.getDetailSection()).addBand(band);
        }
    }

    private static JRDesignBand retrieveEmptyBand(JasperDesign jasperDesign, int height) {
        band = new JRDesignBand();
        band.setHeight(height);
        band.addElement(retrieveLine(0, 0, jasperDesign.getColumnWidth(), 0, JRGraphicElement.FILL_SOLID));

        return band;
    }

    public static JRDesignElement retrieveLine(int positionX, int positionY, int width, int height, @Nullable Byte fill) {
        JRDesignLine line = new JRDesignLine();
        line.setX(positionX);
        line.setY(positionY);
        line.setWidth(width);
        line.setHeight(height);
        line.setBackcolor(Color.white);
        line.setForecolor(Color.black);
        line.setPositionType(JRElement.POSITION_TYPE_FLOAT);
        line.setFill(fill == null ? JRGraphicElement.PEN_NONE : fill);

        return line;
    }
}
4

1 に答える 1

0

サブレポートを使用してください。これが最善の方法です。

于 2015-03-06T09:22:17.623 に答える