Search This Site
Sep 02 2010 23:06 UTC | ||||||||||
Tutorials
CSV ManagerRatingsQuality
Recommendations
Questions
Got a question for us? Bookmarks |
Getting Started
CSV Manager is a Java component that makes it easy to save and load
text files containing data in a CSV (Comma Separated Values) format.
This includes not only Excel files saved as CSV, but also any plain
text format with separated data fields, such as tab separated data and
the UNIX
You use CSV Manager by calling the methods of the
Update Your Classpath
For installation instructions you should read the Installation Guide.
Assuming you have unzipped the CSV Manager download file into a folder of your choice, simply
copy the Loading CSV Data
To load CSV data using CSV Manager, you create a new instance of the
Here is a complete code example. You can try this example out by compiling the
ExampleOne.java
import com.ricebridge.csvman.CsvManager;
import java.util.*;
import java.io.*;
public class ExampleOne {
public static void main( String[] args ) throws Exception {
CsvManager csvManager = new CsvManager();
List data = csvManager.load( "example.csv" );
StringBuffer htmlTable = new StringBuffer("<html><body><table>\n");
for( int line = 0; line < data.size(); line++ ) {
htmlTable.append( "<tr>\n" );
String[] fields = (String[]) data.get(line);
for( int field = 0; field < fields.length; field++ ) {
String celltype = (0==line?"th":"td");
htmlTable.append( "<"+celltype+" align=\"left\">"+fields[field]+"</"+celltype+">\n" );
}
htmlTable.append( "</tr>\n" );
}
htmlTable.append( "</table></body></html>\n" );
FileWriter htmlFile = new FileWriter( "example.htm" );
htmlFile.write( htmlTable.toString() );
htmlFile.close();
}
}
This class generates a HTML table (saved to the file CsvManager csvManager = new CsvManager();
List data = csvManager.load( "example.csv" );
All you need to do to load CSV data is to create a new
The CSV data is returned to you as a
To load your data from a file,
If your CSV data does not come from a file, then you can also provide it using a
Saving CSV Data
To save CSV data using CSV Manager, you need an instance of the
Here is a complete code example. You can try this example out by compiling the
ExampleTwo.java
import com.ricebridge.csvman.CsvManager;
import java.util.*;
import java.io.*;
public class ExampleTwo {
public static void main( String[] args ) throws Exception {
CsvManager csvManager = new CsvManager();
// create some test data to save
List data = new ArrayList();
data.add( new String[] {"Integer", "Even", "Odd", "Square"} );
for( int line = 0; line < 10; line++ ) {
String[] fields
= new String[] { ""+line, ""+(line*2), ""+(line*2+1), ""+(line*line) };
data.add( fields );
}
csvManager.save( "integers.csv", data );
}
}
This class creates a CSV file ( CsvManager csvManager = new CsvManager();
...
csvManager.save( "integers.csv", data );
All you need to do to save CSV data is to use a
You provide the CSV data as a
To save your data to a file,
If you need to save your CSV to something that is not a file, then you can also send the data to a
Importing and Exporting CSV Data from a Database
One of the most practical applications of the CSV file format is to transfer table data between
databases in a platform-independent manner. CSV Manager makes this really easy. For this example,
assume we have a database table ( product.sql
CREATE TABLE product ( id integer primary key, code varchar(20), name varchar(255), price integer ); The data for this table is in the products.csv
id,code,name,price 1,A01,Apple,99 2,O01,Orange,149 3,O02,Satsuma,219 4,B01,Banana,29 5,P01,Pear,129
We want to import this data into the database table ExampleThree.java
import com.ricebridge.csvman.CsvManager;
import java.sql.*;
import java.util.*;
import java.io.*;
public class ExampleThree {
public static void main( String[] args ) throws Exception {
CsvManager csvManager = new CsvManager();
// edit these for your local database
String dburl
= "jdbc:mysql://localhost/mydatabase?"
+ "user=username;password=password";
Driver d
= (Driver) Class.forName( "org.gjt.mm.mysql.Driver" ).newInstance();
DriverManager.registerDriver( d );
Connection con = DriverManager.getConnection(dburl);
// import data into database (first line is column names, so start from 1)
PreparedStatement ps = con.prepareStatement( "INSERT INTO Product VALUES (?,?,?,?)" );
List data = csvManager.load( "products.csv" );
for( int line = 1; line < data.size(); line++ ) {
String[] fields = (String[]) data.get(line);
ps.setInt( 1, Integer.parseInt( fields[0] ) );
ps.setString( 2, fields[1] );
ps.setString( 3, fields[2] );
ps.setInt( 4, Integer.parseInt( fields[3] ) );
ps.executeUpdate();
}
// export data from database
ps = con.prepareStatement( "SELECT * FROM Product" );
ResultSet rs = ps.executeQuery();
csvManager.save( "products-export.csv", rs, true );
}
}
This class imports the data from CsvManager csvManager = new CsvManager();
...
List data = csvManager.load( "products.csv" );
...
csvManager.save( "products-export.csv", rs, true );
So to convert data in a database to and from CSV format, all you need to do is use the standard JDBC API calls
and the Setting the CSV File FormatCSV Manager uses the Excel CSV file format by default. While this covers most CSV files, you may encounter files that use a different format. CSV Manager has a number of settings for files that use a format similar to CSV, and various settings can be altered to suit the file in question.
The most common setting is for the separator characters - the characters that
separate data fields. By default this is a comma (
Another important issue relates to end-of-line characters on different
platforms. CSV Manager will handle Windows, UNIX and Mac files out of
the box, but you may wish to make the end-of-file characters explicit
using Data fields that contain separator, end-of-line or quote characters must be handled carefully.
By default, any such data fields are enclosed in quotes (
You can set the quote character with
More Information
This Getting Started guide only covers the basics of what you can do with CSV Manager.
For more information on settings, refer to the reference documentation.
For a list of examples, including a Swing example with
Please review the licensing terms and
conditions under which the Ricebridge CSV Manager component can be
used. We're always happy to come up with licensing solutions that suit
your needs, so please contact us by mail at
Thanks for using CSV Manager! If you have any problems or questions feel free to mail us at
| |||||||||
|
comment on this page
Home |
Search |
About Us |
Contact Us |
Our Products |
Documentation |
Resources |
Login
Copyright © 2004-2010 Ricebridge. All Rights Reserved. | ||||||||||