Jakarta POI
戻る
Jakarta POIを使ってみます。

import org.apache.poi.hssf.eventmodel.*;
import org.apache.poi.hssf.eventusermodel.*;
import org.apache.poi.hssf.record.formula.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.*;
import org.apache.poi.hssf.*;
import org.apache.poi.poifs.filesystem.*;
import java.io.*;

// $Id: poi.html,v 1.1 2009/06/22 16:12:20 kishi Exp kishi $
// Description: セルの内容を読み込む

public class HSSF
{

    public static void main(String args[])
    {
        try
        {
            POIFSFileSystem fs      =
                new POIFSFileSystem(new FileInputStream("MySample.xls"));
            HSSFWorkbook wb = new HSSFWorkbook(fs);
            HSSFSheet sheet = wb.getSheetAt(0);
            HSSFRow row = sheet.getRow(0);
            HSSFCell cell = row.getCell((short)4);

            //System.out.println( cell.getBooleanCellValue() );

            System.out.println ( getCellValue(cell) );
        }
        catch(Exception ex)
        {
            System.out.println("Exception raised in InteractExcel Main Function "+ ex);
        }

    }

    public static Object getCellValue( HSSFCell cell){
        /**
        	@description タイプに応じて値取得のメソッドを変える
        */

        int type = cell.getCellType();


        if (type == HSSFCell.CELL_TYPE_BLANK){
            return null;
        }else if (type == HSSFCell.CELL_TYPE_BOOLEAN){
            return String.valueOf( cell.getBooleanCellValue() );
        }else if (type == HSSFCell.CELL_TYPE_ERROR){
            return "CELL_TYPE_ERROR";
        }else if (type == HSSFCell.CELL_TYPE_FORMULA){
            return cell.getCellFormula();
        }else if (type == HSSFCell.CELL_TYPE_NUMERIC){
            return String.valueOf( cell.getNumericCellValue() ) ; // double型からString型へ変換
        }else if (type == HSSFCell.CELL_TYPE_STRING){
            return cell.getCellFormula();
        }else{
            return "Not defined";
        }


    }

}

/*

int を文字列(String)に変換するには?


[S007 A-01]
Integer#toString(int)もしくは String#valueOf(int)を使います。

他のプリミティブ型についても同様のことが言えます。それをまとめたものが、以下の表です。

プリミティブ型  Stringを使う場合        それ以外の方法
===============================================================
boolean         valueOf(boolean)        new Boolean(boolean).toString()
char            valueOf(char)           new Character(char).toString()
byte            valueOf(int)            Byte.toString(byte)
short           valueOf(int)            Short.toString(short)
int             valueOf(int)            Integer.toString(int)
long            valueOf(long)           Long.toString(long)
float           valueOf(float)          Float.toString(float)
double          valueOf(double)         Double.toString(double)
===============================================================

また、次のような書き方もできます。

int x;
String s;
s = "" + x;

参考記事 [JavaHouse-Brewers:4046]

*/
import java.io.*;
import org.apache.poi.hssf.usermodel.*;

// $Id: poi.html,v 1.1 2009/06/22 16:12:20 kishi Exp kishi $
// Descriotin: 空のエクセルファイルを作成する

public class Test1{

    public static void main(String[] args){
        HSSFWorkbook workbook = new HSSFWorkbook();

        FileOutputStream out = null;
        try{
            out = new FileOutputStream("MySample.xls");
            workbook.write(out);
        } catch(IOException e) {
            System.out.println(e.toString());
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                System.out.println(e.toString());
            }
        }
    }
}


import java.io.*;
import org.apache.poi.hssf.usermodel.*;

// $Id: poi.html,v 1.1 2009/06/22 16:12:20 kishi Exp kishi $
// Description: 複数のシートを作成する

public class Test2{

    public static void main(String[] args){

        HSSFWorkbook workbook = new HSSFWorkbook();

        HSSFSheet sheet1 = workbook.createSheet("sheet1");
        HSSFSheet sheet2 = workbook.createSheet("sheet2");
        HSSFSheet sheet3 = workbook.createSheet("sheet3");

        sheet2.setSelected(true);

        FileOutputStream out = null;

        try{
            out = new FileOutputStream("MySample.xls");
            workbook.write(out);
        } catch(IOException e) {
            System.out.println(e.toString());
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                System.out.println(e.toString());
            }
        }
    }
}


import java.io.*;
import org.apache.poi.hssf.usermodel.*;
import java.util.Calendar;
import java.util.Date;

// $Id: poi.html,v 1.1 2009/06/22 16:12:20 kishi Exp kishi $

public class Test3{

    /**
    内容を更新する
    */
    public static void main(String[] args){

        HSSFWorkbook workbook = new HSSFWorkbook();

        HSSFSheet sheet = workbook.createSheet("sheet1");
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell1 = row.createCell((short)1);
        HSSFCell cell2 = row.createCell((short)2);
        HSSFCell cell3 = row.createCell((short)3);
        HSSFCell cell4 = row.createCell((short)4);
        HSSFCell cell5 = row.createCell((short)5);
        HSSFCell cell6 = row.createCell((short)6);

        boolean val1 = true;
        Calendar val2 = Calendar.getInstance();
        Date val3 = new Date();
        double val4 = 1234.56;
        String val5 = "Hello";
        String val6 = "日本語";

        cell1.setCellValue(val1);
        cell2.setCellValue(val2);
        cell3.setCellValue(val3);
        cell4.setCellValue(val4);
        cell5.setCellValue(val5);

        /*
          日本語を書き込みする場合は、以下のように
         エンコーディングを指定する
        */

        cell6.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell6.setCellValue(val6);

        FileOutputStream out = null;

        try{
            out = new FileOutputStream("MySample.xls");
            workbook.write(out);
        } catch(IOException e) {
            System.out.println(e.toString());
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                System.out.println(e.toString());
            }
        }
    }
}

戻る

inserted by FC2 system