Skip to content

Commit b35c4e8

Browse files
authored
Merge branch 'master' into fix-5227
2 parents 1136538 + ba29be1 commit b35c4e8

File tree

282 files changed

+3796
-492
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

282 files changed

+3796
-492
lines changed

algorithms-miscellaneous-3/pom.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<dependency>
3838
<groupId>org.apache.commons</groupId>
3939
<artifactId>commons-lang3</artifactId>
40-
<version>${commons.lang3.version}</version>
40+
<version>${commons-lang3.version}</version>
4141
</dependency>
4242
<dependency>
4343
<groupId>pl.pragmatists</groupId>
@@ -63,10 +63,8 @@
6363
</dependencies>
6464

6565
<properties>
66-
<commons-collections4.version>4.3</commons-collections4.version>
6766
<guava.version>28.0-jre</guava.version>
6867
<retrofit.version>2.6.0</retrofit.version>
69-
<commons.lang3.version>3.8.1</commons.lang3.version>
7068
<JUnitParams.version>1.1.0</JUnitParams.version>
7169
</properties>
7270

apache-poi-2/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.docx
2+
temp.xls
3+
temp.xlsx

apache-poi-2/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Apache POI
2+
3+
This module contains articles about Apache POI
4+
5+
### Relevant Articles:
6+
7+
- [Adding a Column to an Excel Sheet Using Apache POI](https://www.baeldung.com/java-excel-add-column)
8+
- More articles: [[<-- prev]](/apache-poi)

apache-poi-2/pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>apache-poi</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<name>apache-poi</name>
9+
10+
<parent>
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>parent-modules</artifactId>
13+
<version>1.0.0-SNAPSHOT</version>
14+
</parent>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.apache.poi</groupId>
19+
<artifactId>poi-ooxml</artifactId>
20+
<version>5.0.0</version>
21+
</dependency>
22+
</dependencies>
23+
24+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.poi.excel.newcolumn;
2+
import org.apache.poi.ss.usermodel.CellType;
3+
import org.apache.poi.ss.usermodel.Row;
4+
import org.apache.poi.ss.usermodel.Sheet;
5+
import org.apache.poi.ss.usermodel.Workbook;
6+
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
7+
8+
9+
public class ExcelColumn {
10+
11+
public void addColumn(Sheet sheet, CellType cellType) {
12+
for (Row currentRow : sheet) {
13+
currentRow.createCell(currentRow.getLastCellNum(), cellType);
14+
}
15+
}
16+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.poi.excel.newcolumn;
2+
3+
import org.apache.poi.ss.usermodel.CellType;
4+
import org.apache.poi.ss.usermodel.Row;
5+
import org.apache.poi.ss.usermodel.Sheet;
6+
import org.apache.poi.ss.usermodel.Workbook;
7+
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
import java.io.IOException;
12+
import java.net.URISyntaxException;
13+
import java.nio.file.Paths;
14+
15+
import static org.junit.Assert.assertEquals;
16+
17+
public class ExcelColumnUnitTest {
18+
private static final String FILE_NAME = "newColumnTest.xlsx";
19+
private String fileLocation;
20+
21+
@Before
22+
public void setup() throws URISyntaxException {
23+
fileLocation = Paths.get(ClassLoader.getSystemResource(FILE_NAME).toURI()).toString();
24+
}
25+
26+
@Test
27+
public void givenExistingRows_whenAddNewColumn_thenRowColumnNumberIncreased() throws IOException {
28+
Workbook workbook = new XSSFWorkbook(fileLocation);
29+
Sheet sheet = workbook.getSheetAt(0);
30+
Row row = sheet.getRow(0);
31+
assertEquals(5, row.getLastCellNum());
32+
33+
ExcelColumn excelColumn = new ExcelColumn();
34+
excelColumn.addColumn(sheet, CellType.STRING);
35+
assertEquals(6, row.getLastCellNum());
36+
37+
workbook.close();
38+
}
39+
40+
}
3.3 KB
Binary file not shown.

apache-poi/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ This module contains articles about Apache POI
1515
- [Multiline Text in Excel Cell Using Apache POI](https://www.baeldung.com/apache-poi-write-multiline-text)
1616
- [Set Background Color of a Cell with Apache POI](https://www.baeldung.com/apache-poi-background-color)
1717
- [Add Borders to Excel Cells With Apache POI](https://www.baeldung.com/apache-poi-add-borders)
18+
- More articles: [[next -->]](/apache-poi-2)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.baeldung.poi.excel;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
7+
import org.apache.poi.ss.usermodel.Cell;
8+
import org.apache.poi.ss.usermodel.CellType;
9+
import org.apache.poi.ss.usermodel.DateUtil;
10+
import org.apache.poi.ss.usermodel.Row;
11+
import org.apache.poi.ss.usermodel.Sheet;
12+
import org.apache.poi.ss.usermodel.Workbook;
13+
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
14+
15+
public class ExcelUtility {
16+
private static final String ENDLINE = System.getProperty("line.separator");
17+
18+
public static String readExcel(String filePath) throws IOException {
19+
File file = new File(filePath);
20+
FileInputStream inputStream = null;
21+
StringBuilder toReturn = new StringBuilder();
22+
try {
23+
inputStream = new FileInputStream(file);
24+
Workbook baeuldungWorkBook = new XSSFWorkbook(inputStream);
25+
for (Sheet sheet : baeuldungWorkBook) {
26+
toReturn.append("--------------------------------------------------------------------").append(ENDLINE);
27+
toReturn.append("Worksheet :").append(sheet.getSheetName()).append(ENDLINE);
28+
toReturn.append("--------------------------------------------------------------------").append(ENDLINE);
29+
int firstRow = sheet.getFirstRowNum();
30+
int lastRow = sheet.getLastRowNum();
31+
for (int index = firstRow + 1; index <= lastRow; index++) {
32+
Row row = sheet.getRow(index);
33+
toReturn.append("|| ");
34+
for (int cellIndex = row.getFirstCellNum(); cellIndex < row.getLastCellNum(); cellIndex++) {
35+
Cell cell = row.getCell(cellIndex, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
36+
printCellValue(cell, toReturn);
37+
}
38+
toReturn.append(" ||").append(ENDLINE);
39+
}
40+
}
41+
inputStream.close();
42+
43+
} catch (IOException e) {
44+
throw e;
45+
}
46+
return toReturn.toString();
47+
}
48+
49+
public static void printCellValue(Cell cell, StringBuilder toReturn) {
50+
CellType cellType = cell.getCellType().equals(CellType.FORMULA) ? cell.getCachedFormulaResultType()
51+
: cell.getCellType();
52+
if (cellType.equals(CellType.STRING)) {
53+
toReturn.append(cell.getStringCellValue()).append(" | ");
54+
}
55+
if (cellType.equals(CellType.NUMERIC)) {
56+
if (DateUtil.isCellDateFormatted(cell)) {
57+
toReturn.append(cell.getDateCellValue()).append(" | ");
58+
} else {
59+
toReturn.append(cell.getNumericCellValue()).append(" | ");
60+
}
61+
}
62+
if (cellType.equals(CellType.BOOLEAN)) {
63+
toReturn.append(cell.getBooleanCellValue()).append(" | ");
64+
}
65+
}
66+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.poi.excel;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.io.IOException;
7+
import java.net.URISyntaxException;
8+
import java.nio.file.Paths;
9+
import java.text.ParseException;
10+
import java.text.SimpleDateFormat;
11+
12+
import org.junit.Before;
13+
import org.junit.Test;
14+
15+
public class ExcelUtilityUnitTest {
16+
private static final String FILE_NAME = "baeldung.xlsx";
17+
private String fileLocation;
18+
private static final String ENDLINE = System.getProperty("line.separator");
19+
private StringBuilder output;
20+
21+
@Before
22+
public void setupUnitTest() throws IOException, URISyntaxException, ParseException {
23+
output = new StringBuilder();
24+
output.append("--------------------------------------------------------------------").append(ENDLINE);
25+
output.append("Worksheet :Sheet1").append(ENDLINE);
26+
output.append("--------------------------------------------------------------------").append(ENDLINE);
27+
output.append("|| Name1 | Surname1 | 3.55696564113E11 | ").append(new SimpleDateFormat("dd/MM/yyyy").parse("4/11/2021").toString()).append(" | ‡ | ||")
28+
.append(ENDLINE);
29+
output.append("|| Name2 | Surname2 | 5.646513512E9 | ").append(new SimpleDateFormat("dd/MM/yyyy").parse("4/12/2021").toString()).append(" | false | ||")
30+
.append(ENDLINE);
31+
output.append("|| Name3 | Surname3 | 3.55696564113E11 | ").append(new SimpleDateFormat("dd/MM/yyyy").parse("4/11/2021").toString()).append(" | 7.17039641738E11 | ||")
32+
.append(ENDLINE);
33+
output.append("--------------------------------------------------------------------").append(ENDLINE);
34+
output.append("Worksheet :Sheet2").append(ENDLINE);
35+
output.append("--------------------------------------------------------------------").append(ENDLINE);
36+
output.append("|| Name4 | Surname4 | 3.55675623232E11 | 13/04/2021 | ||").append(ENDLINE);
37+
38+
fileLocation = Paths.get(ClassLoader.getSystemResource(FILE_NAME).toURI()).toString();
39+
}
40+
41+
@Test
42+
public void givenStringPath_whenReadExcel_thenReturnStringValue() throws IOException {
43+
assertEquals(output.toString(), ExcelUtility.readExcel(fileLocation));
44+
45+
}
46+
47+
@Test
48+
public void givenStringPath_whenReadExcel_thenThrowException() {
49+
assertThrows(IOException.class, () -> {
50+
ExcelUtility.readExcel("baeldung");
51+
});
52+
}
53+
54+
}

0 commit comments

Comments
 (0)