亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

JasperReport實例

系統(tǒng) 1603 0
  1. package ?test.report; ??
  2. ??
  3. import ?java.math.BigDecimal; ??
  4. import ?java.sql.Connection; ??
  5. import ?java.sql.DriverManager; ??
  6. import ?java.sql.PreparedStatement; ??
  7. import ?java.sql.ResultSet; ??
  8. import ?java.sql.SQLException; ??
  9. import ?java.util.HashMap; ??
  10. ??
  11. import ?net.sf.jasperreports.engine.JRException; ??
  12. import ?net.sf.jasperreports.engine.JRResultSetDataSource; ??
  13. import ?net.sf.jasperreports.engine.JasperCompileManager; ??
  14. import ?net.sf.jasperreports.engine.JasperExportManager; ??
  15. import ?net.sf.jasperreports.engine.JasperFillManager; ??
  16. import ?net.sf.jasperreports.engine.JasperPrint; ??
  17. import ?net.sf.jasperreports.engine. JasperReport ; ??
  18. import ?net.sf.jasperreports.engine.util.JRLoader; ??
  19. ??
  20. public ? class ?GenerateReport?{ ??
  21. ??? public ? static ? void ?main(String[]?args)?{ ??
  22. ?????? try ?{ ??
  23. ?????????sql4Report(); ??
  24. ??????}? catch ?(JRException?e)?{ ??
  25. ?????????e.printStackTrace(); ??
  26. ??????}? catch ?(SQLException?e)?{ ??
  27. ?????????e.printStackTrace(); ??
  28. ??????}? finally ?{ ??
  29. ?????????System.out.print(? "finish" ?); ??
  30. ??????} ??
  31. ???} ??
  32. ???? ??
  33. ??? public ? static ? void ?parameter4Report?()? ??
  34. ?????? throws ?SQLException,?JRException?{ ??
  35. ?????? //report?parameter? ??
  36. ??????HashMap<String,?Object>?parameter?=? ??
  37. ????????? new ?HashMap<String,?Object>(); ??
  38. ??????parameter.put(? "master_location_id" ,? new ?BigDecimal( 22 )?); ??
  39. ??
  40. ??????Connection?con?=?getConnection(); ??
  41. ?????? JasperReport ? jasperReport ?=? ??
  42. ?????????JasperCompileManager.compileReport( "reports/Location.jrxml" ); ??
  43. ??????JasperPrint?jasperPrint?=? ??
  44. ?????????JasperFillManager.fillReport(? jasperReport ,?parameter,?con?); ??
  45. ???????? ??
  46. ??????JasperExportManager.exportReportToPdfFile( ??
  47. ?????????jasperPrint,? "reports/location_parameter4Report.pdf" );????? ??
  48. ???} ??
  49. ???? ??
  50. ??? public ? static ? void ?sql4Report?()? throws ?SQLException,?JRException?{ ??
  51. ?????? //create?the?ResultSet? ??
  52. ??????Connection?con?=?getConnection(); ??
  53. ??????PreparedStatement?statement?=? ??
  54. ?????????con.prepareStatement(? ??
  55. ???????????? "select?*?from?loc_location?" ??
  56. ??????????+? "where?location_type_cd?=?'ROOM'?" ??
  57. ??????????+? "order?by?location_type_cd" ? ??
  58. ?????????); ??
  59. ??????ResultSet?resultSet?=?statement.executeQuery(); ??
  60. ???????? ??
  61. ??????JRResultSetDataSource?result?=? ??
  62. ????????? new ?JRResultSetDataSource(?resultSet?); ??
  63. ?????? JasperReport ? jasperReport ?=? ??
  64. ?????????JasperCompileManager.compileReport( "reports/Location.jrxml" ); ??
  65. ??????JasperPrint?jasperPrint?=? ??
  66. ?????????JasperFillManager.fillReport(? ??
  67. ???????????? jasperReport ,? new ?HashMap(),?result? ??
  68. ?????????); ??
  69. ???????? ??
  70. ??????JasperExportManager.exportReportToPdfFile( ??
  71. ?????????jasperPrint,? "reports/location_sql4Report.pdf" ??
  72. ??????); ??
  73. ???} ??
  74. ???? ??
  75. ??? public ? static ? void ?jasper4Report?()? ??
  76. ?????? throws ?SQLException,?JRException?{ ??
  77. ?????? //create?the?ResultSet? ??
  78. ??????Connection?con?=?getConnection(); ??
  79. ??????PreparedStatement?statement?=? ??
  80. ?????????con.prepareStatement(? ??
  81. ???????????? "select?*?from?loc_location?" ??
  82. ??????????+? "where?location_type_cd?=?'ROOM'?" ??
  83. ??????????+? "order?by?location_type_cd" ? ??
  84. ?????????); ??
  85. ??????ResultSet?resultSet?=?statement.executeQuery(); ??
  86. ???????? ??
  87. ??????JRResultSetDataSource?result?=? ??
  88. ????????? new ?JRResultSetDataSource(?resultSet?); ??
  89. ?????? //modify? ??
  90. ?????? JasperReport ? jasperReport ?=? ??
  91. ?????????( JasperReport )?JRLoader.loadObject( ??
  92. ???????????? "reports/Location.jasper" ??
  93. ?????????); ??
  94. ??????JasperPrint?jasperPrint?=? ??
  95. ?????????JasperFillManager.fillReport(? ??
  96. ???????????? jasperReport ,? new ?HashMap(),?result ??
  97. ?????????); ??
  98. ???????? ??
  99. ??????JasperExportManager.exportReportToPdfFile( ??
  100. ?????????jasperPrint,? "reports/location_jasper4Report.pdf" ??
  101. ??????); ??
  102. ???} ??
  103. ??
  104. ??? public ? static ? void ?xls4Report?()? throws ?SQLException,?JRException?{ ??
  105. //????create?the?ResultSet? ??
  106. ??????Connection?con?=?getConnection(); ??
  107. ??????PreparedStatement?statement?=? ??
  108. ?????????con.prepareStatement( ??
  109. ???????????? "select?*?from?loc_location?" ??
  110. ??????????+? "where?location_type_cd?=?'ROOM'?" ??
  111. ??????????+? "order?by?location_type_cd" ? ??
  112. ?????????); ??
  113. ??????ResultSet?resultSet?=?statement.executeQuery(); ??
  114. ???????? ??
  115. ???????? ??
  116. ??????JRResultSetDataSource?result?=? ??
  117. ????????? new ?JRResultSetDataSource(?resultSet?); ??
  118. //????create? JasperReport ?from?.jasper? ??
  119. ?????? JasperReport ? jasperReport ?=? ??
  120. ?????????( JasperReport )?JRLoader.loadObject( "reports/Location.jasper" ); ??
  121. ??????JasperPrint?jasperPrint?=? ??
  122. ?????????JasperFillManager.fillReport(? jasperReport ,? new ?HashMap(),?result?); ??
  123. ???????? ??
  124. //????????JasperExportManager.exportReportToPdfFile(?jasperPrint,?"reports/location_xls4Report.xls");? ??
  125. ???????? ??
  126. ??????JRXlsExporter?xlsExporter?=? new ?JRXlsExporter(); ??
  127. ??????xlsExporter.setParameter(?JRExporterParameter.JASPER_PRINT,?jasperPrint?); ??
  128. ??????xlsExporter.setParameter(?JRExporterParameter.OUTPUT_FILE,? new ?File( "reports/location_xls4Report.xls" )?); ??
  129. ??????xlsExporter.exportReport(); ??
  130. ???} ??
  131. ???? ??
  132. ??? private ? static ?Connection?getConnection?()? throws ?SQLException?{ ??
  133. ??????DriverManager.registerDriver(? ??
  134. ????????? new ?oracle.jdbc.driver.OracleDriver() ??
  135. ??????);?? ??
  136. ?????? return ?DriverManager.getConnection(?url,?user,?pw?); ??
  137. ???} ??
  138. ???? ??
  139. ??? private ? static ?String?url?=? "jdbc:oracle:thin:@localhost:1521:db" ; ??
  140. ??? private ? static ?String?user?=? "report" ; ??
  141. ??? private ? static ?String?pw???=? "report" ; ??
  142. }??
