/** * */ public void fill() throws JRException { long start = System.currentTimeMillis(); Map<String, Object> parameters = new HashMap<String, Object>(); try { JRCsvDataSource xyds = new JRCsvDataSource(JRLoader.getLocationInputStream("data/xyDatasource.csv"), "UTF-8"); xyds.setRecordDelimiter("\r\n"); xyds.setUseFirstRowAsHeader(true); parameters.put("xyDatasource", xyds); } catch (Exception e) { throw new JRException(e); } JasperFillManager.fillReportToFile("build/reports/XYChart.jasper", new HashMap<String, Object>(parameters), new JREmptyDataSource()); System.err.println("Filling time : " + (System.currentTimeMillis() - start)); }
/** * Test of getCsvDataSource method, of class Db. */ @Test public void testGetCsvDataSource() throws Exception { System.out.println("getCsvDataSource"); Config config = new Config(); config.dbType = DsType.csv; config.dataFile = new File("target/test-classes/csvExampleHeaders.csv"); config.csvCharset = "utf-8"; config.csvFieldDel = "|"; config.csvRecordDel = "\r\n"; config.csvFirstRow = true; Db instance = new Db(); JRCsvDataSource jRCsvDataSource = instance.getCsvDataSource(config); jRCsvDataSource.next(); Map names = jRCsvDataSource.getColumnNames(); assertEquals(names.toString(), "{Name=0, Street=1, City=2, Phone=3}"); }
/** * This method return an instanced JRDataDource to the database. * If isJDBCConnection() return true => getJRDataSource() return false */ @Override public net.sf.jasperreports.engine.JRDataSource getJRDataSource() { try { JRCsvDataSource ds = new JRCsvDataSource(new File(getFilename())); if (this.getCustomDateFormat() != null && this.getCustomDateFormat().length() > 0) { ds.setDateFormat(new SimpleDateFormat(this.getCustomDateFormat())); } ds.setFieldDelimiter( getFieldDelimiter().charAt(0) ); ds.setRecordDelimiter( getRecordDelimiter()); ds.setUseFirstRowAsHeader( isUseFirstRowAsHeader()); if (!isUseFirstRowAsHeader()) { String[] names = new String[getColumnNames().size()]; for (int i=0; i<names.length; ++i ) { names[i] = ""+getColumnNames().elementAt(i); } ds.setColumnNames( names ); } return ds; } catch (Exception ex) { ex.printStackTrace(); return super.getJRDataSource(); } }
private static JRCsvDataSource getDataSource() throws JRException, IOException { //JRLoader.getLocationInputStream( JRCsvDataSource ds = new JRCsvDataSource(new File("C:\\wsfirext\\Firext_local\\WebContent\\WEB-INF\\reports\\test.csv")); ds.setRecordDelimiter("\t"); ds.setUseFirstRowAsHeader(true); ds.setFieldDelimiter(';'); return ds; }