使用 Jaspersoft Studio 设计报表样式

用你熟悉的环境设计一套最简单的模板,名称定义为: Reports.jrxml
report_template.png
下载源文件:report.jrxml

  • 语法比较特殊,需要努力学习

    <?xml version="1.0" encoding="UTF-8"?>
    <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="84feeac3-d1f7-4503-84bf-fc3fa153df55">
      <queryString>
          <![CDATA[]]>
      </queryString>
      <field name="id" class="java.lang.Long"/>
      <field name="price" class="java.math.BigDecimal"/>
      <field name="quantity" class="java.lang.Integer"/>
      <field name="categoryName" class="java.lang.String"/>
      <field name="name" class="java.lang.String"/>
      <background>
          <band splitType="Stretch"/>
      </background>
      <title>
          <band height="36" splitType="Stretch"/>
      </title>
      <pageHeader>
          <band height="49" splitType="Stretch">
              <staticText>
                  <reportElement x="170" y="10" width="230" height="30" uuid="04d35299-5eb0-4a34-b340-3ca5cf8290fc"/>
                  <textElement>
                      <font fontName="宋体" size="20"/>
                  </textElement>
                  <text><![CDATA[Product List | 产品列表]]></text>
              </staticText>
          </band>
      </pageHeader>
      <columnHeader>
          <band height="37" splitType="Stretch">
              <staticText>
                  <reportElement x="0" y="10" width="70" height="20" uuid="bcaf85c0-57ff-4a34-af27-cbf1330125ae"/>
                  <textElement>
                      <font fontName="宋体" isBold="true"/>
                  </textElement>
                  <text><![CDATA[ID]]></text>
              </staticText>
              <staticText>
                  <reportElement x="90" y="10" width="100" height="20" uuid="faf9b9ee-7cf2-4ea5-80b8-d371c3c753b9"/>
                  <textElement>
                      <font fontName="宋体" isBold="true"/>
                  </textElement>
                  <text><![CDATA[名称]]></text>
              </staticText>
              <staticText>
                  <reportElement x="210" y="10" width="70" height="20" uuid="93727672-9f69-4856-aa26-33837e8dc417"/>
                  <textElement>
                      <font fontName="宋体" isBold="true"/>
                  </textElement>
                  <text><![CDATA[价格]]></text>
              </staticText>
              <staticText>
                  <reportElement x="314" y="10" width="70" height="20" uuid="09b47abe-09b2-479a-bb76-9770e361a23c"/>
                  <textElement>
                      <font fontName="宋体" isBold="true"/>
                  </textElement>
                  <text><![CDATA[数量]]></text>
              </staticText>
              <staticText>
                  <reportElement x="410" y="10" width="100" height="20" uuid="c258f47b-da6a-418d-a5e0-892388ec65c5"/>
                  <textElement>
                      <font fontName="宋体" isBold="true"/>
                  </textElement>
                  <text><![CDATA[类别]]></text>
              </staticText>
          </band>
      </columnHeader>
      <detail>
          <band height="39" splitType="Stretch">
              <textField>
                  <reportElement x="0" y="4" width="70" height="16" uuid="99fa4c16-cfe2-4482-828e-4439abb9e8f0"/>
                  <textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
              </textField>
              <textField>
                  <reportElement x="210" y="4" width="70" height="16" uuid="ba6e9cfa-6d4c-470b-8bc4-eb2e3411c21e"/>
                  <textFieldExpression><![CDATA[$F{price}]]></textFieldExpression>
              </textField>
              <textField>
                  <reportElement x="310" y="4" width="70" height="16" uuid="447253d3-d268-4873-b0b7-9c174c6a7c9f"/>
                  <textFieldExpression><![CDATA[$F{quantity}]]></textFieldExpression>
              </textField>
              <textField>
                  <reportElement x="410" y="4" width="100" height="16" uuid="8992d0ad-1f5e-4210-b50b-1e29dc66f119"/>
                  <textFieldExpression><![CDATA[$F{categoryName}]]></textFieldExpression>
              </textField>
              <line>
                  <reportElement x="-3" y="27" width="521" height="1" uuid="9b493a65-a499-4199-90bb-b3fedbe45f85"/>
              </line>
              <textField>
                  <reportElement x="90" y="4" width="100" height="16" uuid="fcdd8979-dc44-46e3-be56-11e5f33b8e0c">
                      <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="73fad7b3-74b5-4436-bd46-d2b9c3c113fd"/>
                  </reportElement>
                  <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
              </textField>
          </band>
      </detail>
      <columnFooter>
          <band height="45" splitType="Stretch"/>
      </columnFooter>
      <pageFooter>
          <band height="54" splitType="Stretch"/>
      </pageFooter>
      <summary>
          <band height="42" splitType="Stretch"/>
      </summary>
    </jasperReport>

    新建 Spring Boot 项目

  • 新建 Spring Boot 项目,不多说
    需包含的内容比较特殊,pom.xml,jasperreports 是依赖包,jasperreports-fonts 是字体支持包

          <dependency>
              <groupId>net.sf.jasperreports</groupId>
              <artifactId>jasperreports</artifactId>
              <version>6.15.0</version>
          </dependency>
          <dependency>
              <groupId>net.sf.jasperreports</groupId>
              <artifactId>jasperreports-fonts</artifactId>
              <version>6.15.0</version>
          </dependency>
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-support</artifactId>
              <version>2.0.8</version>
          </dependency>
  • 新建各种类
    controller, domain, repository, service, 其他,大概目录结构如下
    report2.png

  • 创建 Postgresql 数据表

    CREATE TABLE public.product
    (
      id bigint NOT NULL,
      name character varying(255) COLLATE pg_catalog."default",
      price numeric,
      quantity integer,
      category_name character varying(255) COLLATE pg_catalog."default",
      CONSTRAINT product_pkey PRIMARY KEY (id)
    )

集成 JasperReports 到 Spring Boot 项目

中文支持文章:Spring Boot 集成 JasperReports,并支持中文PDF

  • Service 服务层处理要求

    @RequestMapping(value = "/report", method = RequestMethod.GET)
      public void report(HttpServletResponse response, @RequestParam(name = "type", defaultValue = "pdf") String type) throws Exception {
    
          JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(productService.report());
          InputStream inputStream = this.getClass().getResourceAsStream("/report_templates/report.jrxml");
          JasperReport jasperReport = JasperCompileManager.compileReport(inputStream);
          JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, dataSource);
    
          switch (type) {
          case "html":
              response.setContentType("text/html;charset=utf-8");
              HtmlExporter exporter = new HtmlExporter(DefaultJasperReportsContext.getInstance());
              exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
              exporter.setExporterOutput(new SimpleHtmlExporterOutput(response.getWriter()));
              exporter.exportReport();
              break;
    
          case "pdf":
    
          default:
              JRPdfExporter exporterPdf = new JRPdfExporter();
              exporterPdf.setExporterInput(new SimpleExporterInput(jasperPrint));
              OutputStream outputStream = response.getOutputStream();
              exporterPdf.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
              exporterPdf.exportReport();
              break;
          }
    
      }
  • 可以是 HTML 版本
    r1.png

  • 也可以是 PDF 版本
    r2.png

源码下载: https://github.com/jiekechoo/YiqisoftReport

标签: none

添加新评论