/* * [CSVToTable.java] * * Summary: Converts a CSV file to the guts of an HTML table. Output appears in xxx.html. * * Copyright: (c) 2011-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2011-01-23 initial version. * 1.1 2011-01-25 allow optional css classes on command line, encoding. * 1.2 2011-02-14 no longer entify. Do separately with Entify. * 1.3 2013-01-30 now specify class to * Use: java.exe com.mindprod.CSVToTable xxx.csv * Awkward characters will appear as Entities. * * @author Roedy Green, Canadian Mind Products * @version 1.3 2013-01-30 now specify class to or 0 ) { sb.append( "" ); } else { sb.append( "" ); } for ( int i = 0; i < fields.length; i++ ) { final String field = fields[ i ]; int j = i + 1; if ( j < cssClasses.length && cssClasses[ j ].length() > 0 ) { sb.append( "" ); } else { sb.append( "" ); } if ( ST.isNumeric( field ) ) { final long x = Long.parseLong( field ); sb.append( DF.format( x ) ); } else { sb.append( field ); } sb.append( "" ); } sb.append( "\n" ); w.print( sb.toString() ); } } catch ( EOFException e ) { out.println( r.lineCount() + " csv lines converted to table rows." ); r.close(); w.close(); } } /** * Simple command line interface to CSVToTable. Converts one CSV file to an HTML table. Must have * extension .csv
Use java com.mindprod.CSVToTable somefile.csv. * You may optionally provide CSS classes for each column of the table. "" means no css class for that column. * You can use CSVToTable constructor in your own programs. * * @param args name of csv file to remove excess quotes and space */ public static void main( String[] args ) { if ( args.length < 1 ) { throw new IllegalArgumentException( USAGE ); } String filename = args[ 0 ]; if ( !filename.endsWith( ".csv" ) ) { throw new IllegalArgumentException( "Bad Extension\n" + USAGE ); } final File file = new File( filename ); final String[] cssClasses = new String[ args.length - 1 ]; System.arraycopy( args, 1, cssClasses, 0, args.length - 1 ); try { // file, separatorChar, quoteChar, commentChar new CSVToTable( file, ',', '\"', "#", CSV.UTF8, cssClasses ); } catch ( IOException e ) { err.println(); e.printStackTrace( err ); err.println( "CSVToTable failed to export" + EIO.getCanOrAbsPath( file ) ); err.println(); } } // end main }