Иногда попадаются такие отчёты, в которых количество колонок зависит от переданных параметров или данных. В этом случае придётся генерировать шаблон программно. В этом нам помогут классы, расположенные в пакете net.sf.jasperreports.engine.design:
JRDesignStaticText — статический текст.
JRDesignTextField — эквивалентвно TextField в JasperStudio.
JRDesignBand — Title, Details, Summary и прочие band.
JRDesignExpression — вычисляемое выражение для TextField-ов, переменных и прочего.
JRDesignLine — для рисования линий.
JRDesignField — поля отчёта
JRDesignVariable — для подсчёта суммы, количества, среднего значения и т. д.
JRDesignConditionalStyle — для задания условного форматирования.
Также нам понадобится класс для указания ширины и стиля рамок из пакета net.sf.jasperreports.engine:
JRLineBox.
Пример простого отчёта:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
package ru.urvanov.javaexamples.jasperreportsdynamiccolumns; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sf.jasperreports.engine.JRDataSource; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRLineBox; import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.data.JRMapCollectionDataSource; import net.sf.jasperreports.engine.design.JRDesignBand; import net.sf.jasperreports.engine.design.JRDesignConditionalStyle; import net.sf.jasperreports.engine.design.JRDesignExpression; import net.sf.jasperreports.engine.design.JRDesignField; import net.sf.jasperreports.engine.design.JRDesignLine; import net.sf.jasperreports.engine.design.JRDesignParameter; import net.sf.jasperreports.engine.design.JRDesignSection; import net.sf.jasperreports.engine.design.JRDesignStaticText; import net.sf.jasperreports.engine.design.JRDesignStyle; import net.sf.jasperreports.engine.design.JRDesignTextField; import net.sf.jasperreports.engine.design.JRDesignVariable; import net.sf.jasperreports.engine.design.JasperDesign; import net.sf.jasperreports.engine.export.JRXlsExporter; import net.sf.jasperreports.engine.type.CalculationEnum; import net.sf.jasperreports.engine.type.ResetTypeEnum; import net.sf.jasperreports.engine.type.WhenNoDataTypeEnum; import net.sf.jasperreports.export.SimpleExporterInput; import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput; import net.sf.jasperreports.export.SimpleXlsReportConfiguration; /** * Пример генерации отчёта в JasperReports программно. * @author urvanov * */ public class App { public static void main(String[] args) throws JRException { try { generateReport(); } catch (Exception e) { e.printStackTrace(); } } public static void generateReport() throws JRException, IOException { JasperDesign jasperDesign = createDesign(); JasperReport jasperReport = JasperCompileManager .compileReport(jasperDesign); JRDataSource jrDataSource = prepareDataSource(); Map<String, Object> params = new HashMap<String, Object>(); params.put("startDate", new Date()); params.put("endDate", new Date()); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, jrDataSource); try (FileOutputStream baos = new FileOutputStream("dynamicReport.xls")) { JRXlsExporter xlsExporter = new JRXlsExporter(); xlsExporter.setExporterInput(new SimpleExporterInput(jasperPrint)); xlsExporter.setExporterOutput(new SimpleOutputStreamExporterOutput( baos)); SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration(); configuration.setOnePagePerSheet(false); xlsExporter.setConfiguration(configuration); xlsExporter.exportReport(); } } /** * Здесь нам нужно развернуть данные, полученные из хранимой процедуры в * список объектов с нужным количеством полей. В качестве примера мы просто * создадим тестовые данные. * * @return JRDataSource. */ private static JRDataSource prepareDataSource() { List<Map<String, ?>> preparedData = new ArrayList<Map<String, ?>>(); Map<String, Object> map; map = new HashMap<String, Object>(); map.put("name", "Первый"); map.put("value", 10); // В реальности нужно будет добавлять необходимые поля, сколько нужно, // динамически, в зависимости от параметров и данных. preparedData.add(map); map = new HashMap<String, Object>(); map.put("name", "Второй"); map.put("value", 4); // В реальности нужно будет добавлять необходимые поля, сколько нужно, // динамически, в зависимости от параметров и данных. preparedData.add(map); return new JRMapCollectionDataSource(preparedData); } public static JasperDesign createDesign() throws JRException { // Эквивалентно StaticText в JasperStudio JRDesignStaticText staticText = null; // Эквивалентно TextField в JasperStudio JRDesignTextField textField = null; // Band. Details, Summary, Title и другие. JRDesignBand band = null; // Вычисляемое выражение. Для записи значений в JRDesignTextField. JRDesignExpression expression = null; // Для рисования линий. @SuppressWarnings("unused") JRDesignLine line = null; // Для добавления полей в отчёт. JRDesignField field = null; // Можно создавать условные стили. @SuppressWarnings("unused") JRDesignConditionalStyle conditionalStyle = null; // Рамка вокруг ячейки. JRLineBox lineBox = null; // Вычисляемое значение. Можно подсчитать, например сумму. JRDesignVariable variable = null; int x; int y; final int ROW_HEIGHT = 11; final int COLUMN_WIDTH = 60; JasperDesign jasperDesign = new JasperDesign(); jasperDesign.setName("dynamicColumns"); jasperDesign.setPageWidth(600); jasperDesign.setPageHeight(500); jasperDesign.setColumnWidth(COLUMN_WIDTH); jasperDesign.setColumnSpacing(0); jasperDesign.setLeftMargin(40); jasperDesign.setRightMargin(40); jasperDesign.setTopMargin(40); jasperDesign.setBottomMargin(40); jasperDesign.setIgnorePagination(true); jasperDesign .setWhenNoDataType(WhenNoDataTypeEnum.ALL_SECTIONS_NO_DETAIL); JRDesignStyle normalStyle = new JRDesignStyle(); normalStyle.setName("normal"); normalStyle.setDefault(true); normalStyle.setFontName("DejaVu Sans"); normalStyle.setFontSize(8.5f); lineBox = normalStyle.getLineBox(); lineBox.getTopPen().setLineWidth(0.5f); lineBox.getRightPen().setLineWidth(0.5f); lineBox.getBottomPen().setLineWidth(0.5f); lineBox.getLeftPen().setLineWidth(0.5f); jasperDesign.addStyle(normalStyle); JRDesignStyle headerStyle = new JRDesignStyle(); headerStyle.setName("header"); headerStyle.setDefault(true); headerStyle.setFontName("DejaVu Sans"); headerStyle.setFontSize(8.5f); headerStyle.setBold(true); lineBox = headerStyle.getLineBox(); lineBox.getTopPen().setLineWidth(0.5f); lineBox.getRightPen().setLineWidth(0.5f); lineBox.getBottomPen().setLineWidth(0.5f); lineBox.getLeftPen().setLineWidth(0.5f); jasperDesign.addStyle(headerStyle); // Параметры отчёта JRDesignParameter startDateParameter = new JRDesignParameter(); startDateParameter.setName("startDate"); startDateParameter.setValueClass(java.util.Date.class); jasperDesign.addParameter(startDateParameter); JRDesignParameter endDateParameter = new JRDesignParameter(); endDateParameter.setName("endDate"); endDateParameter.setValueClass(java.util.Date.class); jasperDesign.addParameter(endDateParameter); // Поля отчёта. field = new JRDesignField(); field.setName("name"); field.setValueClass(java.lang.String.class); jasperDesign.addField(field); field = new JRDesignField(); field.setName("value"); field.setValueClass(java.lang.Integer.class); jasperDesign.addField(field); // В случае отчёта с динамическими полями пробегаемся по количеству // полей и добавляем JRDesignField для каждого с уникальным именем. // Подсчитываем сумму variable = new JRDesignVariable(); variable.setResetType(ResetTypeEnum.REPORT); variable.setValueClass(java.lang.Integer.class); expression = new JRDesignExpression(); expression.setText("$F{value}"); variable.setExpression(expression); variable.setCalculation(CalculationEnum.SUM); expression = new JRDesignExpression(); expression.setText("0"); variable.setInitialValueExpression(expression); variable.setName("summary"); jasperDesign.addVariable(variable); // Title band band = new JRDesignBand(); // добавляем нужные элементы в band. // Можно добавлять JRDesignTextField-ы и JRDesignStaticField-ы, // картинки и всё, что угодно. Мы пропустим для простоты. jasperDesign.setTitle(band); // Заголовки колонок. x = 0; y = 0; band = new JRDesignBand(); band.setHeight(ROW_HEIGHT); staticText = new JRDesignStaticText(); staticText.setX(x); staticText.setY(y); staticText.setWidth(COLUMN_WIDTH); staticText.setHeight(ROW_HEIGHT); staticText.setStyle(headerStyle); staticText.setText("Название"); band.addElement(staticText); x += staticText.getWidth(); staticText = new JRDesignStaticText(); staticText.setX(x); staticText.setY(y); staticText.setWidth(COLUMN_WIDTH); staticText.setHeight(ROW_HEIGHT); staticText.setStyle(headerStyle); staticText.setText("Значение"); band.addElement(staticText); x += staticText.getWidth(); jasperDesign.setColumnHeader(band); // Detail band (данные) band = new JRDesignBand(); band.setHeight(ROW_HEIGHT); x = 0; y = 0; textField = new JRDesignTextField(); textField.setX(x); textField.setY(y); textField.setWidth(COLUMN_WIDTH); textField.setHeight(ROW_HEIGHT); expression = new JRDesignExpression(); expression.setText("$F{name}"); textField.setExpression(expression); textField.setStyle(normalStyle); band.addElement(textField); x += textField.getWidth(); textField = new JRDesignTextField(); textField.setX(x); textField.setY(y); textField.setWidth(COLUMN_WIDTH); textField.setHeight(ROW_HEIGHT); expression = new JRDesignExpression(); expression.setText("$F{value}"); textField.setExpression(expression); textField.setStyle(normalStyle); band.addElement(textField); x += textField.getWidth(); // DetailsBand добавляется немного странно, да... ((JRDesignSection) jasperDesign.getDetailSection()).addBand(band); // Column footer band = new JRDesignBand(); jasperDesign.setColumnFooter(band); // Подвал страницы band = new JRDesignBand(); jasperDesign.setPageFooter(band); // Summary band band = new JRDesignBand(); band.setHeight(ROW_HEIGHT); x = 0; y = 0; staticText = new JRDesignStaticText(); staticText.setX(x); staticText.setY(y); staticText.setWidth(COLUMN_WIDTH); staticText.setHeight(ROW_HEIGHT); staticText.setStyle(headerStyle); staticText.setText("ИТОГО:"); band.addElement(staticText); x+=staticText.getWidth(); textField = new JRDesignTextField(); textField.setX(x); textField.setY(y); textField.setWidth(COLUMN_WIDTH); textField.setHeight(ROW_HEIGHT); expression = new JRDesignExpression(); expression.setText("$V{summary}"); textField.setExpression(expression); textField.setStyle(headerStyle); band.addElement(textField); x += textField.getWidth(); jasperDesign.setSummary(band); return jasperDesign; } } |
Разумеется, генерировать шаблоны отчётов JasperReports программно имеет смысл только тогда, когда нужно сделать что-то действительно уникальное, странное. В большинстве случаев вполне можно обойтись обычным шаблоном созданным в JasperStudio.
Ссылка на проект на github:
https://github.com/urvanov-ru/java-examples/tree/master/jasper-reports-dynamic-columns (поставьте звезду, пожалуйста)
Ссылка на архив проекта:
jasper-reports-dynamic-columns