Created
December 26, 2017 05:59
-
-
Save hiroto3432/6a4e4c74388a36547b195a8e31e1f524 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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