[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
package jp.co.sample.main;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import jp.co.sample.bean.SampleTempBean;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class ControllerExcel {
public static List<SampleTempBean> readExcel(String filepath)
throws FileNotFoundException, IOException {
File cdirectory = new File(filepath);
String[] xlsFilePaths = cdirectory.list();
List<SampleTempBean> SampleTempBeanList = new ArrayList<SampleTempBean>();
for (int i = 0; i < xlsFilePaths.length; i++) {
if (StringUtils.equals(xlsFilePaths[i], "CVS"))
continue;
POIFSFileSystem filein = new POIFSFileSystem(new FileInputStream(
filepath + xlsFilePaths[i]));
HSSFWorkbook wb = new HSSFWorkbook(filein);
// 総シート数取得
int sheets = wb.getNumberOfSheets();
for (int j = 0; j < sheets; j++) {// 各シートごとの処理
HSSFSheet sheet = wb.getSheetAt(j);
// 0行目から読み込み
for (int k =0; k<200; k++) {
HSSFRow row = sheet.getRow(k);
if (row == null){
continue;
}
HSSFCell messageB = row.getCell((short) 1);//フィールド名称
if (null != messageB){
SampleTempBean tempBean = new SampleTempBean();
// [start]======== 項目1 ==============
HSSFCell messageC = row.getCell((short) 2);//フィールド名称
tempBean.setTitle(messageC.getStringCellValue());// タイトル
// [ end ]======== 項目1 ==============
// [start]======== 項目2 ==============
HSSFRow row2 = sheet.getRow(++k);
HSSFCell messageC2 = row2.getCell((short) 2);//フィールド名称
tempBean.setTableName(messageC2.getStringCellValue());// テーブル
// [ end ]======== 項目2 ==============
// [start]======== 項目3 ==============
HSSFRow row3 = sheet.getRow(++k);
HSSFCell messageC3 = row3.getCell((short) 2);//フィールド名称
tempBean.setHistoryTableName(messageC3.getStringCellValue());
// [ end ]======== 項目3 ==============
// [start]======== 項目3 ==============
HSSFRow row4 = sheet.getRow(++k);
HSSFCell messageC4 = row4.getCell((short) 2);//フィールド名称
String temp = messageC4.getStringCellValue();
String[] strList = temp.split(",");
tempBean.setColumNameList(Arrays.asList(strList));
// [ end ]======== 項目3 ==============
SampleTempBeanList.add(tempBean);
}
}
}
}
return SampleTempBeanList;
}
}