POIを使用してexcelファイルの内容を読み込む(2)
7304 ワード
**
* Excel
*/
public class ExcelReader {
private POIFSFileSystem fs;
private HSSFWorkbook wb;
private HSSFSheet sheet;
private HSSFRow row;
/**
* Excel
* @param InputStream
* @return String
*/
public String[] readExcelTitle(InputStream is) {
try {
fs = new POIFSFileSystem(is);
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
}
sheet = wb.getSheetAt(0);
row = sheet.getRow(0);
//
int colNum = row.getPhysicalNumberOfCells();
System.out.println("colNum:" + colNum);
String[] title = new String[colNum];
for (int i = 0; i < colNum; i++) {
//title[i] = getStringCellValue(row.getCell((short) i));
title[i] = getCellFormatValue(row.getCell((short) i));
}
return title;
}
/**
* Excel
* @param InputStream
* @return Map Map
*/
public Map<Integer, String> readExcelContent(InputStream is) {
Map<Integer, String> content = new HashMap<Integer, String>();
String str = "";
try {
fs = new POIFSFileSystem(is);
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
}
sheet = wb.getSheetAt(0);
//
int rowNum = sheet.getLastRowNum();
row = sheet.getRow(0);
int colNum = row.getPhysicalNumberOfCells();
// ,
for (int i = 1; i <= rowNum; i++) {
row = sheet.getRow(i);
int j = 0;
while (j < colNum) {
// "-" , String replace()
// javabean , javabean
// str += getStringCellValue(row.getCell((short) j)).trim() +
// "-";
str += getCellFormatValue(row.getCell((short) j)).trim() + " ";
j++;
}
content.put(i, str);
str = "";
}
return content;
}
/**
*
*
* @param cell Excel
* @return String
*/
private String getStringCellValue(HSSFCell cell) {
String strCell = "";
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
strCell = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
strCell = String.valueOf(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
strCell = String.valueOf(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK:
strCell = "";
break;
default:
strCell = "";
break;
}
if (strCell.equals("") || strCell == null) {
return "";
}
if (cell == null) {
return "";
}
return strCell;
}
/**
*
*
* @param cell
* Excel
* @return String
*/
private String getDateCellValue(HSSFCell cell) {
String result = "";
try {
int cellType = cell.getCellType();
if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
Date date = cell.getDateCellValue();
result = (date.getYear() + 1900) + "-" + (date.getMonth() + 1)
+ "-" + date.getDate();
} else if (cellType == HSSFCell.CELL_TYPE_STRING) {
String date = getStringCellValue(cell);
result = date.replaceAll("[ ]", "-").replace(" ", "").trim();
} else if (cellType == HSSFCell.CELL_TYPE_BLANK) {
result = "";
}
} catch (Exception e) {
System.out.println(" !");
e.printStackTrace();
}
return result;
}
/**
* HSSFCell
* @param cell
* @return
*/
private String getCellFormatValue(HSSFCell cell) {
String cellvalue = "";
if (cell != null) {
// Cell Type
switch (cell.getCellType()) {
// Cell Type NUMERIC
case HSSFCell.CELL_TYPE_NUMERIC:
case HSSFCell.CELL_TYPE_FORMULA: {
// cell Date
if (HSSFDateUtil.isCellDateFormatted(cell)) {
// Date , Data
// 1: data :2011-10-12 0:00:00
//cellvalue = cell.getDateCellValue().toLocaleString();
// 2: data :2011-10-12
Date date = cell.getDateCellValue();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
cellvalue = sdf.format(date);
}
//
else {
// Cell
cellvalue = String.valueOf(cell.getNumericCellValue());
}
break;
}
// Cell Type STRIN
case HSSFCell.CELL_TYPE_STRING:
// Cell
cellvalue = cell.getRichStringCellValue().getString();
break;
// Cell
default:
cellvalue = " ";
}
} else {
cellvalue = "";
}
return cellvalue;
}
public static void main(String[] args) {
try {
// Excel
InputStream is = new FileInputStream("d:\\test2.xls");
ExcelReader excelReader = new ExcelReader();
String[] title = excelReader.readExcelTitle(is);
System.out.println(" Excel :");
for (String s : title) {
System.out.print(s + " ");
}
// Excel
InputStream is2 = new FileInputStream("d:\\test2.xls");
Map<Integer, String> map = excelReader.readExcelContent(is2);
System.out.println(" Excel :");
for (int i = 1; i <= map.size(); i++) {
System.out.println(map.get(i));
}
} catch (FileNotFoundException e) {
System.out.println(" !");
e.printStackTrace();
}
}
}
3.まとめ
EXcelセルの内容には、日付型、数値型、文字列型など、一定のフォーマットがあることが多いため、読み込むときにフォーマット判断を行う必要があります.そうしないと、エラーが発生します.よくあるのは、日付が正常に読み込めないことです.コードインスタンスには、次の方法があります.
getCellFormatValue(HSSFCell cell)
このメソッドにexcelセルを入力すると、セルのフォーマットが認識され、正しいフォーマットに変換されます.
コードインスタンスには、次のコードがあります.
int colNum = row.getPhysicalNumberOfCells();
HSSFRow.getPhysicalNumberOfCells()この方法は1行に存在するセル数を取得するために用いられ,POIの公式APIではgetPhysicalNumberOfCellsメソッドの解釈が与えられている