Sunday, December 12, 2008

初探jython使用poi库操作excel文件

一。环境
1. JDK  下载并安装jdk。本文使用1.6版本。http://java.sun.com/
    安装后
    进入命令行输入 java 测试一下java是不是加入到环境变量中

2. 下载poi:http://poi.apache.org   本文使用 poi-bin-3.5-beta4-20081128.zip
    解压后:找到 poi-3.5-beta4\poi-3.5-beta4-20081128.jar
   
3. 下载并安装jython2.5b0.   http://is.gd/bvnF
    安装好jython后。 设置jython的环境变量。 进入命令行后输入 jython测试是否加入到环境变量中。
    把 poi-3.5-beta4-20081128.jar  加到 jython 的lib中。 我使用了一个比较笨的方法。如下:
    找到jython的主目录的下面的 bin\jython.bat  或者 bin\jython 文件。 本文以 jython.bat 为例
    修改 jython.bat
    再搜索 :argsDone(大约140行)
    在搜到的下一行加入以下语句:
    set CLASSPATH=.;e:\jay\java\lib\poi-3.5-beta4\poi-3.5-beta4-20081128.jar;%CLASSPATH%;

    然后进入到 jython 中输入
    import  org.apache.poi.hssf.usermodel
    测试poi是否被加入jython的classpath中。

二. java代码:

package com.cngump.poi;

/**
 * @author cngump
 */


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

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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;


public class FirstPoi {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Workbook wb = new HSSFWorkbook();
        //Workbook wb = new XSSFWorkbook();
        Sheet sheet1 = wb.createSheet("new sheet");
      
        Row headerRow = sheet1.createRow(0);
    headerRow.setHeightInPoints(12.75f);
    for (int i = 0; i < 10; i++) {
    Cell cell = headerRow.createCell(i);
    cell.setCellValue(i);
   
    }
        Sheet sheet2 = wb.createSheet("second sheet");
        FileOutputStream fileOut;
        try {
            fileOut = new FileOutputStream("FirstPoi.xls");
              wb.write(fileOut);
                fileOut.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
     

    }
}


3.jython代码(保存为utf-8编码)

#! /usr/bin/jython
#coding=utf-8
__author__="cngump"
__date__ ="$2008-12-14 0:36:49$"

from java.io import *
from org.apache.poi.ss.usermodel import *
from org.apache.poi.hssf.usermodel import HSSFWorkbook

def createExcel():
    wb=HSSFWorkbook()
    sheet1=wb.createSheet("new sheet")
    headerRow=sheet1.createRow(0);
    headerRow.setHeightInPoints(12.75)
    for i in range(1,10):
    cell=headerRow.createCell(i)
    cell.setCellValue("中文 %s"%i)
    sheet2=wb.createSheet("second sheet")
    fos=FileOutputStream("FirstPoi.xls")
    wb.write(fos)
    fos.close()



if __name__ == "__main__":
    print "before create xls ,you must close it. "
    createExcel()
    print "create xls successfull."


四. 运行结果:
jython excel poi

Tags: poi, jython, excel


Sunday, December 12, 2008

启用 Google Appengine Blog

自从用了twitter后。好久没有写blog了。(本来就少写)。 2008年11月份买的国外的php的主机也到期。就没有续费了。 所以重新开了这个blog。 希望自己能够坚持下来。写写自己的一些心情和心得。现在还有一点担心的事gae会不会被墙掉? 我是很老实的。 不管这么多了,墙就墙吧。 写好自己东西先。本blog是使用gae中不错的cpedialog源代码,在此谢过cpedia先。


老甘 于 2008年12月14日凌晨


     

Tags: cngump