... // getting back to the main method here //Now we process the master data and get a list of updates List<Object[]> updatedData = processMasterFile(mapOfDailyRecords); //And write the updated data to another file CsvWriterSettings writerSettings = new CsvWriterSettings(); writerSettings.setHeaders("id", "name", "city", "zip", "occupation", "company", "exp", "salary"); writerSettings.setHeaderWritingEnabled(true); CsvWriter writer = new CsvWriter(newWriter("/path/to/updates.csv"), writerSettings); //Here we write everything, and get the job done. writer.writeRowsAndClose(updatedData); }
//creates a configuration object that allows you to determine how to //format your CSV output, among many other things. Check the tutorial. CsvWriterSettings settings = new CsvWriterSettings(); //Configures the parser to process input objects using the BeanWriterProcessor. //This reads the data from your java beans. In this case the type is "TestBean" //but in yours it seems that would be something like "SearchBean". settings.setRowWriterProcessor(new BeanWriterProcessor<TestBean>(TestBean.class)); //Defines the headers to print out to the output. My "TestBean" class has the //following fields. Adjust this to reflect the fields in your "SearchBean" settings.setHeaders("amount", "pending", "date", "quantity", "comments"); // Creates a CsvWriter with the settings above CsvWriter writer = new CsvWriter(new FileWriter(new File("/path/to/file.csv")), settings); //Loads the beans from the database. //The "loadBeanList" method is something you should implement. List<TestBean> beansToPersist = loadBeanList(); //With a list of beans loaded from the database, you can simply call this //and write everything into your CSV file. You can also write beans one by one. writer.processRecordsAndClose(beansToPersist);
Writer output = new StringWriter(); // use a FileWriter for your case CsvWriterSettings writerSettings = new CsvWriterSettings(); //many options here, check the tutorial ObjectRowWriterProcessor writerProcessor = new ObjectRowWriterProcessor(); // handles rows of objects and conversions to String. writerSettings.setRowWriterProcessor(writerProcessor); writerSettings.setHeaders("A", "B", "C", "D", "E", "F", "G", "H"); CsvWriter writer = new CsvWriter(output, writerSettings); writerProcessor.convertFields(Conversions.toBoolean("0", "1")).add("C", "H"); // will write "0" and "1" instead of "true" and "false" on column "C" and "H" writerProcessor.convertFields(Conversions.toDate("M/d/YYYY HH:mm:ss")).add("A", "E"); writerProcessor.convertFields(Conversions.formatToBigDecimal("$#0.00")).add("B", "D"); writer.writeHeaders(); writer.processRecord(new Date(), BigDecimal.TEN, true, new BigDecimal("999.99"), new Date(), "some text here", null, false); writer.processRecord(new Date(), BigDecimal.ZERO, false, null, null, null, "more text here", null); writer.close(); System.out.println(output.toString()); //and here is the result
writerSettings.setHeaders("A", "B", "C", "D");
StringWriter output = new StringWriter(); CsvWriterSettings settings = new CsvWriterSettings(); // many options here, check the documentation ResultSetMetaData md = rs.getMetaData(); int columns = md.getColumnCount(); String[] headers = new String[columns]; for(int i = 1; i <= columns; i++){ headers[i - 1] = md.getColumnLabel(i); } //if an input value is null, you may want print "NULL" settings.setNullValue("NULL"); // if an input value is not null, but is empty (e.g. ""), the writer can be configured to print "NULL" as well settings.setEmptyValue("NULL"); // Sets the file headers (used for finer selection of fields, these values won't be written automatically) settings.setHeaders(headers); // Creates a writer with the above settings; CsvWriter writer = new CsvWriter(output, settings); // Writes the headers specified in the settings writer.writeHeaders(); Object[] row = new Object[columns]; while(rs.next()){ for(int i = 1; i <= columns; i++){ row[i - 1] = rs.getObject(i); } writer.writeRow(row); } writer.close(); String yourString = output.toString();
CsvWriterSettings settings = new CsvWriterSettings(); // Sets the file headers (used for selection only, these values won't be written automatically) settings.setHeaders("Year", "Make", "Model", "Description", "Price"); // Selects which fields from the input should be written. In this case, fields "make" and "model" will be empty // The field selection is not case sensitive settings.selectFields("description", "price", "year"); //configures the writer process java beans with annotations (assume TestBean has a few annotated fiedls) settings.setRowWriterProcessor(new BeanWriterProcessor<TestBean>(TestBean.class)); // Creates a writer with the above settings; CsvWriter writer = new CsvWriter(new File("/path/to/output.csv"), settings); // Writes the headers specified in the settings writer.writeHeaders(); //creates a bean instance for writing TestBean bean = new TestBean(); bean.setPrice(new BigDecimal("500.33")); bean.setDescription("Blah,blah"); bean.setYear(1997); //writes it writer.processRecord(bean); writer.close();