-
Notifications
You must be signed in to change notification settings - Fork 380
/
ExcelDataAdapterApp.java
163 lines (138 loc) · 5.95 KB
/
ExcelDataAdapterApp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* JasperReports - Free Java Reporting Library.
* Copyright (C) 2001 - 2023 Cloud Software Group, Inc. All rights reserved.
* http://www.jaspersoft.com
*
* Unless you have purchased a commercial license agreement from Jaspersoft,
* the following license terms apply:
*
* This program is part of JasperReports.
*
* JasperReports is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JasperReports is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
*/
import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import net.sf.jasperreports.data.DataAdapterParameterContributorFactory;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrintManager;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.AbstractSampleApp;
import net.sf.jasperreports.engine.util.JRLoader;
/**
* @author Sanda Zaharia (shertage@users.sourceforge.net)
*/
public class ExcelDataAdapterApp extends AbstractSampleApp
{
/**
*
*/
public static void main(String[] args)
{
main(new ExcelDataAdapterApp(), args);
}
@Override
public void test() throws JRException
{
fill();
pdf();
html();
}
/**
*
*/
public void fill() throws JRException
{
long start = System.currentTimeMillis();
//Preparing parameters
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("ReportTitle", "Address Report");
Set<String> states = new HashSet<String>();
states.add("Active");
states.add("Trial");
parameters.put("IncludedStates", states);
//query executer mode
parameters.put("DataFile", "XLS query executer mode for Excel data adapter");
JasperFillManager.fillReportToFile("build/reports/ExcelXlsQeDataAdapterReport.jasper", new HashMap<String, Object>(parameters));
parameters.put("DataFile", "XLSX query executer mode for Excel data adapter");
JasperFillManager.fillReportToFile("build/reports/ExcelXlsxQeDataAdapterReport.jasper", new HashMap<String, Object>(parameters));
JasperReport jasperReport = (JasperReport)JRLoader.loadObjectFromFile("build/reports/ExcelXlsQeDataAdapterReport.jasper");
jasperReport.setProperty(DataAdapterParameterContributorFactory.PROPERTY_DATA_ADAPTER_LOCATION, "data/XlsQeDataAdapter.jrdax");
JasperFillManager.fillReportToFile(jasperReport, "build/reports/XlsQeDataAdapterReport.jrprint", new HashMap<String, Object>(parameters));
jasperReport = (JasperReport)JRLoader.loadObjectFromFile("build/reports/ExcelXlsxQeDataAdapterReport.jasper");
jasperReport.setProperty(DataAdapterParameterContributorFactory.PROPERTY_DATA_ADAPTER_LOCATION, "data/XlsxQeDataAdapter.jrdax");
JasperFillManager.fillReportToFile(jasperReport, "build/reports/XlsxQeDataAdapterReport.jrprint", new HashMap<String, Object>(parameters));
//data source mode
parameters.put("DataFile", "Excel data adapter for XLS data source");
JasperFillManager.fillReportToFile("build/reports/ExcelXlsDataAdapterReport.jasper", new HashMap<String, Object>(parameters));
parameters.put("DataFile", "Excel data adapter for XLSX data source");
JasperFillManager.fillReportToFile("build/reports/ExcelXlsxDataAdapterReport.jasper", new HashMap<String, Object>(parameters));
jasperReport = (JasperReport)JRLoader.loadObjectFromFile("build/reports/ExcelXlsDataAdapterReport.jasper");
jasperReport.setProperty(DataAdapterParameterContributorFactory.PROPERTY_DATA_ADAPTER_LOCATION, "data/XlsDataAdapter.jrdax");
JasperFillManager.fillReportToFile(jasperReport, "build/reports/XlsDataAdapterReport.jrprint", new HashMap<String, Object>(parameters));
jasperReport = (JasperReport)JRLoader.loadObjectFromFile("build/reports/ExcelXlsxDataAdapterReport.jasper");
jasperReport.setProperty(DataAdapterParameterContributorFactory.PROPERTY_DATA_ADAPTER_LOCATION, "data/XlsxDataAdapter.jrdax");
JasperFillManager.fillReportToFile(jasperReport, "build/reports/XlsxDataAdapterReport.jrprint", new HashMap<String, Object>(parameters));
System.err.println("Filling time : " + (System.currentTimeMillis() - start));
}
/**
*
*/
public void print() throws JRException
{
long start = System.currentTimeMillis();
JasperPrintManager.printReport("build/reports/ExcelXlsDataAdapterReport.jrprint", true);
System.err.println("Printing time : " + (System.currentTimeMillis() - start));
}
/**
*
*/
public void pdf() throws JRException
{
File[] files = getFiles(new File("build/reports"), "jrprint");
for(int i = 0; i < files.length; i++)
{
File reportFile = files[i];
long start = System.currentTimeMillis();
String fileName = reportFile.getAbsolutePath();
JasperExportManager.exportReportToPdfFile(
fileName,
fileName.substring(0, fileName.indexOf(".jrprint")) + ".pdf"
);
System.err.println("PDF creation time : " + (System.currentTimeMillis() - start));
}
}
/**
*
*/
public void html() throws JRException
{
File[] files = getFiles(new File("build/reports"), "jrprint");
for(int i = 0; i < files.length; i++)
{
File reportFile = files[i];
long start = System.currentTimeMillis();
String fileName = reportFile.getAbsolutePath();
JasperExportManager.exportReportToHtmlFile(
fileName,
fileName.substring(0, fileName.indexOf(".jrprint")) + ".html"
);
System.err.println("HTML creation time : " + (System.currentTimeMillis() - start));
}
}
}