List of usage examples for org.apache.commons.csv CSVFormat getHeaderComments
public String[] getHeaderComments()
From source file:fabrice.CSVPrinter.java
/** * Creates a printer that will print values to the given stream following the CSVFormat. * <p>/*w ww . j av a 2s . co m*/ * Currently, only a pure encapsulation format or a pure escaping format is supported. Hybrid formats (encapsulation * and escaping with a different character) are not supported. * </p> * * @param out * stream to which to print. Must not be null. * @param format * the CSV format. Must not be null. * @throws IOException * thrown if the optional header cannot be printed. * @throws IllegalArgumentException * thrown if the parameters of the format are inconsistent or if either out or format are null. */ public CSVPrinter(final Appendable out, final CSVFormat format) throws IOException { if (out == null) { throw new IllegalArgumentException("Parameter out must not be null!"); } if (format == null) { throw new IllegalArgumentException("Parameter format must not be null!"); } this.out = out; this.format = format; // TODO: Is it a good idea to do this here instead of on the first call to a print method? // It seems a pain to have to track whether the header has already been printed or not. if (format.getHeaderComments() != null) { for (String line : format.getHeaderComments()) { if (line != null) { this.printComment(line); } } } if (format.getHeader() != null) { this.printRecord((Object[]) format.getHeader()); } }
From source file:edu.harvard.mcz.imagecapture.RunnableJobReportDialog.java
protected void serializeTableModel() { PrintWriter out = null;// ww w . ja v a 2 s. c o m CSVPrinter writer = null; try { int cols = jTable.getModel().getColumnCount(); CSVFormat csvFormat = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL) .withHeaderComments(jTextArea.getText()); TableModel model = jTable.getModel(); switch (cols) { case 9: csvFormat = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL) .withHeader(model.getColumnName(0), model.getColumnName(1), model.getColumnName(2), model.getColumnName(3), model.getColumnName(4), model.getColumnName(5), model.getColumnName(6), model.getColumnName(7), model.getColumnName(8)) .withCommentMarker('*').withHeaderComments(jTextArea.getText()); break; case 6: csvFormat = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL) .withHeader(model.getColumnName(0), model.getColumnName(1), model.getColumnName(2), model.getColumnName(3), model.getColumnName(4), model.getColumnName(5)) .withCommentMarker('*').withHeaderComments(jTextArea.getText()); break; case 5: csvFormat = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL) .withHeader(model.getColumnName(0), model.getColumnName(1), model.getColumnName(2), model.getColumnName(3), model.getColumnName(4)) .withCommentMarker('*').withHeaderComments(jTextArea.getText()); break; case 4: csvFormat = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL) .withHeader(model.getColumnName(0), model.getColumnName(1), model.getColumnName(2), model.getColumnName(3)) .withCommentMarker('*').withHeaderComments(jTextArea.getText()); break; } log.debug(jTextArea.getText()); log.debug(csvFormat.getHeaderComments()); Date now = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmdd_HHmmss"); String time = dateFormat.format(now); String filename = "jobreport_" + time + ".csv"; out = new PrintWriter(filename); writer = new CSVPrinter(out, csvFormat); writer.flush(); int rows = jTable.getModel().getRowCount(); for (int i = 0; i < rows; i++) { ArrayList<String> values = new ArrayList<String>(); for (int col = 0; col < cols; col++) { values.add((String) jTable.getModel().getValueAt(i, col)); } writer.printRecord(values); } writer.flush(); writer.close(); JOptionPane.showMessageDialog(Singleton.getSingletonInstance().getMainFrame(), "Saved report to file: " + filename, "Report to CSV file", JOptionPane.OK_OPTION); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { out.close(); } catch (Exception e) { } try { writer.close(); } catch (Exception e) { } } }