public String getLineEnd() { return new String(settings.getFormat().getLineSeparator()); } }
public char getEscapeChar() { return settings.getFormat().getQuoteEscape(); }
public char getSeparator() { return settings.getFormat().getDelimiter(); }
public char getQuoteCharacter() { return settings.getFormat().getQuote(); }
/** * Private constructor to prevent instantiation */ public CsvWriter(Table table, CsvWriteOptions options) { this.table = table; this.header = options.header(); this.writer = options.writer(); this.settings = new CsvWriterSettings(); // Sets the character sequence to write for the values that are null. settings.setNullValue(nullValue); settings.getFormat().setDelimiter(options.separator()); settings.getFormat().setQuote(options.quoteChar()); settings.getFormat().setQuoteEscape(options.escapeChar()); settings.getFormat().setLineSeparator(options.lineEnd()); // writes empty lines as well. settings.setSkipEmptyLines(false); }
public String getLineEnd() { return new String(settings.getFormat().getLineSeparator()); } }
public char getEscapeChar() { return settings.getFormat().getQuoteEscape(); }
public char getSeparator() { return settings.getFormat().getDelimiter(); }
public char getQuoteCharacter() { return settings.getFormat().getQuote(); }
ResultSet resultSet = dbase.getResultSet(); // Configure the output format as needed before actually dumping the data: CsvWriterSettings writerSettings = new CsvWriterSettings(); //many settings here, check the tutorials & examples. writerSettings.getFormat().setLineSeparator("\n"); writerSettings.setHeaderWritingEnabled(true); // we want the column names to be printed out as well. // Then create a routines object: CsvRoutines routines = new CsvRoutines(writerSettings); // The write() method takes care of everything. The resultSet and any other resources required are closed by the routine. routines.write(resultSet, new File(fileName), "UTF-8");
CsvWriterSettings settings = new CsvWriterSettings(); //many options here. Check the documentation settings.getFormat().setDelimiter('|'); CsvWriter writer = new CsvWriter(new FileWriter(new File("path/to/output.csv")), settings); writer.writeHeaders("Header A", "Header B"); //write rows individually writer.writeRow("string 1", "string 2"); writer.writeRow(234, new BigDecimal(111)); //or write a list of rows in bulk List<Object[]> yourRows = yourMethodToBuildRowsWithData(); writer.writeRowsAndClose(yourRows);
/** * Provides a basic CSV configuration that allows writing CSV files that can be read by Microsoft Excel. * * @return a pre-configured {@link CsvWriterSettings} object with suggested settings for generating * CSV files that can be read by Microsoft Excel. */ public static CsvWriterSettings writeExcel() { CsvWriterSettings settings = new CsvWriterSettings(); settings.getFormat().setLineSeparator("\r\n"); settings.getFormat().setComment('\0'); settings.setEmptyValue(null); settings.setSkipEmptyLines(false); settings.trimValues(false); return settings; }
/** * Private constructor to prevent instantiation */ public CsvWriter(Table table, CsvWriteOptions options) { this.table = table; this.header = options.header(); this.writer = options.writer(); this.settings = new CsvWriterSettings(); // Sets the character sequence to write for the values that are null. settings.setNullValue(nullValue); settings.getFormat().setDelimiter(options.separator()); settings.getFormat().setQuote(options.quoteChar()); settings.getFormat().setQuoteEscape(options.escapeChar()); settings.getFormat().setLineSeparator(options.lineEnd()); // writes empty lines as well. settings.setSkipEmptyLines(false); }
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();