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     */
016    package org.opengion.plugin.io;
017    
018    import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
019    import org.apache.poi.ss.usermodel.Cell;
020    import org.apache.poi.ss.usermodel.Row;
021    import org.apache.poi.ss.usermodel.Sheet;
022    import org.apache.poi.ss.usermodel.Workbook;
023    import org.opengion.hayabusa.common.HybsSystem;
024    import 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     */
042    public 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    }