Monday, December 07, 2009

Apache POI and Jython

import sys

def setClassPath():
libDir = "E:/apps/jython2.5.1/com/"
classPaths = ["poi-3.5-FINAL-20090928.jar","commons-logging-1.1.jar","log4j-1.2.13.jar","poi-scratchpad-3.5-FINAL-20090928.jar"]
for classPath in classPaths:
sys.path.append(libDir + classPath)


setClassPath()

from org.apache.poi.hssf.usermodel import *
from java.io import FileInputStream

file = "users.xls"

fis = FileInputStream(file)
wb = HSSFWorkbook(fis)
sheet = wb.getSheetAt(0)

# get No. of rows
rows = sheet.getPhysicalNumberOfRows()
# print wb, sheet, rows

cols = 0 # No. of columns
tmp = 0

# This trick ensures that we get the data properly even if it
# doesn.t start from first few rows
for i in range(0, 10,1):
row = sheet.getRow(i)
if(row != None):
tmp = sheet.getRow(i).getPhysicalNumberOfCells()
if tmp > cols:
cols = tmp
# print cols

for r in range(0, rows, 1):
row = sheet.getRow(r)
# print r
if(row != None):
for c in range(0, cols, 1):
cell = row.getCell(c)
if cell != None:
# print cell
pass


print sheet.getRow(5).getCell(3)

#wb.close()
fis.close()

No comments: