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.ss.usermodel.Cell; 019import org.apache.poi.ss.usermodel.Row; 020import org.apache.poi.ss.usermodel.Sheet; 021import org.apache.poi.ss.usermodel.Workbook; 022 023/** 024 * POI による、EXCELバイナリファイルに対する、ユーティリティクラスです。 025 * 026 * ここでは、アクティブセル領域になるように、不要なセルや行を削除します。 027 * 028 * 入力形式は、openXML形式にも対応しています。 029 * ファイルの内容に応じて、.xlsと.xlsxのどちらで読み取るかは、内部的に 030 * 自動判定されます。 031 * 032 * @og.rev 5.5.7.2 (2012/10/09) 新規作成 033 * @og.group その他 034 * 035 * @version 4.0 036 * @author Kazuhiko Hasegawa 037 * @since JDK5.0, 038 */ 039public final class ExcelUtil { 040 //* このプログラムのVERSION文字列を設定します。 {@value} */ 041 private static final String VERSION = "5.5.7.2 (2012/10/09)" ; 042 043 /** 044 * オブジェクトを作らせない為の、private コンストラクタ 045 */ 046 private ExcelUtil() {} 047 048 /** 049 * DBTableModel から 各形式のデータを作成して,BufferedReader より読み取ります。 050 * コメント/空行を除き、最初の行は、必ず項目名が必要です。 051 * それ以降は、コメント/空行を除き、データとして読み込んでいきます。 052 * このメソッドは、EXCEL 読み込み時に使用します。 053 * 054 * @og.rev 5.5.7.2 (2012/10/09) 新規追加 055 * 056 * @param wb 処理対象のワークブック 057 * @return アクティブ領域のみに再設定された、ワークブック 058 */ 059 public static Workbook activeWorkbook( final Workbook wb ) { 060 int sheetSu = wb.getNumberOfSheets(); 061 for( int shno = 0; shno<sheetSu; shno++ ) { 062 Sheet sheet = wb.getSheetAt(shno); 063 064 int nFirstRow = sheet.getFirstRowNum(); 065 int nLastRow = sheet.getLastRowNum(); 066 067 for( int nIndexRow = nFirstRow; nIndexRow <= nLastRow; nIndexRow++) { 068 Row oRow = sheet.getRow(nIndexRow); 069 if( oRow != null ) { 070 boolean isAllBrank = true; 071 int nFirstCell = oRow.getFirstCellNum(); 072 int nLastCell = oRow.getLastCellNum(); 073 for( int nIndexCell = nFirstCell; nIndexCell <= nLastCell; nIndexCell++) { 074 Cell oCell = oRow.getCell(nIndexCell); 075 if( oCell != null && oCell.getCellType() != Cell.CELL_TYPE_BLANK ) { 076 isAllBrank = false; 077 break; 078 } 079 } 080 if( isAllBrank ) { sheet.removeRow( oRow ); } 081 } 082 } 083 } 084 return wb ; 085 } 086}