smigh Posted August 28, 2009 Share Posted August 28, 2009 I'm rather new to iReport and I need to send data from a JTable to it. I've been doing that by saving the data on a database and then getting it back for the report but this involves a needless connection to a database that I want to avoid. I know that I can send the table model directly but I couldn't find out how to do it (my thread regarding that is here) so I thought that using CSV to send the data would be easier.Using the code below, the CSV file is generated successfully and no errors are thrown but the report shows no rows. It prints everything I send from the parameters but nothing from the CSV file. However if I use iReport to preview the report using that same file that was generated by the java app, the report prints just fine.Is there something else I need to do when filling the report from the java app?Code:java.io.File ficheiro_csv = new java.io.File("f.csv");ficheiro_csv.createNewFile();grelha1.saveAsCSV(ficheiro_csv, true);java.io.InputStream rel = getClass().getResourceAsStream("rptVD2.jasper");net.sf.jasperreports.engine.data.JRCsvDataSource csv_datasource = new net.sf.jasperreports.engine.data.JRCsvDataSource(ficheiro_csv);String[] colunas_csv = {"COD","DESIGNACAO","QT","PRECO","DESC","DS","UPC","PM","RG","UMED","PV","VALOR"};csv_datasource.setColumnNames(colunas_csv);net.sf.jasperreports.engine.JasperPrint relat = net.sf.jasperreports.engine.JasperFillManager.fillReport(rel, MapaParametros, csv_datasource);javax.print.PrintService defaultPrintService = javax.print.PrintServiceLookup.lookupDefaultPrintService();net.sf.jasperreports.engine.export.JRPrintServiceExporter exporter = new net.sf.jasperreports.engine.export.JRPrintServiceExporter();exporter.setParameter(net.sf.jasperreports.engine.JRExporterParameter.JASPER_PRINT, relat);exporter.setParameter(net.sf.jasperreports.engine.export.JRPrintServiceExporterParameter.PRINT_SERVICE, defaultPrintService);exporter.exportReport(); Link to comment Share on other sites More sharing options...
anarosa Posted August 28, 2009 Share Posted August 28, 2009 I couldn't understand what is the error and you don't tell how is your csv structure. I see that you are setting the column names. I also set some other properties: csv_datasource.setUseFirstRowAsHeader --> true or false csv_datasource.setFieldDelimiter csv_datasource.setRecordDelimiter --> \n if new line in unix and \r\n if new line in WindowsPost Edited by anarosa at 08/28/2009 12:39 Link to comment Share on other sites More sharing options...
smigh Posted August 28, 2009 Author Share Posted August 28, 2009 Thanks for the reply.The problem is that the report doesn't display the content of the CSV file if I send it through java application. However, if I open on iReport and preview it with the CSV file that the java app saved to disk, the report prints with all the content. I believe something is missing from the java code.I added the lines: csv_datasource.setUseFirstRowAsHeader(false); csv_datasource.setRecordDelimiter("\r\n"); csv_datasource.setFieldDelimiter(',');But it made no difference. I've also tried: csv_datasource.setRecordDelimiter("\n");But no difference either.The CSV file, as opened in Windows notepad, looks like this:"131.11.01.05","Tubo galvanizado S/M 1""","1,00","5,41","0,00","0,00","0,00","0,00","0,00","MT","0,00","0,00""131.11.01.06","Tubo galvanizado S/M 1.1/4""","1,00","5,41","0,00","0,00","0,00","0,00","0,00","MT","0,00","0,00""131.11.01.12","Tubo galvanizado S/M 4""","1,00","5,41","0,00","0,00","0,00","0,00","0,00","MT","0,00","0,00"I can't find any example or tutorial for me to follow. Is there anything missing?Post Edited by smigh at 08/28/2009 11:01 Link to comment Share on other sites More sharing options...
smigh Posted August 28, 2009 Author Share Posted August 28, 2009 Just a small update. I was using a method from a grid component that saves the data into a CSV file but since I couldn't check the code it was using, I'm using my own method now. This was to make sure the record delimiter was actually "\r\n".The method is called "gravaGrelhaCSV" in the code below and the file it creates is valid, which I prove by opening iReport and previewing the report using that file as datasource. In that case, it shows all records and fields apropriately.Now when I execute the code below from a java application, sending that file as the datasource, I get the report shown but with no data from the CSV file. Empty. Can you see if I'm missing something here? I can do the same thing but with a database connection so the problem is with the CSV datasource or some parameter of it. File ficheiro_csv = Global.gravaGrelhaCSV(grelha1); ficheiro_csv.createNewFile(); InputStream rel = getClass().getResourceAsStream("rptVD2.jasper"); JRCsvDataSource csv_datasource = new JRCsvDataSource(ficheiro_csv); String[] colunas_csv = {"COD","DESIGNACAO","QT","PRECO","DESC","DS","UPC","PM","RG","UMED","PV","VALOR"}; csv_datasource.setColumnNames(colunas_csv); csv_datasource.setUseFirstRowAsHeader(false); csv_datasource.setRecordDelimiter("\r\n"); csv_datasource.setFieldDelimiter(','); net.sf.jasperreports.engine.JasperPrint relat = JasperFillManager.fillReport(rel, MapaParametros, csv_datasource); Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now