#37 java: poi api ex
This commit is contained in:
@@ -12,6 +12,10 @@ repositories {
|
||||
dependencies {
|
||||
implementation 'org.jsoup:jsoup:1.16.1'
|
||||
implementation 'com.google.code.gson:gson:2.10.1'
|
||||
implementation 'org.apache.poi:poi:5.2.3'
|
||||
implementation 'org.apache.poi:poi-ooxml:5.2.3'
|
||||
|
||||
implementation 'org.apache.logging.log4j:log4j-core:2.20.0'
|
||||
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
|
||||
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
|
||||
|
||||
BIN
java/api/example.xlsx
Normal file
BIN
java/api/example.xlsx
Normal file
Binary file not shown.
63
java/api/src/main/java/poi/ExcelEx.java
Normal file
63
java/api/src/main/java/poi/ExcelEx.java
Normal file
@@ -0,0 +1,63 @@
|
||||
package poi;
|
||||
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class ExcelEx {
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
FileInputStream file = new FileInputStream("example.xlsx");
|
||||
Workbook workbook = WorkbookFactory.create(file);
|
||||
Sheet sheet = workbook.getSheetAt(0);
|
||||
|
||||
for (Row row : sheet) {
|
||||
for (Cell cell : row) {
|
||||
|
||||
switch (cell.getCellType()) {
|
||||
case NUMERIC:
|
||||
if (DateUtil.isCellDateFormatted(cell)) {
|
||||
Date date = cell.getDateCellValue();
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
||||
String dateValue = format.format(date);
|
||||
System.out.print(dateValue + "\t");
|
||||
} else {
|
||||
double numericValue = cell.getNumericCellValue();
|
||||
if (numericValue == Math.floor(numericValue)) {
|
||||
int intValue = (int) numericValue;
|
||||
System.out.print(intValue + "\t");
|
||||
} else {
|
||||
System.out.print(numericValue + "\t");
|
||||
}
|
||||
}
|
||||
break;
|
||||
case STRING:
|
||||
String stringValue = cell.getStringCellValue();
|
||||
System.out.print(stringValue + "\t");
|
||||
break;
|
||||
case BOOLEAN:
|
||||
boolean booleanValue = cell.getBooleanCellValue();
|
||||
System.out.print(booleanValue + "\t");
|
||||
break;
|
||||
case FORMULA:
|
||||
String formulaValue = cell.getCellFormula();
|
||||
System.out.print(formulaValue + "\t");
|
||||
case BLANK:
|
||||
default:
|
||||
System.out.print("\t");
|
||||
break;
|
||||
}
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
file.close();
|
||||
System.out.println("엑셀 데이터 읽어오기 완료");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user