iReport入门DEMO,带你快速入门

     由于最近系统中要做报表,所以使用到了Ireport来制作报表.我就不再介绍Ireport是干嘛的了.下面我将用SpringBoot整合Ireport的一个入门Demo来告诉你们在项目中怎么使用iReport导出pdf文件.

 

  • 创建一个maven项目

  • Pom.xml配置


<!-- 加入SpringBoot的 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>


    <dependencies>
        <!-- 添加SpringBoot-web的依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
         <!-- 添加Swagger的依赖 -->
        <!--导入Ireport的包 -->
        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>6.0.0</version>
        </dependency>
            <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.4.11</version>
        </dependency>
        
    </dependencies>
    
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
                <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>


  • 编写一个简单的hello.jrxml

  • 导入到项目中去

  • 编写Controller


@RestController

public class HelloController {

 

/**

扫描二维码关注公众号,回复: 3509880 查看本文章

 * 导入为PDF

 * @return

 * @throws Exception

 */

@RequestMapping(value="/exportPdf")

public ResponseEntity<byte[]> exportPdf() throws Exception{

 

   PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

   Resource resource = resolver.getResource("jasper/HelloReport.jasper");

  JasperPrint jasperPrint=JasperFillManager.fillReport(resource.getInputStream(), null,new JREmptyDataSource());

  byte[] body = JasperExportManager.exportReportToPdf(jasperPrint);

  HttpHeaders header=new HttpHeaders();

  header.setContentType(MediaType.APPLICATION_PDF);

  header.add("Content-Disposition", "inline;filename=hello.pdf");

  return new ResponseEntity<byte[]>(body, header, HttpStatus.OK);

 

  }

}


  • 启动SpringBoot项目

  


@SpringBootApplication(scanBasePackages="com.hjy")

public class App {

public static void main(String[] args) throws Exception {

      SpringApplication.run(App.class, args);

  }

}


  • 项目目录结构

  • 访问PDF


猜你喜欢

转载自blog.csdn.net/qq_37549708/article/details/82344413