Abdul Manaf
Abdul Manaf

Reputation: 13

Alignment Issue in .csv File

I am working on writing of data into CSV file using Java. How can I give left alignment to each column in CSV File?

public class WriteToFileCsv
{
    public static void main(String[] args) throws IOException
    {
       FileWriter fw = new FileWriter("WriteTest.csv");
       PrintWriter out = new PrintWriter(fw);

       out.print("This");
       out.print(",");
       out.print("is");
       out.print(",");

       out.print("It's");
       out.print(",");
       out.print("really");
       out.print(",");

       out.flush();      
       out.close();
       fw.close();       
    }
}

Upvotes: 1

Views: 4221

Answers (3)

Scary Wombat
Scary Wombat

Reputation: 44854

To have alignment in you csv file you need to create an excel file. Try Apache POI

http://poi.apache.org/

Upvotes: 0

Rudy
Rudy

Reputation: 7044

You are referring to Fixed-Length data format. CSV is not meant to have alignment.

Upvotes: 1

Keppil
Keppil

Reputation: 46239

You can choose a width for the columns, e.g. 10, and then use format():

int width = 10;
String columnFormat = "%-" + width + "s";
out.format(columnFormat, "This");
out.print(",");
out.format(columnFormat, "is");
out.print(",");
...

Upvotes: 3

Related Questions