Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Java Databases Discussion specific to working with Java Databases. For other Java topics, please see related Java forums. For database discussions not specific to Java, please see the Database category.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old December 13th, 2005, 06:58 PM
Registered User
 
Join Date: Dec 2005
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default save as CSV file

Hi All,

i'm using a program that retrives result from Database using JDBC conncetion. I would like to save the result in RecordSet in a CSV file. It'll be great if someone can help me out with this.

or, i'm displaying all the contents in a Applet TextArea, so if anyone has any idea on how to save the content of a TextArea as a CSV file, please let me know..

Thanks,

ng.
  #2 (permalink)  
Old December 21st, 2005, 07:26 AM
Registered User
 
Join Date: Dec 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I wanted to do the same just a few days ago and came up with my own method to transform a given 2-dimensional array of Strings representing the content to be transformed into CSV. Database records are basically nothing else than such an array. Note that this method changes the supplied array instead of working on its own copy (call by reference).

Basically all you have to do is to quote existing quotes in the original cell and, if it contains the separator for CSV records (usually a semicolon or comma), to quote the whole cell as well.

Code:
public void formatData(String[][] source)

{

    if (source != null)

    {

        int width = source.length;


        for (int i = 0; i < width; i++)

        {

            int height = source[i].length;

            int lastCol = height - 1;



            for (int j = 0; j < height; j++)

            {

                String cell = source[i][j];



                if (cell == null)

                {

                    cell = EMPTY_STRING;

                }

                else

                {

                    // quote existing quotes

                    cell = cell.replaceAll(QUOTE, QUOTE + QUOTE);



                    // quote cell data if it contains the separator

                    if (cell.contains(SEPARATOR))

                    {

                        cell = QUOTE + cell + QUOTE;

                    }

                }



                // add separator to cells that are not in the last column

                if (j < lastCol)

                {

                    cell += SEPARATOR;

                }



                source[i][j] = cell.trim();

            }

        }



    }

}


Similar Threads
Thread Thread Starter Forum Replies Last Post
save .csv with newline character sansircar ASP.NET 1.0 and 1.1 Professional 1 November 19th, 2008 03:50 AM
Read CSV file - Save Columns into Rows pintoo BOOK: Professional SQL Server 2005 Integration Services ISBN: 0-7645-8435-9 1 August 20th, 2008 09:01 AM
Save a file in CSV format thru Excel VBA macro roli2712 Excel VBA 1 July 11th, 2007 03:05 AM
save a PDF file as text file through VB. NET kvenkatu VB.NET 0 April 6th, 2006 12:15 PM
save file in csv, txt format in jsp URGENT ernidhi2003 JSP Basics 1 October 14th, 2004 04:59 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.