Home Answers Viewqa Development-process sir, how to convert excel file to csv file using java? please send me sample code.

 
 


hariprasad
sir, how to convert excel file to csv file using java? please send me sample code.
2 Answer(s)      2 years and 2 months ago
Posted in : Development process

please send me sample code for converting excel file into csv file uisng java.

View Answers

March 8, 2011 at 5:38 PM


import java.io.*;
import jxl.*;
import java.util.*;

class  ConvertExcelToCSV
{
  public static void main(String[] args) 
  {
    try
    {
      String filename = "C:/data.xls";
      WorkbookSettings ws = new WorkbookSettings();
      ws.setLocale(new Locale("en", "EN"));
      Workbook w = Workbook.getWorkbook(new File(filename),ws);

      File f = new File("c:/new.csv");
      OutputStream os = (OutputStream)new FileOutputStream(f);
      String encoding = "UTF8";
      OutputStreamWriter osw = new OutputStreamWriter(os, encoding);
      BufferedWriter bw = new BufferedWriter(osw);


      for (int sheet = 0; sheet < w.getNumberOfSheets(); sheet++)
      {
        Sheet s = w.getSheet(sheet);

        bw.write(s.getName());
        bw.newLine();

        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(',');
              bw.write(row[j].getContents());
            }
          }
          bw.newLine();
        }
      }
      bw.flush();
      bw.close();
    }
    catch (Exception e)
    {
      System.err.println(e);
    }

  }
}

March 8, 2011 at 5:41 PM


For the above code, you need JExcel Api,

Download it









Related Pages:

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.