public String tableToCsv(String url) throws IOException { Document doc = Jsoup.connect(url).get(); Elements tables = doc.select("table"); if (tables.size() != 1) { throw new IllegalStateException( "Reading html to table currently works if there is exactly 1 html table on the page. " + " The URL you passed has " + tables.size() + ". You may file a feature request with the URL if you'd like your pagae to be supported"); } Element table = tables.get(0); CsvWriterSettings settings = new CsvWriterSettings(); StringWriter stringWriter = new StringWriter(); CsvWriter csvWriter = new CsvWriter(stringWriter, settings); for (Element row : table.select("tr")) { Elements headerCells = row.getElementsByTag("th"); Elements cells = row.getElementsByTag("td"); String[] nextLine = Stream.concat(headerCells.stream(), cells.stream()) .map(Element::text).toArray(String[]::new); csvWriter.writeRow(nextLine); } return stringWriter.toString(); } }
/** * 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); }
/** * 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; }
//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);
List<IColumn> cols = table.getLoadedColumns(schema.getColumnNames()); CsvWriterSettings settings = new CsvWriterSettings(); CsvFormat format = new CsvFormat(); format.setDelimiter(this.separator); settings.setFormat(format); settings.setEmptyValue("\"\""); settings.setNullValue(null);
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();
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");
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();
CsvParserSettings parserSettings = new CsvParserSettings(); //many options here, check the tutorial. parserSettings.detectFormatAutomatically(); CsvParser parser = new CsvParser(parserSettings); //parse and get the detected format List<String[]> parsedRows = parser.parseAll(new File("/path/to/input.csv")); CsvFormat detectedFormat = parser.getDetectedFormat(); //now, write using the detected format: CsvWriterSettings writerSettings = new CsvWriterSettings(); writerSettings.setFormat(detectedFormat); CsvWriter writer = new CsvWriter(new File("/path/to/output.csv"), writerSettings); writer.writeStringRowsAndClose(parsedRows);
... // 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); }
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();
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);
/** * {@inheritDoc} */ @Override public void open(final Writer writer) { final CsvWriterSettings csvWriterSettings = new CsvWriterSettings(); csvWriterSettings.setQuoteAllFields(true); csvWriter = new CsvWriter(writer, csvWriterSettings); if (headers == null) { headers = HEADERS; } //We need the null reference check since the config may be null when called from JUnit tests //if (config.getProperties() != null) { // String hashName = config.getProperties().getProperty("profile.hashAlgorithm").toString().toUpperCase(); // headers[HASH_ARRAY_INDEX] = String.format(headers[HASH_ARRAY_INDEX], hashName); //} csvWriter.writeHeaders(headers); }
CsvFormat format = settings.getFormat(); this.multiDelimiter = format.getDelimiterString().toCharArray(); if (multiDelimiter.length == 1) { this.escapeEscape = settings.getFormat().getCharToEscapeQuoteEscaping(); this.newLine = format.getNormalizedNewline(); this.quoteAllFields = settings.getQuoteAllFields(); this.escapeUnquoted = settings.isEscapeUnquotedValues(); this.inputNotEscaped = !settings.isInputEscaped(); this.dontProcessNormalizedNewLines = !settings.isNormalizeLineEndingsWithinQuotes(); quotedFieldSelector = settings.getQuotedFieldSelector(); int triggerCount = settings.getQuotationTriggers().length; int offset = settings.isQuoteEscapingEnabled() ? 1 : 0; char[] tmp = Arrays.copyOf(settings.getQuotationTriggers(), triggerCount + offset); if (offset == 1) { tmp[triggerCount] = quoteChar;
public String getLineEnd() { return new String(settings.getFormat().getLineSeparator()); } }
/** * {@inheritDoc} */ @Override protected void configureWriterSettings(CsvWriterSettings settings) { super.configureWriterSettings(settings); if (quoteAllFields != null) { settings.setQuoteAllFields(quoteAllFields); } }
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 void writeToStream(DataFrame df, Writer out) throws IOException { String[] columnNames = df.getSchema().getColumnNames().toArray(new String[0]); final CsvWriterSettings settings = new CsvWriterSettings(); settings.setFormat(format); CsvWriter writer = new CsvWriter(out, settings); writer.writeHeaders(columnNames); List<Row> rows = df.getRows(); for (Row curRow : rows) { List<Object> rowValues = curRow.getVals(); writer.writeRow(rowValues); } writer.close(); } }
public char getEscapeChar() { return settings.getFormat().getQuoteEscape(); }
@Override protected String assemble(List<PolicyStats> results) { StringWriter output = new StringWriter(); CsvWriter writer = new CsvWriter(output, new CsvWriterSettings()); writer.writeHeaders(headers()); for (PolicyStats policyStats : results) { Object[] data = new Object[] { policyStats.name(), String.format("%.2f", 100 * policyStats.hitRate()), policyStats.hitCount(), policyStats.missCount(), policyStats.requestCount(), policyStats.evictionCount(), String.format("%.2f", 100 * policyStats.admissionRate()), (policyStats.operationCount() == 0) ? null : policyStats.operationCount(), policyStats.stopwatch().elapsed(TimeUnit.MILLISECONDS) }; writer.writeRow(data); } writer.close(); return output.toString(); } }