1

so what i am trying to accomplish is to write an array of data down each cell of the column for as many array elements that i have. i want i too look like this in excel:

buyer | parts |  price| quantity  | comments
tom   | blah  | 546   | 5         |   blah
      |  blah | 56    |  67       |

my file has manyyyyy more columns than this but i wanted to give a brief example. what also creates my problem is that i dont want to print the next column after it on the last row that the writer is on once its done my array data set, so i want it to be on that forst line. i have looked into other libraries for accomplishing this, but there is nothing that i can find that seems to meet my needs. i do understand the nature of CSV file may not allow this to happen, without manually adding in blank cells, but is there any other way to do this?

Enumeration <String> paramNames = request.getParameterNames();
        while(paramNames.hasMoreElements()) 
        {
            String paramName = (String)paramNames.nextElement();
                            writer.append(paramName);
            writer.append(',');
            String[] paramValues = request.getParameterValues(paramName);
            if (paramValues.length == 1)
            {
                String paramValue = paramValues[0];
                if (paramValue.length() == 0)
                {
                    writer.append("No Value");
                    writer.append('\n');
                }
                else
                {
                    writer.append(paramValue);
                    writer.append('\n');
                }
            }
            else
            {
                for(int i = 0; i<paramValues.length; i++)
                {
                    writer.append(paramValues[i]);
                    writer.append(',');
                }
                writer.append('\n');
            }
            previousname = paramName;

        }
        writer.flush();
        writer.close();
    }

also to clarify where this data is coming from, a JSP table thats in the middle of the form, so i am reading in arrays of values that are in the table.

6
  • Are you saying you want to write all the rows for each column. E.g. all the buyers, then all the titles, the all the ... Commented Jul 31, 2012 at 17:24
  • well like in this case i would have one buyer, he could order multiple parts, and i would write each column as i go along the columns. basically the data im getting would be an invoice type of thing Commented Jul 31, 2012 at 17:29
  • Can you please explain better the data structures you have already in use? Do you have one array with all of this data somehow inside, or 5 arrays, one for each column? Or an Array<SomeClass> that contains the data? Commented Jul 31, 2012 at 17:31
  • String[] paramValues = request.getParameterValues(paramName); is the code that gets my values from my html form. so usually there is only one value, but sometimes there are multiple in the case of a table Commented Jul 31, 2012 at 17:32
  • 1
    I don't think a CSV library will let you write data by columns easily. (If only to avoid having to buffer the whole output.) You'll have to build the rows you want to output in memory, with empty cells as needed, and then write those to a CSV. Commented Jul 31, 2012 at 17:37

2 Answers 2

1

from : http://javacodeonline.blogspot.ca/2009/09/java-code-to-write-to-csv-file.html

/** The below Java Code writes 
 * some data to a file in CSV
 * (Comma Separated Value) File
 */

package developer;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class MakeCSVFile {

    public static void main(String[] args) throws IOException {

        //Note the "\\" used in the path of the file 
        //instead of "\", this is required to read 
        //the path in the String format.
        FileWriter fw = new FileWriter("D:\\JavaCodeFile\\WriteTest.csv");
        PrintWriter pw = new PrintWriter(fw);

        //Write to file for the first row
        pw.print("Hello guys");
        pw.print(",");
        pw.print("Java Code Online is maing");
        pw.print(",");
        pw.println("a csv file and now a new line is going to come");

        //Write to file for the second row
        pw.print("Hey");
        pw.print(",");
        pw.print("It's a");
        pw.print(",");
        pw.print("New Line");

        //Flush the output to the file
        pw.flush();

        //Close the Print Writer
        pw.close();

        //Close the File Writer
        fw.close();        
    }
}

Probably instead of doing hard print do a loop of the array and print each value separated by a comma.

Something like :

Array array;

    Iterator<String> iter = array.iterator();
    while (iter.hasNext())
    {
        object = iter.next();
        pw.print(object.getBuyer());
        pw.print(",");
        pw.print(object.getTitle());
        ..          
    }
Sign up to request clarification or add additional context in comments.

6 Comments

i have the code currently and it prints things perfectly when i just use a vertical list...but i need to have as a horizontal list....making the multiple values thing an issue
I think you need to expose us your data structure. from what I understand each buyer might have multiple part, best way I think is that for each part enter a line but with the buyer again.
String[] paramValues = request.getParameterValues(paramName); thats how i get all of the values from the form, usually it only has one because most of the things it handles are textfields, but when it comes to the table in the middle of the form, it now can have multiple values returned using that function.
i added my code that i currently have for printing my data verticall in my CSV file
You can enter the same value for each of those line (like buyer have many part, you put the same buyer for each part) or like millimoose said you can use blank to fills the other column. Is using a csv a requirement ? check with your client the format perhaps it will help. You can either try to put that in a db instead (sqllite might be perfect for your case) or save it in a json or xml format.
|
0

If you are writing CSV files with Java, consider using the OpenCSV library.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.