001/*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016package org.opengion.plugin.io;
017
018import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
019import org.apache.poi.ss.usermodel.Cell;
020import org.apache.poi.ss.usermodel.Row;
021import org.apache.poi.ss.usermodel.Sheet;
022import org.apache.poi.ss.usermodel.Workbook;
023import org.opengion.hayabusa.common.HybsSystem;
024import org.opengion.hayabusa.common.HybsSystemException;
025
026/**
027 * POI による、EXCELバイナリファイルに対する、ユーティリティクラスです。
028 *
029 * ここでは、アクティブセル領域になるように、不要なセルや行を削除します。
030 *
031 * 入力形式は、openXML形式にも対応しています。
032 * ファイルの内容に応じて、.xlsと.xlsxのどちらで読み取るかは、内部的に
033 * 自動判定されます。
034 *
035 * @og.rev 5.5.7.2 (2012/10/09) 新規作成
036 * @og.group その他
037 *
038 * @version  4.0
039 * @author   Kazuhiko Hasegawa
040 * @since    JDK5.0,
041 */
042public class ExcelUtil {
043        //* このプログラムのVERSION文字列を設定します。   {@value} */
044        private static final String VERSION = "5.5.7.2 (2012/10/09)" ;
045
046        /**
047         * DBTableModel から 各形式のデータを作成して,BufferedReader より読み取ります。
048         * コメント/空行を除き、最初の行は、必ず項目名が必要です。
049         * それ以降は、コメント/空行を除き、データとして読み込んでいきます。
050         * このメソッドは、EXCEL 読み込み時に使用します。
051         *
052         * @og.rev 5.5.7.2 (2012/10/09) 新規追加
053         *
054         * @param       wb      処理対象のワークブック
055         * @return      アクティブ領域のみに再設定された、ワークブック
056         */
057        public static Workbook activeWorkbook( final Workbook wb ) {
058                int sheetSu = wb.getNumberOfSheets();
059                for( int shno = 0; shno<sheetSu; shno++ ) {
060                        Sheet sheet = wb.getSheetAt(shno);
061
062                        int nFirstRow = sheet.getFirstRowNum();
063                        int nLastRow  = sheet.getLastRowNum();
064
065                        for( int nIndexRow = nFirstRow; nIndexRow <= nLastRow; nIndexRow++) {
066                                Row oRow = sheet.getRow(nIndexRow);
067                                if( oRow != null ) {
068                                        boolean isAllBrank = true;
069                                        int nFirstCell = oRow.getFirstCellNum();
070                                        int nLastCell  = oRow.getLastCellNum();
071                                        for( int nIndexCell = nFirstCell; nIndexCell <= nLastCell; nIndexCell++) {
072                                                Cell oCell = oRow.getCell(nIndexCell);
073                                                if( oCell != null && oCell.getCellType() != Cell.CELL_TYPE_BLANK ) { 
074                                                        isAllBrank = false;     
075                                                        break;
076                                                }
077                                        }
078                                        if( isAllBrank ) { sheet.removeRow( oRow ); }
079                                }
080                        }
081                }
082                return wb ;
083        }
084}