Jump to content
We've recently updated our Privacy Statement, available here ×

szaboaz

Members
  • Posts

    29
  • Joined

  • Last visited

 Content Type 

Profiles

Forum

Events

Featured Visualizations

Knowledge Base

Documentation (PDF Downloads)

Blog

Documentation (Test Area)

Documentation

Dr. Jaspersoft Webinar Series

Downloads

Everything posted by szaboaz

  1. I know I can a) use iReport for viewing the report, but I need a printable version, and b) the template viewer is not enough: e.g. somehow I'd like to display the "print when" expressions along the fields they applied to. To put it another way: I'd like to achieve something like Microsoft Word's commenting feature. Or yet another way: our client doesn't want to learn, how to use iReport. They gave us samples, we did it in JasperReports, which works great. And now they want to have better understanding, what is happening inside the reports. E.g. they'd like to see which fields' appearance are conditional, and what is the condition. What database table-field is displayed in the fields. Yet, they don't want to use iReport for this, they need a pdf which they can print, and work out with pencil and paper, whether they'd like to ask further changes and what those changes would be. (And a generated report with example data doesn't enough either, since conditional fields have a behaviour according to their condition of course, and we cannot generate every possible version.) And to be honest, they have another reason (which they don't actually know): some nice workarounds were applied during the creation of the reports, which would make their lives a little bit difficult if they'd like to get an good understanding of the reports by opening them in iReport: the textFields have 1px height, and they are in many dummy groups, since we have long long reports, and the dummy groups have to be placed to the page's bottom, if they can fit there. I wrote a short description about it in a previous thread: http://jasperforge.org/plugins/espforum/view.php?group_id=102&forumid=103&topicid=42979#43473 Possible solutions would be: 1) Create an "analysis version" of all the reports. Manually. Sounds like much work, and if anything changes in the report, it has to be done in the analysis version too... 2) Put extra content into the very same fields, and make them appear if an "ANALYSIS" parameter is true. Sounds like much work either... And all the conditional expressions shouldn't apply. Does it make any sense to anyone? I was wondering if I could actually PUT extra fields and switch off conditional expressions automatically, by working on the jrxml, or rather through the API. But putting extra fields between another fields, and changing the 1px height fields to e.g. 19 px, and position them one above the other programmatically doesn't sound like fun either. I've read the new feature of 3.1.0 about "generic component support" with "custom made visual components". I wonder if you see some kind of "field comment" component a good candidate for using this feature. Thanks, Arpad
  2. What do you mean by "where". Of course, to the place where other band elements are. Here's an average-ish jrxml (with no actual content): Code:<?xml version="1.0" encoding="UTF-8" ?> <!-- Created with iReport - A designer for JasperReports --> <!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd"> <jasperReport name="Untitled_report_1" columnCount="1" printOrder="Vertical" orientation="Portrait" pageWidth="595" pageHeight="842" columnWidth="535" columnSpacing="0" leftMargin="30" rightMargin="30" topMargin="20" bottomMargin="20" whenNoDataType="NoPages" isTitleNewPage="false" isSummaryNewPage="false"> <property name="ireport.scriptlethandling" value="2" /> <property name="ireport.encoding" value="UTF-8" /> <import value="java.util.*" /> <import value="net.sf.jasperreports.engine.*" /> <import value="net.sf.jasperreports.engine.data.*" /> <background> <band height="0" isSplitAllowed="true" > </band> </background> <title> <band height="50" isSplitAllowed="true" > </band> </title> <pageHeader> <band height="50" isSplitAllowed="true" > </band> </pageHeader> <columnHeader> <band height="30" isSplitAllowed="true" > </band> </columnHeader> <detail> <band height="100" isSplitAllowed="true" > </band> </detail> <columnFooter> <band height="30" isSplitAllowed="true" > </band> </columnFooter> <pageFooter> <band height="50" isSplitAllowed="true" > </band> </pageFooter> <lastPageFooter> <band height="50" isSplitAllowed="true" > </band> </lastPageFooter> <summary> <band height="50" isSplitAllowed="true" > </band> </summary> </jasperReport> As you can see, there's a <background></background> element there. So according to the advices in the previous message, you have to set the background's height to the maximum available value (which is the page size minus top and bottom margins). Then you put your border (that is a rectangle element) into the band (with the appropriate size - in this case it is also the maximum available, I guess). Is it clearer? Arpad
  3. In index.jsp lines 35-43 and lines 52-57 are for the usage of datasources in relation of a subreport. I used datasources for both the master (masterreport_ds.jrxml) and the subreport (subreport_ds.jrxml). Subreport's datasource is put into the parameters of the master report, and this very same parameter is set as Data Source Expression in the subreport element of masterreport_ds.jrxml. Lines 59-64 (now commented out) demonstrates the usage of subreports without datasources (using only the Connection object). This time, masterreport_sql.jrxml and subreport_sql.jrxml contain their own sql query string. If you watch the two result pdfs carefully, you can notice one important thing about datasources: result_with_connection.pdf contains three records from master report, and every record has its own subreport section containing several records from the subreport. However, result_with_datasources.pdf contains the three records from the master report also, but only the first record has its own subreport section, the second and third one doesn't. This is because the subreport datasource is consumed first time. If you want to give a datasource to a subreport, you have to be careful, to assure that your subreport will appear only once. Best regards, Arpad [file name=subreport_with_datasource.zip size=16476] Post edited by: szaboaz, at: 2008/07/16 09:30 Post edited by: szaboaz, at: 2008/07/16 09:32
  4. Please see my answer in the other thread you started: "Multiple datasource in one report - 2008/07/14 17:44" http://www.jasperforge.org/index.php?option=com_joomlaboard&Itemid=215&func=view&id=43537&catid=8 Arpad
  5. Yeah, subreports are your friends, just like lshannon suggested in the other thread. Arpad
  6. Try this: to the Print When Expression, write Code:new Boolean(true)and Code:[code]new Boolean(false) and try it. Does the image appear/disappear? If yes, then the parameter handling must be the problem. Does your parameter have any default value set (which takes over, if you mis-spelled it in your code)? You can also write out your parameter's value into a TextField, just to be sure, that it is what you think it is. Good luck, Arpad
  7. I thought it wouldn't be useless if I write a small follow-up on this. As far as workarounds work well, the tip solved the problem basically. The design time height of my dummy groups became 20-30 pixels, and it's a rare case, when they contain so much optional elements, which would result in a smaller height in the generated report. However, the 1px height TextFields also brought another problem to the surface: the vertical alignment of the elements. Previously, I had 17 pixels height StaticTexts and TextFields, with about 11 pixels Font size. They were attached vertically to each others, to form lines, and the difference between line heights and font sizes gave a nice couple pixels of space between them. Now that I had 1pixels TextFields set to stretch with overflow, this space has gone. I first tried to set the TextFields linespacing to 1-1-2, or Double, and it made a difference, but I had TextFields, which stretched into more than one lines, and the space between those lines were also 1-1-2/double. Next, I put empty space between the TextFields. This was OK, until I saw, that if I have an optional line, which is not displayed actually, the empty space around it remained, and "pushed away" the ones around it. The final solution was that I put one pixel height TextFields, with a space in the String expression. (Later I changed the space to a dummy character,and set the foreground color to white, because I wasn't sure if the space is working every time.) Then, I applied the print-when expressions to them accordingly with the optional text fields near them. It's quite a big work. These spaceholder TextFields are useful for the following reason: they take only one pixel of my precious desing time height, and their font size can be set to different values, which results in easily configurable "line spacing". To further facilitate this configurableness, I made them wide enough to reach well into the margins. This way I can select all of them with one mouse movement, and set the font size to a different value. I set their background color to different values according to their font size, and I only need to switch the transparent property to true or false, to make the colors displayed or hidden in the generated report. Attached image shows how it looks like in 800% (and shrinked down to 50%, so that forum accept it). Best regards, Arpad size=375]http://www.jasperforge.org/components/com_joomlaboard/uploaded/images/spaceholdertextfields.png Post edited by: szaboaz, at: 2008/07/12 11:17
  8. szaboaz

    Blank Pdf

    For sure. The idea behind subreports is that there is a changing data element in every record of the masterreport's data source, and a subreport usually depends on this data (takes it as a parameter, and gets its value from the query's result). This is the most typical usage of subreports. Then, a masterreport can also pass a non-changing value to the subreport, if it is required. So the masterreport has to get this value from somewhere. And where should it get from? Of course from a parameter. So yes, if you want to give a parameter value to a subreport, you have to "inject" it into the masterreport, which forwards it to the subreport to the subreport's own parameter - whether the masterreport actually use it or not. Best regards, Arpad ps. All the things I said before is said "AFAIK", I don't consider myself an authentic source ;-). If I'm wrong, please correct me. Thanks.
  9. szaboaz

    Blank Pdf

    I was curious, how much the difference can be between the two methods (starting with .jrxml, or starting with .jasper). In my case, these were the times, when I made them run five times: .jasper: 1953 1859 1843 1937 1969 .jrxml: 3250 3328 3296 3406 3344 Convincing, isn't it? You can do the same by putting these two lines to your code: Code: long start = System.currentTimeMillis(); System.err.println("Filling time : " + (System.currentTimeMillis() - start)); Best regards, Arpad Post edited by: szaboaz, at: 2008/07/12 08:11 Post edited by: szaboaz, at: 2008/07/12 08:13
  10. szaboaz

    Blank Pdf

    Using .jasper files and not .jrxml makes the code faster, since it doesn't need to do the jrxml->jasper transformation each and every time your report runs. If your report runs thousands and thousands of times, it can cause significant decrease in performance. However, if your code needs to be executed rather rarely, BUT your reports change often, and you have to deploy them again and again, it might be reasonable to use .jrxml files in the code, since you don't need to compile them into .jasper files before you deploy them. Code: package subreportjasperrunner; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.JasperExportManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; /** * * @author szaboaz */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver"«»); Connection connection; try { connection = DriverManager.getConnection("jdbc:mysql://localhost/bank", "root", "password"«»); JasperReport subreport; JasperReport masterreport; try { subreport = JasperCompileManager.compileReport("subreport.jrxml"«»); masterreport = JasperCompileManager.compileReport("masterreport.jrxml"«»); Map parameters = new HashMap(); parameters.put("SubreportParameter", subreport); //It can be done more than one way: //I choose to follow runReportToPdfFile method's code //in JasperRunManager.java (in fact, I copied it from there). //But I use a fix string for output filename) //If a previous file exists with the same filename, it will be overwritten. JasperPrint jasperPrint = JasperFillManager.fillReport(masterreport, parameters, connection); JasperExportManager.exportReportToPdfFile(jasperPrint, "masterreport.pdf"«»); //Another approach: //this time, I didn't use JasperRunManager.runReportToPdfFile, //since it cannot use a JasperReport as parameter. //runReportToPdf, however, could take a JasperReport object //as parameter, and it returns a byte array: //byte[] pdf = JasperRunManager.runReportToPdf(masterreport, parameters, connection); //you can do whatever you want to do with your byte array //e.g. write it into a file (tricky part to decide, what to do with //the filenames, should it overwrite or not, if it already exists //or apply some logic to save the file with some differences in the //name all the time... } catch (JRException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } catch (SQLException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } catch (ClassNotFoundException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } }
  11. szaboaz

    Blank Pdf

    Here you are. This time I created a masterreport.jrxml and a subreport.jrxml, and used a MySQL connection to actually make them run, and they did! The reports demonstrate essentially the same "feature" as your files modified previously by me: masterreport has a "SubreportParameter" parameter of type JasperReport, this is used in the subreport element of the masterreport as Subreport Expression. It's your turn now to apply it to your reports (and your database connection, of course) :) Good Luck, Arpad [file name=notaservletthistime.zip size=3968]
  12. szaboaz

    Blank Pdf

    I've modified your jrxml files, and wrote a servlet, which should run it, if you set the database connection properly, and place the jasper files to a place where they can be get by the ServletContext. What did I modify: I added a parameter named SubreportParam, of type net.sf.jasperreports.engine.JasperReport to the master report. I set this parameter as Subreport Expression of the subreport element (and made sure, that it's type, the Subreport Expression Class, is set to net.sf.jasperreports.engine.JasperReport). In the Java code I create a net.sf.jasperreports.engine.JasperReport object instance from the subreport, and put it into the parameters Map. And that's all, it should fly (hope, hope). Good Luck, Arpad [file name=rbojja.zip size=26274] Post edited by: szaboaz, at: 2008/07/11 14:06
  13. What is the reason of giving a JRDataSource to the master report in your java code, instead of giving the Connection itself to it? (Especially becouse your master report contains an SQL query, which is absolutely unnecessary, and doesn't do anything.) Now your master report has a JRDataSource, and forwards this very same datasource to the subreport (through $P{REPORT_CONNECTION}). And the subreport also contains an SQL query, which should run on the JRDatasource?!? HTH, Arpad ps. I had no problem opening the two jasper files from the attached help.zip file. Oh, and another remark, while I'm at it. The subreport's page width is 535. However, it is intended to fit into the detail band of your master report, which is narrower (by the size of the two side margins), isn't it? Let me recommend to set your subreport's page width to 535-30-30=475, and position your subreport element to the 0th pixel of the detail band, and set its width to 475 accordingly. Post edited by: szaboaz, at: 2008/07/11 08:00 Post edited by: szaboaz, at: 2008/07/11 08:01
  14. You can find lots of demo code (including subreports) in demo/samples directory of JasperReports' "project" release. (You can find it at the sourceforge download place, the one with "project" in its name.) Best regards, Arpad Post edited by: szaboaz, at: 2008/07/11 06:44 Post edited by: szaboaz, at: 2008/07/11 06:46
  15. szaboaz

    Help Plz

    What if you use TextField (not StaticText) for the long text? You can set its vertical size to a small value, eg. 17 pixels, and set its "Stretch with overflow" value to true. Doing so, you will have the necessary space for placing other things to the page. This will not save you from lines going to the second page, if the middle part of your report grows big vertically, though. (But how can you put an elephant into a mouse-hole?) HTH, Arpad ps. use meaningful subject lines to your posts, it helps people to find out, if they can help you or not.
  16. Set "Subreport Expression" to the subreport's filename (with .jasper extension). E.g. "mysubreport.jasper" (between "" signs), and choose java.lang.String as Subreport Expression Class. Now you need to put your subreport jasper file to the classpath of your web application (e.g. into WEB-INF/classes), and nothing else needs to be done, when you generate the master report, it will be found. Another practical approach is that you set Subreport Expression Class to e.g. net.sf.jasperreports.engine.JasperReport, and its value is a parameter (of the same type). This parameter gets its value just like the other parameters: so you can instantiate your subreport in your java code (just like you do with your master report) as a net.sf.jasperreports.engine.JasperReport object, and add it to the parameters. HTH, Arpad
  17. Maybe a subreport for each page, with a special query, which takes the record limits as parameters, with its own summary? This poses some questions (maybe more, than it solves...), e.g. where the main summary will get the data to sum from, maybe the subreports give them back, or the main summary should be also a subreport then? These are just ideas from the top of my head, I haven't tried-an'-tested them. All in all, a good look on subreports may help. Good luck, Arpad
  18. Hi Kevin, Now I'm confused :huh: after seeing the screenshot and the explanation. But never mind... If you want to be more clear, can I suggest such a screenshot which demonstrates a bad case, and maybe another with a good case. It's not clear for me, that the one you attached is a good, or a bad (or both, or neither),but it's maybe me. (And as far as I still understand, it cannot be solved, but hey, let someone prove me wrong!) All the best, Arpad
  19. This worked for me: Code: <%@ page contentType="text/plain;charset=UTF-8" language="java" errorPage="" %> <%@ page import="java.io.IOException"%> <%@ page import="java.io.InputStream"%> <%@ page import="java.io.PrintWriter"%> <%@ page import="java.io.StringWriter"%> <%@ page import="java.util.HashMap"%> <%@ page import="java.util.Map"%> <%@ page import="javax.servlet.ServletException"%> <%@ page import="javax.servlet.ServletOutputStream"%> <%@ page import="javax.servlet.http.HttpServlet"%> <%@ page import="javax.servlet.http.HttpServletRequest"%> <%@ page import="javax.servlet.http.HttpServletResponse"%> <%@ page import="net.sf.jasperreports.engine.JRExporterParameter"%> <%@ page import="net.sf.jasperreports.engine.JasperFillManager"%> <%@ page import="net.sf.jasperreports.engine.JasperPrint"%> <%@ page import="net.sf.jasperreports.engine.export.JRTextExporter"%> <%@ page import="net.sf.jasperreports.engine.export.JRTextExporterParameter"%> <% try { response.setContentType("text/plain"«»); response.setHeader("Content-Disposition", "inline; filename=xyz.txt"«»); ServletOutputStream servletOutputStream = response.getOutputStream(); InputStream reportStream = getServletConfig().getServletContext().getResourceAsStream("/reports/example.jasper"«»); Map parameters = new HashMap(); JasperPrint jasperPrint = JasperFillManager.fillReport(reportStream, parameters); JRTextExporter exporter = new JRTextExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRTextExporterParameter.OUTPUT_STREAM, servletOutputStream); exporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH, new Integer(10)); exporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT, new Integer(10)); exporter.exportReport(); servletOutputStream.flush(); servletOutputStream.close(); } catch (Exception e) { // display stack trace in the browser StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); e.printStackTrace(printWriter); response.setContentType("text/plain"«»); response.getOutputStream().print(stringWriter.toString()); } %> Best regards, Arpad
  20. As per "partnerrows are not cut", I don't think it can be solved unfortunately. But let me explain, how I (maybe mis-)understand your question, and let's hope I'll stand corrected. And maybe this will help to clarify the problem further for someone more competent than me. You basically want to achieve, that when an "oenr_Group" fits entirely (with all of its detail rows) on a page, it should be placed there, and not on the next page. And when it doesn't fit at the end of the page, it should start on the next. (Except when it doesn't fit on an entire page, of course, since this would result in an endless loop.) Whether a group starts in the middle/end a given page or goes to the next, is based on "Start on a new page" setting. If it is set to false, then the layout logic uses the design time height of the group header/detail/footer (I haven't checked, if all of them together, or one by one). About this "design time height" problem please see this thread of mine from a couple days before (a little different situation, but I believe this is common): dummy group header design time height applies I *think* this is a legitimate requirement, which should be turned into a Request For Enhancement, if it hasn't been done yet, and we and others can vote for it, and follow its status. (I have to admit, that I don't checked if such an RFE exists, or where to add it, it's time for some reading for me, I guess.) Best regards, Arpad Post edited by: szaboaz, at: 2008/07/09 06:37 Post edited by: szaboaz, at: 2008/07/09 06:40
  21. You're welcome. There's another question here, maybe needs to be solved, maybe not: when I run the servlet, the browser always gives the Open/Save/Cancel dialog box, and doesn't fires up excel inside the browser window. I had to achieve this second situation with pdf output, and it was ok this way: response.setContentType("application/pdf"); response.setHeader("Content-Disposition", "inline; filename=xyz.pdf"); Note, that it is absolutely similar to the lines in the Excel example: response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "inline; filename=xyz.xls"); I think it has something to do with difference in Excel and Acrobat settings on the client machine. I wonder if an xls still can be opened somehow inside of the browser... Best regards, Arpad
  22. Your server basically gives two kinds of things to your browser: the headers, and the data. You always have to set the headers first, and after that, you can put your data to the output stream. You indeed call response.setContentType("application/vnd.ms-excel"); before you send the data, BUT look what happens: your JSP contains lines like <HTML><BODY>. Your server actually starts to give these (as data) to the browser, so any call of setContentType inside the code block is too late. Since you won't put html tags to the output in this situation, you can use a servlet for this as well. But I was interested, if a jsp has anything wrong to it, so I created a short example, as a servlet first, then I put the code into a jsp. I put both of them here. They are working for me, I hope you will find them useful. Best regards, Arpad First, the servlet: Code: /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.example; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.StringWriter; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.export.JRXlsExporter; import net.sf.jasperreports.engine.export.JRXlsExporterParameter; /** * * @author szaboaz */ public class BrowseExcelServlet extends HttpServlet { /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * @param request servlet request * @param response servlet response */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { response.setContentType("application/vnd.ms-excel"«»); response.setHeader("Content-Disposition", "inline; filename=xyz.xls"«»); ServletOutputStream servletOutputStream = response.getOutputStream(); InputStream reportStream = getServletConfig().getServletContext().getResourceAsStream("/reports/example.jasper"«»); Map parameters = new HashMap(); JasperPrint jasperPrint = JasperFillManager.fillReport(reportStream, parameters); JRXlsExporter exporter = new JRXlsExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, servletOutputStream); exporter.exportReport(); servletOutputStream.flush(); servletOutputStream.close(); } catch (Exception e) { // display stack trace in the browser StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); e.printStackTrace(printWriter); response.setContentType("text/plain"«»); response.getOutputStream().print(stringWriter.toString()); } } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP <code>GET</code> method. * @param request servlet request * @param response servlet response */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP <code>POST</code> method. * @param request servlet request * @param response servlet response */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. */ public String getServletInfo() { return "Short description"; }// </editor-fold> } And the jsp: Code:[code] <%@ page contentType="application/vnd.ms-excel;charset=UTF-8" language="java" import="java.sql.*" errorPage="" %> <%@ page import="java.io.IOException"%> <%@ page import="java.io.InputStream"%> <%@ page import="java.io.PrintWriter"%> <%@ page import="java.io.StringWriter"%> <%@ page import="java.util.HashMap"%> <%@ page import="java.util.Map"%> <%@ page import="javax.servlet.ServletException"%> <%@ page import="javax.servlet.ServletOutputStream"%> <%@ page import="javax.servlet.http.HttpServlet"%> <%@ page import="javax.servlet.http.HttpServletRequest"%> <%@ page import="javax.servlet.http.HttpServletResponse"%> <%@ page import="net.sf.jasperreports.engine.JRExporterParameter"%> <%@ page import="net.sf.jasperreports.engine.JasperFillManager"%> <%@ page import="net.sf.jasperreports.engine.JasperPrint"%> <%@ page import="net.sf.jasperreports.engine.export.JRXlsExporter"%> <%@ page import="net.sf.jasperreports.engine.export.JRXlsExporterParameter"%> <% try { response.setContentType("application/vnd.ms-excel"«»); response.setHeader("Content-Disposition", "inline; filename=xyz.xls"«»); ServletOutputStream servletOutputStream = response.getOutputStream(); InputStream reportStream = getServletConfig().getServletContext().getResourceAsStream("/reports/example.jasper"«»); Map parameters = new HashMap(); JasperPrint jasperPrint = JasperFillManager.fillReport(reportStream, parameters); JRXlsExporter exporter = new JRXlsExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, servletOutputStream); exporter.exportReport(); servletOutputStream.flush(); servletOutputStream.close(); } catch (Exception e) { // display stack trace in the browser StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); e.printStackTrace(printWriter); response.setContentType("text/plain"«»); response.getOutputStream().print(stringWriter.toString()); } %> Post edited by: szaboaz, at: 2008/07/08 19:38 Post edited by: szaboaz, at: 2008/07/08 19:41
  23. You can pop up a JFileChooser, and it will give back a File. If you assign it to a reference named destFile, you can say: Code:exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); And after that, you can call Code:[code]exporter.exportReport(); If you want to pop up the JFileChooser AFTER you did the export, then instead of JRExporterParameter.OUTPUT_FILE_NAME, set any of these: JRExporterParameter.OUTPUT_STRING_BUFFER, JRExporterParameter.OUTPUT_WRITER, JRExporterParameter.OUTPUT_STREAM With the appropriate result object as a parameter value. The exporter will put the result in that, you pop up the JFileChooser, get the File, and write the thing into it. HTH, Arpad Post edited by: szaboaz, at: 2008/07/08 12:49
  24. If you want to export to csv, I think still the best solution (and one which can be made very fast) is to make a separate jrxml (and not to use the one which is highly formatted, and the fields are aligned heavily). Such a report can be made e.g. iReport's wizard, which will put all the columns on the page (the title, some lines, date, page number and margins can be easily erased/set to zero). Usually this will result in columns, which are too narrow for the data to fit in, but this can be solved by using Code:<property name="net.sf.jasperreports.print.keep.full.text" value="true" /> HTH, Arpad
×
×
  • Create New...