csv save data only in one cell instead of a row

By : JDEV
Source: Stackoverflow.com
Question!

I have the following code which writes data to the CSV file. But instead of writing the headers each in different cell it writes all in the same cell. When i change to "UTF-8" it works fine but writs "Gibberish" instead of correct letters when opened in Office Excel.

        CSVWriter csvWrite = new CSVWriter(new OutputStreamWriter(new FileOutputStream(file),
                "UTF-16"), CSVWriter.DEFAULT_SEPARATOR ,CSVWriter.NO_QUOTE_CHARACTER,
                CSVWriter.DEFAULT_ESCAPE_CHARACTER, CSVWriter.DEFAULT_LINE_END);

        //Add Columns headers to the excel file
        String columnHeaders[] = {"אאא","בבב","גגג"};
        csvWrite.writeNext(columnHeaders);

        //Collect Data
        ArrayList<String[]> listData = collectDataFromDBToList();
        //Write/Add all line from list to the csv file
        csvWrite.writeAll(listData);

        csvWrite.close();

Received output

pic press here

Wanted output

pic press here

By : JDEV


Answers

Thank phoenix but it did not helped. I found the solution :) , it was just to use tab separator instead of the default one.

use: '\t' instead of CSVWriter.DEFAULT_SEPARATOR

CSVWriter csvWrite = new CSVWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-16"),'\t' ,CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.DEFAULT_ESCAPE_CHARACTER, CSVWriter.DEFAULT_LINE_END);



Here is this code that i wrote for a project..the values outside for loop ( "WSDL Name" and "Tags" are the column header and list will contain values to be inserted). It will generate test.csv

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

public class genCSV
{
   public static void main(String [] args)
   {
       generateCsvFile("c:\\test.csv"); 
   }

   private static void generateCsvFile(String sFileName)
   {
    try
    {
        FileWriter writer = new FileWriter(sFileName);

    writer.append("WSDLName");
    writer.append("Tags");
    writer.append('\n');

   for(int i=0;i<list.size();i++){
       writer.append(list.getKey);
        writer.append(list.getValueForKey);
        writer.append('\n');
   }

    //generate whatever data you want

    writer.flush();
    writer.close();
}
catch(IOException e)
{
     e.printStackTrace();
} 
}
}
By : phoenix


There may be a chance of string value in the column RVU.MA. Try out with the below query to find out such descrepancies.

   SELECT *
   FROM RVU
   WHERE ISNUMERIC (RVU.MA)!=1


This video can help you solving your question :)
By: admin