Skip to content

Instantly share code, notes, and snippets.

@hiroto3432
Created December 26, 2017 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiroto3432/6a4e4c74388a36547b195a8e31e1f524 to your computer and use it in GitHub Desktop.
Save hiroto3432/6a4e4c74388a36547b195a8e31e1f524 to your computer and use it in GitHub Desktop.
import org.apache.poi.ss.usermodel.*;
import java.io.File;
import java.io.FileInputStream;
public class ReadExcelTest {
static final String folder = "folderPath";
public static void main(String[] args) {
final String fileAddress = folder + "KosenData.xlsx";
Workbook wb = null;
File f = new File(fileAddress);
try {
FileInputStream input = new FileInputStream(f);
wb = WorkbookFactory.create(input);
input.close();
for(Sheet sheet : wb){
for(Row row : sheet){
for(Cell cell: row){
System.out.print(getCellValue(cell));
System.out.print(",");
}
System.out.println();
}
}
wb.close();
}
catch(Exception e){
e.printStackTrace();
}
}
private static Object getCellValue(Cell cell){
switch(cell.getCellTypeEnum()){
case STRING:
return cell.getRichStringCellValue().getString();
case FORMULA:
return cell.getRichStringCellValue().getString();
case NUMERIC:
if(DateUtil.isCellDateFormatted(cell)){
return cell.getDateCellValue();
}
else
return cell.getNumericCellValue();
case BOOLEAN:
return cell.getBooleanCellValue();
case BLANK:
return "-";
default:
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment