Java code to set column width in CSV file

Can someone help on how to set the column width in CSV file using javacode.
I have used FileWriter class to upload data into the csv file. Results are getting populated properly but csv file showing results with default column width. Is there a way we can set the width of the column dynamically based on result length or set fixed length?

Questions by 19swapna84

Showing Answers 1 - 6 of 6 Answers

Account,LastName,FirstName,Balance,CreditLimit,AccountCreated,Rating
101,Reeves,Keanu,9315.45,10000.00,1/17/1998,A
312,Butler,Gerard,90.00,1000.00,8/6/2003,B
868,Hewitt,Jennifer Love,0,17000.00,5/25/1985,B
761,Pinkett-Smith,Jada,49654.87,100000.00,12/5/2006,A
317,Murray,Bill,789.65,5000.00,2/5/2007,C


Code
  1. package com.northconcepts.datapipeline.examples.cookbook;

  2.  

  3. import java.io.File;

  4.  

  5. import com.northconcepts.datapipeline.core.DataReader;

  6. import com.northconcepts.datapipeline.csv.CSVReader;

  7. import com.northconcepts.datapipeline.fixedwidth.FixedWidthWriter;

  8. import com.northconcepts.datapipeline.job.JobTemplate;

  9.  

  10. public class WriteACsvFileToFixedWidth {

  11.      

  12.     public static void main(String[] args) throws Throwable {

  13.         DataReader reader = new CSVReader(new File("example/data/input/credit-balance-01.csv"))

  14.             .setFieldNamesInFirstRow(true);

  15.          

  16.         FixedWidthWriter writer = new  FixedWidthWriter(new File("example/data/output/credit-balance-02.fw"));

  17.         writer.addFields(8);

  18.         writer.addFields(16);

  19.         writer.addFields(16);

  20.         writer.addFields(12);

  21.         writer.addFields(14);

  22.         writer.addFields(16);

  23.         writer.addFields(7);

  24.          

  25.         JobTemplate.DEFAULT.transfer(reader, writer);

  26.     }

  27.  

  28. }


  Was this answer useful?  Yes

Dusmanta

  • Nov 12th, 2014
 

Use Apachae POI jar for this
HSSFSheet sheet = workBook.createSheet("testXls");

Code
  1. // Code to Auto size the column widths

  2.         for(int columnPosition = 0; columnPosition< 5; columnPosition++) {

  3.              sheet.autoSizeColumn((short) (columnPosition));

  4.         }

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions