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 }