Sunday, September 19, 2010

Reading excel file in java with JXL api

We can use JXL api to read/write/edit an excel file. Here in this article we are going to see how to read an excel file using this api.

First of all you have to download the api and add the path of jxl.jar into the system classpath .

Then the simple following code will read all the cell values from the excel file and will print into the console.

import java.io.*;
import java.util.Scanner;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class ReadExcel {
    // method that read the excel file and print all it's cell values
    public void readFile(File file) {
        try {
            Workbook wb = Workbook.getWorkbook(file);    // get the excel work book
            // from book you can select sheet and work on it
            // here we are taking the first sheet for our example 
            Sheet sheet = wb.getSheet(0);                
            
            // travel all cells in the selected sheet
            for(int i=0; i < sheet.getRows(); i++) {
                for(int j=0; j < sheet.getColumns(); j++) {
                    Cell cell = sheet.getCell(j, i);                        // get a particular cell
                    System.out.print(cell.getContents() + "\t");    // print cell content
                }
                System.out.println();
            }
        } catch (BiffException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String args[]) {
        // read the file path that you want to read from console
        Scanner in = new Scanner(System.in);
        String file = in.nextLine();
        
        // initiate the ReadExcel class and call the method to read the file
        ReadExcel re = new ReadExcel();
        re.readFile(new File(file));
    }

}


If you need more information regarding this you may visit this link - http://www.vogella.de/articles/JavaExcel/article.html

Monday, August 23, 2010

Setup google analytics to observe your blogger(blogspot) blog

You may want to view the statistics of your blog and there are many providers from which you can see it. But if you are using blogger(blogspot) then you can follow this link to setup google analytics for your blog and see its statistics. I am using it for my blog too and its pretty cool and interesting.

Finding time difference in Ruby

Finding time difference between two dates or times is very simple in ruby. It is just like doing arithmetic subtraction. We can do it for different formats in the following way:

Date
d1 = Date.parse("2010-08-23")
d2 = Date.parse("2010-08-24")
day_diff = (d2 - d1).to_i     # returns number of days

DateTime
dt1 = DateTime.strptime("2009/04/16 19:52:30", "%Y/%m/%d %H:%M:%S").to_time
dt2 = DateTime.strptime("2009/04/17 19:52:30", "%Y/%m/%d %H:%M:%S").to_time
time_diff = dt2-dt1    # returns time difference in seconds

Time
t1 = Time.parse("2010-08-24 01:20:00")
 t2 = Time.parse("2010-08-24 05:20:00")
time_diff =  t2-t1        # returns time difference in seconds

Friday, August 20, 2010

Using MS Access in Rails application

There is no default support to use MS Access database with our Rails applications. Among many solutions while googling, I have found 2 solutions which can be easily used and managed in our projects. These solutions can be found in the following links –
  1. http://blog.behindlogic.com/2007/07/msaccess-for-rails-heres-your-rough.htm
  2. http://rubyonwindows.blogspot.com/2007/06/using-ruby-ado-to-work-with-ms-access.html

Solution one is more preferable than the second one. Using this solution we can do same operations that we do using ActiveRecord.
If you could not use the solution one for your expected result then you can use second solution. But this solution is not that much intuitive. Using this solution, if you want to any operation then you will need to write SQL yourself.