Java代碼?? 收藏代碼
  1. package ?test.report;??
  2. ??
  3. import ?java.math.BigDecimal;??
  4. import ?java.sql.Connection;??
  5. import ?java.sql.DriverManager;??
  6. import ?java.sql.PreparedStatement;??
  7. import ?java.sql.ResultSet;??
  8. import ?java.sql.SQLException;??
  9. import ?java.util.HashMap;??
  10. ??
  11. import ?net.sf.jasperreports.engine.JRException;??
  12. import ?net.sf.jasperreports.engine.JRResultSetDataSource;??
  13. import ?net.sf.jasperreports.engine.JasperCompileManager;??
  14. import ?net.sf.jasperreports.engine.JasperExportManager;??
  15. import ?net.sf.jasperreports.engine.JasperFillManager;??
  16. import ?net.sf.jasperreports.engine.JasperPrint;??
  17. import ?net.sf.jasperreports.engine.<span? class = "hilite1" ><span?style= "background-color:?#ffff00;" >JasperReport</span></span>;??
  18. import ?net.sf.jasperreports.engine.util.JRLoader;??
  19. ??
  20. public ? class ?GenerateReport?{??
  21. ??? public ? static ? void ?main(String[]?args)?{??
  22. ?????? try ?{??
  23. ?????????sql4Report();??
  24. ??????}? catch ?(JRException?e)?{??
  25. ?????????e.printStackTrace();??
  26. ??????}? catch ?(SQLException?e)?{??
  27. ?????????e.printStackTrace();??
  28. ??????}? finally ?{??
  29. ?????????System.out.print(? "finish" ?);??
  30. ??????}??
  31. ???}??
  32. ??????
  33. ??? public ? static ? void ?parameter4Report?()???
  34. ?????? throws ?SQLException,?JRException?{??
  35. ?????? //report?parameter ??
  36. ??????HashMap<String,?Object>?parameter?=???
  37. ????????? new ?HashMap<String,?Object>();??
  38. ??????parameter.put(? "master_location_id" ,? new ?BigDecimal( 22 )?);??
  39. ??
  40. ??????Connection?con?=?getConnection();??
  41. ??????<span? class = "hilite1" ><span?style= "background-color:?#ffff00;" >JasperReport</span></span>?<span? class = "hilite1" ><span?style= "background-color:?#ffff00;" >jasperReport</span></span>?=???
  42. ?????????JasperCompileManager.compileReport( "reports/Location.jrxml" );??
  43. ??????JasperPrint?jasperPrint?=???
  44. ?????????JasperFillManager.fillReport(?<span? class = "hilite1" ><span?style= "background-color:?#ffff00;" >jasperReport</span></span>,?parameter,?con?);??
  45. ??????????
  46. ??????JasperExportManager.exportReportToPdfFile(??
  47. ?????????jasperPrint,? "reports/location_parameter4Report.pdf" );???????
  48. ???}??
  49. ??????
  50. ??? public ? static ? void ?sql4Report?()? throws ?SQLException,?JRException?{??
  51. ?????? //create?the?ResultSet ??
  52. ??????Connection?con?=?getConnection();??
  53. ??????PreparedStatement?statement?=???
  54. ?????????con.prepareStatement(???
  55. ???????????? "select?*?from?loc_location?" ??
  56. ??????????+? "where?location_type_cd?=?'ROOM'?" ??
  57. ??????????+? "order?by?location_type_cd" ???
  58. ?????????);??
  59. ??????ResultSet?resultSet?=?statement.executeQuery();??
  60. ??????????
  61. ??????JRResultSetDataSource?result?=???
  62. ????????? new ?JRResultSetDataSource(?resultSet?);??
  63. ??????<span? class = "hilite1" ><span?style= "background-color:?#ffff00;" >JasperReport</span></span>?<span? class = "hilite1" ><span?style= "background-color:?#ffff00;" >jasperReport</span></span>?=???
  64. ?????????JasperCompileManager.compileReport( "reports/Location.jrxml" );??
  65. ??????JasperPrint?jasperPrint?=???
  66. ?????????JasperFillManager.fillReport(???
  67. ????????????<span? class = "hilite1" ><span?style= "background-color:?#ffff00;" >jasperReport</span></span>,? new ?HashMap(),?result???
  68. ?????????);??
  69. ??????????
  70. ??????JasperExportManager.exportReportToPdfFile(??
  71. ?????????jasperPrint,? "reports/location_sql4Report.pdf" ??
  72. ??????);??
  73. ???}??
  74. ??????
  75. ??? public ? static ? void ?jasper4Report?()???
  76. ?????? throws ?SQLException,?JRException?{??
  77. ?????? //create?the?ResultSet ??
  78. ??????Connection?con?=?getConnection();??
  79. ??????PreparedStatement?statement?=???
  80. ?????????con.prepareStatement(???
  81. ???????????? "select?*?from?loc_location?" ??
  82. ??????????+? "where?location_type_cd?=?'ROOM'?" ??
  83. ??????????+? "order?by?location_type_cd" ???
  84. ?????????);??
  85. ??????ResultSet?resultSet?=?statement.executeQuery();??
  86. ??????????
  87. ??????JRResultSetDataSource?result?=???
  88. ????????? new ?JRResultSetDataSource(?resultSet?);??
  89. ?????? //modify ??
  90. ??????<span? class = "hilite1" ><span?style= "background-color:?#ffff00;" >JasperReport</span></span>?<span? class = "hilite1" ><span?style= "background-color:?#ffff00;" >jasperReport</span></span>?=???
  91. ?????????(<span? class = "hilite1" ><span?style= "background-color:?#ffff00;" >JasperReport</span></span>)?JRLoader.loadObject(??
  92. ???????????? "reports/Location.jasper" ??
  93. ?????????);??
  94. ??????JasperPrint?jasperPrint?=???
  95. ?????????JasperFillManager.fillReport(???
  96. ????????????<span? class = "hilite1" ><span?style= "background-color:?#ffff00;" >jasperReport</span></span>,? new ?HashMap(),?result??
  97. ?????????);??
  98. ??????????
  99. ??????JasperExportManager.exportReportToPdfFile(??
  100. ?????????jasperPrint,? "reports/location_jasper4Report.pdf" ??
  101. ??????);??
  102. ???}??
  103. ??
  104. ??? public ? static ? void ?xls4Report?()? throws ?SQLException,?JRException?{??
  105. //????create?the?ResultSet ??
  106. ??????Connection?con?=?getConnection();??
  107. ??????PreparedStatement?statement?=???
  108. ?????????con.prepareStatement(??
  109. ???????????? "select?*?from?loc_location?" ??
  110. ??????????+? "where?location_type_cd?=?'ROOM'?" ??
  111. ??????????+? "order?by?location_type_cd" ???
  112. ?????????);??
  113. ??????ResultSet?resultSet?=?statement.executeQuery();??
  114. ??????????
  115. ??????????
  116. ??????JRResultSetDataSource?result?=???
  117. ????????? new ?JRResultSetDataSource(?resultSet?);??
  118. //????create?<span?class="hilite1"><span?style="background-color:?#ffff00;">JasperReport</span></span>?from?.jasper ??
  119. ??????<span? class = "hilite1" ><span?style= "background-color:?#ffff00;" >JasperReport</span></span>?<span? class = "hilite1" ><span?style= "background-color:?#ffff00;" >jasperReport</span></span>?=???
  120. ?????????(<span? class = "hilite1" ><span?style= "background-color:?#ffff00;" >JasperReport</span></span>)?JRLoader.loadObject( "reports/Location.jasper" );??
  121. ??????JasperPrint?jasperPrint?=???
  122. ?????????JasperFillManager.fillReport(?<span? class = "hilite1" ><span?style= "background-color:?#ffff00;" >jasperReport</span></span>,? new ?HashMap(),?result?);??
  123. ??????????
  124. //????????JasperExportManager.exportReportToPdfFile(?jasperPrint,?"reports/location_xls4Report.xls"); ??
  125. ??????????
  126. ??????JRXlsExporter?xlsExporter?=? new ?JRXlsExporter();??
  127. ??????xlsExporter.setParameter(?JRExporterParameter.JASPER_PRINT,?jasperPrint?);??
  128. ??????xlsExporter.setParameter(?JRExporterParameter.OUTPUT_FILE,? new ?File( "reports/location_xls4Report.xls" )?);??
  129. ??????xlsExporter.exportReport();??
  130. ???}??
  131. ??????
  132. ??? private ? static ?Connection?getConnection?()? throws ?SQLException?{??
  133. ??????DriverManager.registerDriver(???
  134. ????????? new ?oracle.jdbc.driver.OracleDriver()??
  135. ??????);????
  136. ?????? return ?DriverManager.getConnection(?url,?user,?pw?);??
  137. ???}??
  138. ??????
  139. ??? private ? static ?String?url?=? "jdbc:oracle:thin:@localhost:1521:db" ;??
  140. ??? private ? static ?String?user?=? "report" ;??
  141. ??? private ? static ?String?pw???=? "report" ;??
  142. }??



