您好,欢迎来到爱玩科技网。
搜索
您的当前位置:首页jasper编程方式实现报表导出

jasper编程方式实现报表导出

来源:爱玩科技网


jasper编程方式实现报表导出

引自:http://www.iteye.com/topic/98140

项目里用到了jasperreport,平时都是用ireport来设计好报表模板直接使用就好了,但是碰到动态表头等情况就比较麻烦了,而且有些报表可能仅是针对一个数据表或者说一个实体对象的操作,对于这种报表一个个设计JRXML也比较麻烦,尝试了一下编程的方式来生成报表,只是个简单的测试。在网上找一些资料发现纯编程来写报表的相关内容很少,也是看了相关的API后才写了个例子。

ReportProcess.java是一个简单的用来构造、编译并导出报表的类。

Java代码

1. package jasper.test;

2. import java.io.FileInputStream;

3. import java.io.InputStream;

4. import java.io.InputStreamReader;

5. import net.sf.jasperreports.engine.JRException;

6. import net.sf.jasperreports.engine.JasperCompileManager;

7. import net.sf.jasperreports.engine.JasperExportManager;

8. import net.sf.jasperreports.engine.JasperFillManager;

9. import net.sf.jasperreports.engine.JasperPrint;

10. import net.sf.jasperreports.engine.JasperReport;

11. import net.sf.jasperreports.engine.design.JRDesignBand;

12. import net.sf.jasperreports.engine.design.JRDesignExpression;

13. import net.sf.jasperreports.engine.design.JRDesignField;

14. import net.sf.jasperreports.engine.design.JRDesignStaticText;

15. import net.sf.jasperreports.engine.design.JRDesignTextField;

16. import net.sf.jasperreports.engine.design.JasperDesign;

17. public class ReportProcess {

18. public static void main(String[] args) {

19. try {

20. JasperReport jp = getJasperReport();

21. InputStream in = new FileInputStream(

22. \"src/test/java/jasper/test/Data.csv\");// 换成自己目录

23. CsvDataSource csvDataSource = new CsvDataSource(

24. new InputStreamReader(in));

25. in.close();

26. JasperPrint jpr = JasperFillManager.fillReport(jp, null,

27. csvDataSource);

28. JasperExportManager.exportReportToHtmlFile(jpr, \"c:/test.html\");// 写的时候随便指定了个查看的目录

29. } catch (Exception e) {

30. e.printStackTrace();

31. }

32. }

33. public static JasperReport getJasperReport() throws JRException {

34. JasperDesign design = new JasperDesign();

35. design.setName(\"testReport\");

36. JRDesignBand title = new JRDesignBand();

37. title.setHeight(50);

38. JRDesignStaticText titleText = new JRDesignStaticText();

39. titleText.setText(\"test report\");

40. titleText.setX(230);

41. titleText.setFontSize(20);

42. titleText.setHeight(50);

43. titleText.setWidth(100);

44. title.addElement(titleText);

45. design.setTitle(title);

46. String[] headers = { \"name\", \"age\", \"gender\", \"like\" };

47. JRDesignBand columnHeader = new JRDesignBand();

48. columnHeader.setHeight(30);

49. JRDesignBand detail = new JRDesignBand();

50. detail.setHeight(30);

51. for (int i = 0; i < headers.length; i++) {

52. // add column headers

53. JRDesignStaticText staticText = new JRDesignStaticText();

54. staticText.setText(headers[i]);

55. staticText.setFontSize(16);

56. staticText.setHeight(30);

57. staticText.setWidth(50);

58. staticText.setX(50 * i);

59. columnHeader.addElement(staticText);

60. // define fields

61. JRDesignField field = new JRDesignField();

62. field.setName(headers[i]);

63. field.setValueClass(String.class);

. design.addField(field);

65. // add text fields for displaying fields

66. JRDesignTextField textField = new JRDesignTextField();

67. JRDesignExpression expression = new JRDesignExpression();

68. expression.setText(\"$F{\" + headers[i] + \);

69. expression.setValueClass(String.class);

70. textField.setExpression(expression);

71. textField.setFontSize(14);

72. textField.setHeight(30);

73. textField.setWidth(50);

74. textField.setX(50 * i);

75. detail.addElement(textField);

76. }

77. design.setColumnHeader(columnHeader);

78. design.setDetail(detail);

79. return JasperCompileManager.compileReport(design);

80. }

81. }

CsvDataSource.java是摘自JASER例子里的一个数据源实现,有现成的做例子的时候就拿来用了,用这个比较简单,不用写数据库那一堆。

Java代码

1. package jasper.test;

2. import java.io.FileNotFoundException;

3. import java.io.IOException;

4. import java.io.Reader;

5. import java.util.List;

6. import net.sf.jasperreports.engine.JRException;

7. import net.sf.jasperreports.engine.JRField;

8. import net.sf.jasperreports.engine.JRRewindableDataSource;

9. import au.com.bytecode.opencsv.CSVReader;

10. /**

11. *

12. * 这个类摘自JASPER带的一个例子里的CSV数据源实现。

13. *

14. */

15. public class CsvDataSource implements JRRewindableDataSource {

16. private CSVReader csvReader;

17. private List rows;

18. private int currentRowIndex = -1;

19. private int currentColIndex = 0;

20. private int totalRows;

21. public CsvDataSource(Reader reader) {

22. try {

23. csvReader = new CSVReader(reader);

24. rows = csvReader.readAll();

25. totalRows = rows.size();

26. } catch (FileNotFoundException e) {

27. e.printStackTrace();

28. } catch (IOException e) {

29. e.printStackTrace();

30. }

31. }

32. public boolean next() throws JRException {

33. boolean retVal = true;

34. currentRowIndex++;

35. currentColIndex = 0;

36. if (currentRowIndex >= totalRows) {

37. retVal = false;

38. }

39. return retVal;

40. }

41. public Object getFieldValue(JRField arg0) throws JRException {

42. String value = null;

43. String[] currentRow = (String[]) rows.get(currentRowIndex);

44. value = currentRow[currentColIndex];

45. currentColIndex++;

46. return value;

47. }

48. public void moveFirst() throws JRException {

49. currentRowIndex = 0;

50. currentColIndex = 0;

51. }

52. }

Data.csv里的数据。

Java代码

1. jim,12,male,basketball

2. lily,13,female,dancing

3. david,23,male,swimming

4. su,23,female,running

结果大致是这个样子。

Java代码

1. test report

2. name age gender like

3. jim 12 male basketb

4. lily 13 female dancing

5. david 23 male swimmi

6. su 23 female running

test report

name age gender like

jim 12 male basketb

lily 13 female dancing

david 23 male swimmi

su 23 female running

这里就是个实验性的代码,但至少可以知道,我们可以通过一些手段来编程处理JASPER的报表。可以写得更完善一些,动态获取表头个数与内容,写QUERY查询,填充报表。

比如一张表有一部分表头是不定的,那就可以在程序中追加表头定义,追加字段定义等。

http://www.iteye.com/topic/98140

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- aiwanbo.com 版权所有 赣ICP备2024042808号-3

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务