Archive for March, 2010

Writing xlsx with java…

Hello again;

Today i want to tell you, how do you create and write excel files in java. First of all we must download apache’s POI api in here. (The JAVA api for Microsoft Document.) In fact this api doesn’t for only excel. You can use this api for word, power point, and etc… It supports office from 97 to 2007. You can create,read or write xls, xlsx, doc, docx and etc.. I must say this api is more fastest than c# office interop api. You must add jars into your project.

Now we create an excel xlsx file and we write some text in this file.  Ok lets write some code.

package excel;

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class exelim {

 * @param args
 * @throws IOException
 public static void main(String[] args) throws IOException {
 // TODO Auto-generated method stub
 Workbook wb = new XSSFWorkbook();
 FileOutputStream fos = new FileOutputStream("sample.xlsx");
 Sheet sh = wb.createSheet("new sheet 1");
 for (int k = 0; k < 30; k++) {
 Row row = sh.createRow((short)k);
 for (int i = 0; i < 30; i++) {
 Cell cell = row.createCell((short)i);
 cell.setCellValue("hi world : )");


You must create workbook, sheet, row and cell for write something into the cell. I use XSSF for xlsx, if you want to create, read or write xls you can use HSSF. For about more information please visit POI project website. You can find more good artice at there.

See you at another article.


Read Full Post »


In this article i explain how to connect oracle with jdbc.  Before we start, we must download oracle oci driver for oracle version. You can find these oci drivers from here.

I use Oracle driver for Oracle 10.x database. After you download the Oracle oci driver we can start . We add new Java Project in eclipse. In libraries from “add external jars”, you must choice oci driver jar file.(I select ojdbc14.jar for oracle 10.x.) We add a main class for getting output from sql.

import java.sql.*;

public class main {

 * @param args
 * @throws SQLException
 public static void main(String[] args) throws SQLException {
 // TODO Auto-generated method stub
 try {
 Class.forName ("oracle.jdbc.driver.OracleDriver");
 } catch (ClassNotFoundException e) {

 Connection con = DriverManager.getConnection
 ("jdbc:oracle:thin:@ngeen-Laptop:1521:xe", "ngeen", "202020");
// @machine:port:SID,   user,  pass

 Statement st = con.createStatement();
 ResultSet rs = st.executeQuery("select * from all_tables");
 while (rs.next())
System.out.println (rs.getString(1));   // echo column 1

If you look at the console you can see 1st column of all_tables view in database.

Read Full Post »