「java開発」Javaでexcelファイルを読むサンプルコード

Javaコード:

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Startnews24_ExcelUtil {
public static void main(String[] args) throws Exception {
System.out.println(" 開始");
String path="E:/poqq/startnews24_test.xls";
String convertFileType = convertFileType(path);
System.out.println(convertFileType);
readExcel(path,convertFileType);
System.out.println(“終了");
}
/**
* Excelを読む
* @param filePath
* ファイルパス
* @param fileType
* ファイルタイプ
* @throws Exception
*/
public static void readExcel(String filePath,String fileType) throws Exception {
Workbook wb = null;
InputStream fileInputStream = new FileInputStream(new File(filePath));
if (fileType.equals(“xls")) {
wb = new HSSFWorkbook(fileInputStream);
}
else if(fileType.equals(“xlsx"))
{
wb=new XSSFWorkbook(fileInputStream);
}
else
{
throw new IllegalArgumentException(fileType+"が判断できない");
}
Sheet sheetAt = wb.getSheetAt(0);
int firstRowNum = sheetAt.getFirstRowNum();
int lastRowNum = sheetAt.getLastRowNum();
System.out.println(“総計" + (lastRowNum – firstRowNum) + “行目のレコード");
for (int i = firstRowNum; i < lastRowNum – firstRowNum; i++) {
Row row = sheetAt.getRow(i);
short firstCellNum = row.getFirstCellNum();
short lastCellNum = row.getLastCellNum();

for (int j = firstCellNum; j < lastCellNum – firstCellNum; j++) {
Cell cell = row.getCell(j);
if (cell != null) {
int cellType = cell.getCellType();

String cellValue = null;
switch (cellType) {
case Cell.CELL_TYPE_STRING:
cellValue = cell.getStringCellValue();
System.out.println(“第" + i + “行目,第" + j + “列レコード!タイプ:STRING ,値:"+cellValue);
break;
case Cell.CELL_TYPE_NUMERIC:
cellValue = cell.getNumericCellValue() + “";
System.out.println(“第" + i + “行目,第" + j + “列のレコード!タイプ:NUMERIC ,値:"+cellValue);
break;
case Cell.CELL_TYPE_FORMULA:
cellValue = cell.getCellFormula();
System.out.println(“第" + i + “行目,第" + j + “列のレコード!タイプ:FORMULA ,値:"+cellValue);
break;
case Cell.CELL_TYPE_ERROR:
cellValue = cell.getErrorCellValue() + “";
System.out.println(“第" + i + “行目,第" + j + “列のレコード!タイプ:ERROR ,値:"+cellValue);
break;
case Cell.CELL_TYPE_BOOLEAN:
cellValue = cell.getBooleanCellValue() + “";
System.out.println(“第" + i + “行目,第" + j + “列のレコード!タイプ:BOOLEAN ,値:"+cellValue);
break;
case Cell.CELL_TYPE_BLANK:
cellValue = cell.getRichStringCellValue() + “";
System.out.println(“第" + i + “行目,第" + j + “列のレコード!タイプ:BLANK ,値:"+cellValue);
break;
default:
break;
}
if(i==firstRowNum){
String convertToPinyinString = convertToPinyinString(cellValue);
System.out.println(“convertToPinyinString="+convertToPinyinString);
}
}
}
}
}
/**
* ファイルの拡張名を取得
* @param filepath
* @return
*/
public static String convertFileType(String filepath){
int lastIndexOf = filepath.lastIndexOf(“.");
return filepath.substring(lastIndexOf+1);

}

}

Java

Posted by arkgame