skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Saturday, October 27, 2012

Excel Data To Delimited File Utility

Posted by Raju Gupta at 7:30 PM – 0 comments
 

A windows based File converter utility has been developed (with Java) to convert an excel file into a delimited format.

This utility takes two inputs ,need to be provided by the user :
1) The path of the excel file (.xls/.xlt) required to be converted
2) Delimiter to be used (For example : pipe (|), comma (,), dollar ($) )

A delimited file named output_<worksheet>.txt is created ,on the same location as of input excel file. Also ,the utility is able to read multiple worksheets of the excel and the output file(s) should be opened with notepad.   

/*GetUserInput.java*/


import java.io.*;

public class GetUserInput
{  
 /*Method to take excel file path and name input from user*/
 String input()
 {
  System.out.print("Enter excel file name in the pattern C:\FolderName\filename " + "n");
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  String inputfile = null;
  try
  {
   inputfile = br.readLine();
  }catch (IOException e) 
   {         
   System.out.println("Error in input file!");        
   System.exit(1);      
   }
  return inputfile;
 }/*Method input closed*/
 
 /*Method to take delimiter to be used from user*/
 String delimiter()
 {
  System.out.print("Enter delimiter to be used " + "n");
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  String delimiter = null;
  try
  {
   delimiter = br.readLine();
  }catch (IOException e) 
  {         
   System.out.println("Error in delimiter!");        
   System.exit(1);      
  }
  return delimiter;
 }/*Method delimiter closed*/
}/*Class closed*/

/****************************************************************************************/
/*ConvertExcelToCSV.java*/

import java.io.*;
import jxl.*;
import java.util.*;
class  ConvertExcelToDelimited
{
 public static void main(String[] args)
 {
  try
  {
   /*To Get the input excel name from user*/
   GetUserInput input1 = new GetUserInput();
   String filename = null;
   filename = input1.input();
   
   /*To Get delimiter to be used from user*/
   GetUserInput input2 = new GetUserInput();
   String user_delimiter = null;
   user_delimiter= input2.delimiter();
   
   /*To Get the output file path from the input excel name*/ 
   File outpath = new File(filename);
   String outputpath = null;
   outputpath = outpath.getParent();
   

   WorkbookSettings ws = new WorkbookSettings();
   ws.setLocale(new Locale("en", "EN"));
   Workbook w = Workbook.getWorkbook(new File(filename),ws);
   
   for (int sheet = 0; sheet < w.getNumberOfSheets(); sheet++)
   {
    /*Objects :
     * 1. To read each sheet 
     * 2. Create output files as per the sheet name in the excel */
    Sheet s = w.getSheet(sheet);
    File f = new File(outputpath+"//output_" + s.getName() + ".txt");
    
    OutputStream os = (OutputStream)new FileOutputStream(f);
    String encoding = "UTF8";
    OutputStreamWriter osw = new OutputStreamWriter(os, encoding);
    BufferedWriter bw = new BufferedWriter(osw);
    
    Cell[] row = null;
    for (int i = 0 ; i < s.getRows() ; i++)
    {
     row = s.getRow(i);
     if (row.length > 0)
     {
      bw.write(row[0].getContents());
      for (int j = 1; j < row.length; j++)
      {
       bw.write(user_delimiter);
       bw.write(row[j].getContents());
      }/*For loop closed*/
     }/*If closed*/         
     bw.newLine();
    }/*Row for loop closed*/
    bw.flush();
    bw.close();
    System.out.print("Output File " + (sheet+1) +  " is created: " + outputpath +"\output_"+ s.getName() +".txt" + "n" );
   }/*Worksheet for loop closed*/      
   System.out.print("Please open the output file(s) using notepad.");   
   
  }/*Try closed*/
  catch (Exception e)
  {
   System.err.println(e);
  }/*Catch closed*/
 }/*Main Closed*/
}/*Class closed*/

Labels: Excel Example , Java File Example

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger