The Apache POI is an open source library of the Apache Software Foundation to help Java programs read and write to Microsoft Office format files. POI provides the following types for parsing Microsoft Office format files:
HSSF-provides the ability to read and write Microsoft Excel xls format files.
XSSF-provides the ability to read and write Microsoft Excel OOXML xlsx format files.
HWPF-provides the ability to read and write Microsoft Word doc format files.
HSLF-provides the ability to read and write Microsoft PowerPoint format files.
HDGF-provides the ability to read Microsoft Visio format files.
HPBF-provides the ability to read Microsoft Publisher format files.
You can visit POI's homepage http://poi.apache.org/download your favorite version and learn more about it.
This only describes using POI to read Excel files, first locate the Excel file location when reading Excel, and then generate a worksheet Hssfworkbook object through the POI API:
File file = new File(filePath);
FileInputStream fint = new FileInputStream(file);
POIFSFileSystem poiFileSystem = new POIFSFileSystem(fint);
HSSFWorkbook workbook = new HSSFWorkbook(poiFileSystem);
The corresponding child worksheet Hssfsheet can be accessed through the getsheetat (int sheetnum) provided by Hssfworkbook, starting with ' 0 '. After obtaining the Hssfsheet object through Sheet.getrow (rownum) method to obtain the specified row of the Hssfrow,hssfrow provides a Getcell (short) method to access the Cell object. When working with cells, be aware that you cannot simply use the Getstringcellvalue of Hssshell () Method gets the value in the cell, and in the version I use (2.0) does not provide the function of automatic type conversion, so determine the value based on the type:
public static String getCell(HSSFCell cell) {
if (cell == null)
return "";
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:
return cell.getNumericCellValue() + "";
case HSSFCell.CELL_TYPE_STRING:
return cell.getStringCellValue();
case HSSFCell.CELL_TYPE_FORMULA:
return cell.getCellFormula();
case HSSFCell.CELL_TYPE_BLANK:
return "";
case HSSFCell.CELL_TYPE_BOOLEAN:
return cell.getBooleanCellValue() + "";
case HSSFCell.CELL_TYPE_ERROR:
return cell.getErrorCellValue() + "";
}
return "";
}