Skip to content

Commit 595062b

Browse files
authored
Merge pull request #48 from folio-org/MODEUSCNT-16
MODEUSCNT-16
2 parents 5bc7bf2 + decbf00 commit 595062b

File tree

1 file changed

+25
-6
lines changed
  • mod-erm-usage-counter-common/src/main/java/org/olf/erm/usage/counter/common

1 file changed

+25
-6
lines changed

mod-erm-usage-counter-common/src/main/java/org/olf/erm/usage/counter/common/ExcelUtil.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
import java.util.stream.Collectors;
1212
import java.util.stream.IntStream;
1313
import org.apache.commons.lang3.StringUtils;
14+
import org.apache.poi.ss.usermodel.Cell;
15+
import org.apache.poi.ss.usermodel.CellStyle;
16+
import org.apache.poi.ss.usermodel.CellType;
1417
import org.apache.poi.ss.usermodel.Row;
1518
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
1619
import org.apache.poi.ss.usermodel.Sheet;
@@ -45,9 +48,14 @@ public static String toCSV(InputStream inputStream) throws IOException {
4548
int lastCellNum = row.getLastCellNum() - 1;
4649
return IntStream.rangeClosed(0, lastCellNum)
4750
.mapToObj(
48-
cn ->
49-
row.getCell(cn, MissingCellPolicy.CREATE_NULL_AS_BLANK)
50-
.getStringCellValue())
51+
cn -> {
52+
Cell cell = row.getCell(cn, MissingCellPolicy.CREATE_NULL_AS_BLANK);
53+
if (cell.getCellType() == CellType.NUMERIC) {
54+
return String.valueOf((int) cell.getNumericCellValue());
55+
} else {
56+
return cell.getStringCellValue();
57+
}
58+
})
5159
.collect(Collectors.toList());
5260
})
5361
.collect(Collectors.toList());
@@ -72,13 +80,24 @@ public static InputStream fromCSV(String csvString) throws IOException {
7280
ByteArrayOutputStream baos = new ByteArrayOutputStream();
7381
try (Workbook wb = new XSSFWorkbook()) {
7482
Sheet sheet = wb.createSheet();
83+
CellStyle numberCellStyle = wb.createCellStyle();
84+
numberCellStyle.setDataFormat((short) 1);
7585

76-
List<String> line;
86+
List<String> columns;
7787
int rowNo = 0;
78-
while ((line = csvListReader.read()) != null) {
88+
while ((columns = csvListReader.read()) != null) {
7989
Row row = sheet.createRow(rowNo++);
8090
AtomicInteger cellNo = new AtomicInteger();
81-
line.forEach(s -> row.createCell(cellNo.getAndIncrement()).setCellValue(s));
91+
columns.forEach(
92+
s -> {
93+
Cell cell = row.createCell(cellNo.getAndIncrement());
94+
try {
95+
cell.setCellValue(Integer.parseInt(s));
96+
cell.setCellStyle(numberCellStyle);
97+
} catch (NumberFormatException e) {
98+
cell.setCellValue(s);
99+
}
100+
});
82101
}
83102

84103
wb.write(baos);

0 commit comments

Comments
 (0)