Preface
In the previous articles, we have explained some common reports in simple format, but sometimes our reports must be in the form of tables. In this case, we need to use our jrtablemodeldatasource.
Question
As before, the following steps are required to generate a report:
1.IntroductionJarFor more information, see《Static text report.
2.Create a report template:
This is the same as the aircraftreport report template in the previous article. For details, see map data sources for different data sources.
3.For details about how to compile a report template, see static text report..
4.WriteServlet:
Package COM. dan. jasper; import Java. AWT. borderlayout; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; import Java. util. hashmap; import javax. swing. jbutton; import javax. swing. jframe; import javax. swing. jlabel; import javax. swing. jtable; import javax. swing. table. defaulttablemodel; import net. SF. jasperreports. engine. jrexception; import net. SF. jasperreports. engine. jasperfillmanager; import net. SF. jasperreports. engine. jasperprint; import net. SF. jasperreports. engine. data. jrtablemodeldatasource; import net. SF. jasperreports. view. jasperviewer; public class tablemodelreport {jframe mainframe; borderlayout; defaulttablemodel tablemodel; jtable table = new jtable (); jbutton generatereportbutton = new jbutton ("Generate Report"); Public tablemodelreport () {mainframe = new jframe ("aircraft data"); borderlayout = new borderlayout (); generatereportbutton. addactionlistener (New reportgenerator (); populatetablemodel (); mainframe. setsize (640,150); mainframe. setvisible (true); mainframe. getcontentpane (). setlayout (borderlayout); mainframe. add (New jlabel ("aircraft data"), borderlayout. north); table. setmodel (tablemodel); mainframe. getcontentpane (). add (table, borderlayout. center); mainframe. getcontentpane (). add (generatereportbutton, borderlayout. south); mainframe. setvisible (true);} private void populatetablemodel () {string [] columnnames = {"ID", "name", "sex "}; string [] [] DATA = {"1", "rebecca1", "female" },{ "2", "rebeca2", "female "}, {"3", "rebeca3", "female" },{ "4", "rebecca4", "female" }}; tablemodel = new defaulttablemodel (data, columnnames );} private void displayreport () {jasperprint = generatereport (); jasperviewer = new jasperviewer (jasperprint); jasperviewer. setvisible (true);} private jasperprint generatereport () {jasperprint = NULL; try {jasperprint = jasperfillmanager. fillreport ("E:/project/work1/report3/webroot/WEB-INF/classes/reports/aircraftreport. jasper ", new hashmap (), new jrtablemodeldatasource (tablemodel);} catch (jrexception e) {e. printstacktrace ();} return jasperprint;} private class reportgenerator implements actionlistener {public void actionreceivmed (actionevent e) {displayreport () ;}} public static void main (string [] ARGs) {New tablemodelreport ();}}
5.ConfigurationWeb. xmlIs the configurationServlet
6.Run the project
Let's take a look at my running results:
Summary:
This table-based report is similar to the previous preview report, but it shows the table style and needs to preview and print the report function.