Simple demo of the jasperreport report

Source: Internet
Author: User
[Java] & lt; prename & quot; code & quot; class & quot; java & quot; & gt; & lt; prename & quot; code & quot; class & quot; java & quot; & gt; & lt; spanstyle & quot; font-family: Arial, Helvetica, san [java]
Recently, the project manager has arranged tasks to learn about the design and production of jasperreport reports. Based on the provided materials and online learning, he has developed a report that can be imported externally to download excel,
Because the data source is still in the beginner stage, it is currently passed in the program, but it has not been passed in through the database or javabean. The next time we will go deeper.
First, I designed my own report graph in the IReport software. I designed a very simple report. In fact, the most important part of the report design is its core processes.
  
Design reports, data import, and output reports
[Java] the first program is the download code in the service [java] @ Service ("reportService") @ Path ("/report") public class ReportService {@ Autowired private ICommonDao; @ Path ("download") @ Produces (MediaType. TEXT_PLAIN) @ Transactional public String downloadReport (@ Context HttpServletRequest req, @ Context HttpServletResponse res) throws Exception {String designFilePath = req. getSession (). getServletContext (). getRealPath ("/jasper") + File. separator + "reportTest. jrxml "; File designFile = new File (designFilePath); if (designFile. exists () {DataReportProcess reportProcess = new DataReportProcess (); reportProcess. process (req, res, designFile);} return "success ";}} this Code contains the DataReportProcess class [java] public class DataReportProcess extends XLSReportProcess {/*** obtain the jasperReport object from the template file compilation * @ return JasperReport object jasperReport * @ throws JRException */private JasperReport getJasperReport (File designFile) throws Exception {JasperReport jasperReport = null; JasperDesign design = JRXmlLoader. load (designFile); jasperReport = JasperCompileManager. compileReport (design); return jasperReport;} public void process (HttpServletRequest req, HttpServletResponse res, File designFile) throws Exception {String outputFileName = "dataReport.xlsx"; Map  DataMap = new HashMap  (); DataMap. put ("name", "Zhang San"); Collection  > DataMapList = new ArrayList  > (); DataMapList. add (dataMap); JRMapCollectionDataSource dataSource = new JRMapCollectionDataSource (dataMapList); JasperReport jasperReport = this. getJasperReport (designFile); JasperPrint jasperPrint = JasperFillManager. fillReport (jasperReport, null, dataSource); this.exportWebReport(ReportProcess.Type.xlsx, res, jasperPrint, outputFileName);} the data source interface here uses JRMapCollectionDataSource, which is now a dead key value.. This class mainly involves some APIJasperReport core API1. JRXmlLoader (xml loader) which contains the load method for loading *. the jrxml file returns the jasperDesign object 2. JRcompile (Interface) defines the method acceptance parameter to return the jasperReport object 3. jasperCompileManager (compilation manager) provides some methods to compile a Report into a file. JasperReport jasperReport = JasperCompileManager. compileReport (design); 4. jasperFillManager is mainly used to fill the report into the file JasperPrint jasperPrint = JasperFillManager. fillReport (jasperReport, reportParams, re SultSetDataSource); 5. jasperPrintManager prints the JasperPrint object (related data) to pdf. xml and other files. jasperExportManager exports the JasperPrint object (related data) to pdf. output report function [java] @ Override public void exportWebReport (Type type, HttpServletResponse res, JasperPrint print, String outputFileName) throws Exception {if (type! =Null&type.ss(reportprocess.type.xlsx) {// 2007 excel or above res. setContentType ("application/vnd. openxmlformats-officedocument.spreadsheetml.sheet ");} else {res. setContentType ("application/vnd. ms-excel ");} // resolve the Chinese file name and set the file name if (outputFileName! = Null &&! OutputFileName. isEmpty () {res. setHeader ("charset", "ISO8859-1"); res. setHeader ("Content-Disposition", "attachment; filename = \" "+ new String (outputFileName. getBytes (), "ISO8859-1") + "\" ");} BufferedOutputStream outputStream = null; outputStream = new BufferedOutputStream (res. getOutputStream (); this. exportFile (res, print, outputFileName, outputStream); outputStream. flush (); outputStream. close ();} [ja Va] // here, you only need to set various high attributes for the output excel file. The file name is sheetname... Public void exportFile (HttpServletResponse res, JasperPrint print, String outputFileName, OutputStream outputStream) throws JRException {jrw.actexporter jrExporter = new JRXlsxExporter (); jrExporter. setParameter (JRExporterParameter. JASPER_PRINT, print); jrExporter. setParameter (JRExporterParameter. OUTPUT_STREAM, outputStream); jrExporter. setParameter (JRXlsExporterParameter. IS_DETECT_CELL_TYPE, Boolean. TRUE); jrExporter. setParameter (JRXlsExporterParameter. IS_COLLAPSE_ROW_SPAN, Boolean. TRUE); jrExporter. setParameter (JRXlsExporterParameter. IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean. TRUE); jrExporter. setParameter (JRXlsExporterParameter. IS_ONE_PAGE_PER_SHEET, Boolean. FALSE); jrExporter. setParameter (JRXlsExporterParameter. IS_WHITE_PAGE_BACKGROUND, Boolean. FALSE); if (outputFileName! = Null &&! OutputFileName. isEmpty () {jrExporter. setParameter (JRExporterParameter. OUTPUT_FILE_NAME, outputFileName);} System. out. println ("exportFile: outputFileName outputStream" + outputFileName + "" + outputStream); jrExporter. setParameter (JRXlsExporterParameter. SHEET_NAMES, new String [] {"info"}); jrExporter. exportReport ();} the final result is that the input service link will automatically download the excel file named datareport.xlsx. The content in the file is very simple, that is, the name is three OK, and a simple demo is completed, next, we will implement a more general and targeted report design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.