需要的包:?
jasperreports-3.1.2.jar?
jasperreports-3.1.2-applet.jar?
jasperreports-3.1.2-javaflow.jar?
commons-collections.jar?
commons-digester.jar?
commons-logging-1.0.4.jar?
commons-logging-api.jar?
commons-beanutils.jar?
itext-1.3.1.jar?
classes12.jar?
poi-3.0.1.jar?
poi-contrib-3.0.1-FINAL-20070705.jar?
poi-scratchpad-3.0.1-FINAL-20070705.jar?

文件結(jié)構(gòu):?
+test?
++report?
+++GenerateReport.java?

+reports?
++Location.jrxml?
++Location.jasper?

數(shù)據(jù)表要和Location.jrmal或Location.jasper(即report的設(shè)計一致就行了

JasperReport實例


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!??!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲国产精品视频在线观看 | 视频黄色在线 | 亚洲成年人视频 | 久久我们这里只有精品国产4 | 伊人久久丁香色婷婷啪啪 | 91在线精品视频 | 欧洲黄色网 | 国产高清一区二区三区免费视频 | 夜夜艹天天干 | 全黄毛片 | 欧美一级毛片免费大全 | 羞羞视频网页 | 四虎最新紧急更新地址 | 国产不卡视频 | 国产精品久久久久久久久久影院 | 成年女人毛片免费播放人 | 久久伊人精品综合观看99 | 国产真实乱子伦精品 | 色吧久久| 99综合色 | 最新中文字幕一区 | 亚洲区一 | 天天在线欧美精品免费看 | 精品美女在线观看 | 伊人网伊人 | 波多野结衣一区二区三区 | 欧美综合视频在线 | 日本aaaaa级毛片 | 四虎在线影视在线影库 | 视频播放在线观看精品视频 | 99精品视频免费在线观看 | 一级黄色录像免费观看 | 99久久精品一区二区三区 | 五月婷综合网 | 久久亚洲精品中文字幕第一区 | 久久精品亚洲一区二区三区浴池 | 亚洲综合色视频 | 4虎永免费最新永久免费地址 | 国内精品手机在线观看视频 | sihu影院永久在线影院 | 五月激激激综合网色播免费 |