1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package pwc.taxtech.atms.common;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Optional;
public class POIUtil {
private static final Logger logger = LoggerFactory.getLogger(POIUtil.class);
public static final String XLSX = ".xlsx";
public static final String XLS = ".xls";
public static Optional<Workbook> cloneNewSheet(Sheet sheet, String fileName) {
Workbook workbook;
try {
if (StringUtils.endsWith(fileName, XLSX)) {
workbook = new XSSFWorkbook();
} else if (StringUtils.endsWith(fileName, XLS)) {
workbook = new HSSFWorkbook();
} else {
return Optional.empty();
}
Sheet tmpSheet = workbook.createSheet(sheet.getSheetName());
cloneSheet(sheet, tmpSheet);
return Optional.of(workbook);
} catch (Exception e) {
logger.error("cloneNewSheet error.", e);
}
return Optional.empty();
}
public static void cloneSheet(Sheet sheet, Sheet targetSheet) {
for (int r = sheet.getFirstRowNum(); r <= sheet.getLastRowNum(); r++) {
Row row = sheet.getRow(r);
Row targetRow = targetSheet.createRow(r);
for (int c = row.getFirstCellNum(); c <= row.getLastCellNum(); c++) {
Cell cell = row.getCell(c);
if (null == cell){
continue;
}
Cell targetCell = targetRow.createCell(c);
targetCell.setCellType(cell.getCellTypeEnum());
switch (cell.getCellTypeEnum()) {
case STRING:
targetCell.setCellValue(cell.getStringCellValue());
break;
case NUMERIC:
targetCell.setCellValue(cell.getNumericCellValue());
break;
case BOOLEAN:
targetCell.setCellValue(cell.getBooleanCellValue());
break;
case FORMULA:
targetCell.setCellFormula(cell.getCellFormula());
break;
// case ERROR:
//// case BLANK:
//// case _NONE:
default:
break;
}
if (null != cell.getCellComment()) {
targetCell.setCellComment(cell.getCellComment());
}
if (null != cell.getCellStyle()) {
targetCell.getCellStyle().cloneStyleFrom(cell.getCellStyle());
}
if (null != cell.getHyperlink()) {
targetCell.setHyperlink(cell.getHyperlink());
}
}
}
}
public static String getCellFormulaString(Cell cell) {
switch (cell.getCellTypeEnum()) {
case STRING:
case FORMULA:
case BLANK:
return cell.getStringCellValue();
case NUMERIC:
return String.valueOf(cell.getNumericCellValue());
case BOOLEAN:
return String.valueOf(cell.getBooleanCellValue());
default:
return StringUtils.EMPTY;
}
}
public static Optional<String> getFileSuffix(String fileName) {
if (StringUtils.endsWith(fileName, XLSX)) {
return Optional.of(XLSX);
} else if (StringUtils.endsWith(fileName, XLS)) {
return Optional.of(XLS);
} else {
return Optional.empty();
}
}
}