//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
public static <T> void writeBeans(String filePath, CsvWriterSettings csvWriterSettings, Iterable<T> records, Class<T> cls) throws FileNotFoundException{ BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath)); csvWriterSettings.setRowWriterProcessor(new BeanWriterProcessor<T>(cls)); CsvWriter csvWriter = new CsvWriter(bos, csvWriterSettings); csvWriter.writeHeaders(); csvWriter.processRecords(records); csvWriter.close(); }
writerProcessor.convertAll(Conversions.toBoolean("T", "F")); // will write "T" and "F" instead of "true" and "false" writerSettings.setRowWriterProcessor(writerProcessor);
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();
OutputValueSwitch writerSwitch = new OutputValueSwitch(0); //row identifiers go at column 0 // If the value is "ROWHEAD", we want to use an BeanWriterProcessor. You can provide field names to be associated with the fields in the class. writerSwitch.addSwitchForValue("ROWHEAD", new BeanWriterProcessor(RowHead.class)); writerSwitch.addSwitchForValue("SECHEAD", new BeanWriterProcessor(SecHead.class)); // If the value is "SECBODY", a ObjectRowWriterProcessor will be used. Let's assume you are writing object arrays here writerSwitch.addSwitchForValue("SECBODY", new ObjectRowWriterProcessor()); //...and so on. //Configure the CSV writer here CsvWriterSettings settings = new CsvWriterSettings(); // the writer should use the switch defined above settings.setRowWriterProcessor(writerSwitch); settings.getFormat().setLineSeparator("\n"); settings.setHeaderWritingEnabled(false); //etc //Create the CSV writer CsvWriter writer = new CsvWriter(new File("/path/to/your.csv"), "UTF-8", settings); writer.processRecord(new RowHead()); //writing bean writer.processRecord(new SecHead()); //writing the other bean writer.processRecord(new Object[]{"SECBODY", "Value 1", "Value 2", "etc"}); //writing an array writer.close();