#37 java: poi api ex

This commit is contained in:
haerong22
2023-05-24 12:08:24 +09:00
parent 3747851cff
commit 6b99c06e31
3 changed files with 67 additions and 0 deletions

View File

@@ -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

Binary file not shown.

View 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();
}
}
}