Convert XLS to CSV or TXT in Java


Untuk dapat menjalankan program ini, anda membutuhkan library jxl (jxl.jar). Untuk mendapatkannya silahkan download jxl.jar di sini.

Berikut ini, adalah sourcecode yang dapat digunakan:

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

class  ConvertXls{
  public static void main(String[] args){
    try{
      //File to store data in form of CSV
      File f = new File("input.txt");

      OutputStream os = (OutputStream)new FileOutputStream(f);
      String encoding = "UTF8";
      OutputStreamWriter osw = new OutputStreamWriter(os, encoding);
      BufferedWriter bw = new BufferedWriter(osw);

      //Excel document to be imported
      String filename = "test.xls";
      WorkbookSettings ws = new WorkbookSettings();
      ws.setLocale(new Locale("en", "EN"));
      Workbook w = Workbook.getWorkbook(new File(filename),ws);

      // Gets the sheets from workbook
      for (int sheet = 0; sheet < w.getNumberOfSheets(); sheet++){
        Sheet s = w.getSheet(sheet);

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

        Cell[] row = null;

        // Gets the cells from sheet
        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 (UnsupportedEncodingException e){
      System.err.println(e.toString());
    }
    catch (IOException e){
      System.err.println(e.toString());
    }
    catch (Exception e){
      System.err.println(e.toString());
    }
  }
}

Note:

  1. Simpan program di atas dengan nama ConvertXls.java
  2. Pastikan Program di atas berada dalam satu direktori dengan file test.xls
  3. Pastikan jxl.jar berada dalam satu direktori dengan program
  4. Anda dapat menyesuaikan nama file xls, sesuai dengan nama file yang dimiliki
  5. Dalam program di atas, output hasil konversi disimpan dalam file txt. Jika anda ingin konversi ke csv, tinggal rubah saja ekstensi output menjadi input.csv
  6. COMPILE: javac -classpath jxl.jar ConvertXls.java
  7. RUNNING: java -classpath jxl.jar;. ConvertXls  (windows)
  8. RUNNING: java -classpath jxl.jar:. ConvertXls  (linux)

About windupurnomo

I'm interested in programming. I am active with several programming such as Java, C #, C, JavaScript, HTML. I'm also develop desktop application (Java Swing), Mobile Application (Android), and Web programming (ASP MVC).
This entry was posted in Kuliah and tagged , , , , , , , . Bookmark the permalink.

6 Responses to Convert XLS to CSV or TXT in Java

  1. bisa convert massal, win?

  2. windupurnomo says:

    Pake loop bisa mas, panggil berulang sj class di atas.

  3. nini says:

    mas.. taw ga.. cara bikin remainder pakai java ?
    makasih infonya..

  4. GUSTI says:

    kang asyik eng menta papatahan ah……php genan tos hilap deui jarang dipake mah….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s