天天看点

iReport3.0+MyEclipse8.5生成PDF文件

1.启动iReport3,然后建立名为firstReport的新档:

  点击"档案"-->"开启新档"-->报表名称:"firstReport"

iReport3.0+MyEclipse8.5生成PDF文件

2.点击工具栏上的

iReport3.0+MyEclipse8.5生成PDF文件

图标,然后分别在title和columnHeader栏上输入报表标题和列名

iReport3.0+MyEclipse8.5生成PDF文件

3.建立javaBean作为数据源

   点击:"Data"-->"连结/资料来源"-->"New"-->"JavaBeans set data source"

iReport3.0+MyEclipse8.5生成PDF文件

输入数据来源名称Name: JavaBean_ITSTAR

iReport3.0+MyEclipse8.5生成PDF文件

4.建立web项目并发布:(Struts1+Hibernate3+Spring2.5), 并发布。

iReport3.0+MyEclipse8.5生成PDF文件

5.将iReport的开发包拷进web工程的lib目录下。

   iText-2.1.0.jar      

   iTextAsian.jar

   jasperreports-3.0.1.jar

   jfreechart-1.0.0.jar

   jcommon-1.0.0.jar

6.设置iReport中各种格式输出的系统路径(在这里此步骤可以不要)

点击"Options"-->"选项"-->"External Programs"

iReport3.0+MyEclipse8.5生成PDF文件

7.处理PDF中文问题(对于PDF文件必须处理)

   点击:"格式化"-->"报表类型"-->"myreport1 字体"-->"新建"

iReport3.0+MyEclipse8.5生成PDF文件

8.设置iReport的ClassPath路径,让iReport能找到这个JavaBean

  单击:"Options"-->Classpath-->添加文件夹(添加发布了的工程的WEB-INF/classes文件夹)-->Sava Classpath

iReport3.0+MyEclipse8.5生成PDF文件

7.在detail栏中拖拽JavaBean属性进myreport1文档中,职称这个报表。

iReport3.0+MyEclipse8.5生成PDF文件

10 查询JavaBean数据源,生成Field字段

  单击:"Data"-->"报表查询"-->"JavaBean Data Source"-->"填写class name(edu.pojos.User2)"-->Read attributes-->选中Field-->Add Selected Field(s)-->OK

iReport3.0+MyEclipse8.5生成PDF文件

11. 将编译生成的firstReport.jasper文件拷如web工程中

iReport3.0+MyEclipse8.5生成PDF文件

12. 编写程序,当以下载的方式将User2中的数据转化为PDF格式

publicclass ReportActionextends

DispatchAction{

    private User2Serviceuser2Service;

    publicvoid setUser2Service(User2Service user2Service)

{

       this.user2Service

= user2Service;

    }

public ActionForward topdf(ActionMapping mapping, ActionForm form,

 HttpServletRequest request, HttpServletResponse response)

       throws IOException,JRException{

 //集合 数据源

 List<User2>

list =user2Service.getAllUser2();

 JRBeanCollectionDataSource

dataSource = new JRBeanCollectionDataSource(list);

  //转真实路径

 InputStream

in = this.getClass().getResourceAsStream("/edu/report/firstReport.jasper");

 //填充

 /**

 * JasperFillManager.fillreport(InputStream inputStream,Map parameters,JRDataSource dataSource)

 **/

  JasperPrint

print = JasperFillManager.fillReport(in,null,

dataSource);

  //导出

  byte[]

data = JasperExportManager.exportReportToPdf(print);

  //输出格式为pdf

  response.setContentType("application/pdf");

  //弹出下载框

  response.setHeader("Content-Disposition","attachment;filename=user2.pdf");

  //输出

  response.getOutputStream().write(data);

  return

null;

}