Skip to content

Commit 7a62ed3

Browse files
committedMar 24, 2025·
Merge branch 'master' into rxsocks
2 parents e88f5af + 854d53d commit 7a62ed3

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
 

‎rxlib-x/src/main/java/org/rx/util/Helper.java

+107
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
import org.apache.poi.ss.usermodel.*;
88
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
99
import org.rx.core.Numbers;
10+
import org.rx.exception.InvalidException;
1011
import org.rx.spring.MiddlewareConfig;
1112
import org.rx.spring.SpringContext;
13+
import org.rx.util.function.QuadraFunc;
14+
import org.rx.util.function.TripleFunc;
1215

1316
import javax.mail.*;
1417
import javax.mail.internet.InternetAddress;
@@ -61,6 +64,110 @@ protected PasswordAuthentication getPasswordAuthentication() {
6164
}
6265
}
6366

67+
public static void replaceExcel(InputStream in, OutputStream out, String sheetName, TripleFunc<Integer, List<Object>, List<Object>> fn, boolean is2003File) {
68+
replaceExcel(in, out, sheetName, fn, is2003File, false);
69+
}
70+
71+
@SneakyThrows
72+
public static void replaceExcel(@NonNull InputStream in, @NonNull OutputStream out, @NonNull String sheetName,
73+
TripleFunc<Integer, List<Object>, List<Object>> fn, boolean is2003File, boolean skipColumn) {
74+
FormulaEvaluator evaluator = null;
75+
try (Workbook workbook = is2003File ? new HSSFWorkbook(in) : new XSSFWorkbook(in)) {
76+
Sheet sheet = workbook.getSheet(sheetName);
77+
if (sheet == null) {
78+
throw new InvalidException("Sheet {} not found", sheetName);
79+
}
80+
81+
List<Object> cells = new ArrayList<>();
82+
int firstRowNum = sheet.getFirstRowNum();
83+
for (int rowIdx = skipColumn ? firstRowNum + 1 : firstRowNum; rowIdx <= sheet.getLastRowNum(); rowIdx++) {
84+
Row row = sheet.getRow(rowIdx);
85+
if (row == null) {
86+
throw new InvalidException("Sheet row {} not found", rowIdx);
87+
// fn.accept(rowIndex, rowIndex, Collections.emptyList());
88+
// continue;
89+
}
90+
91+
cells.clear();
92+
short firstCellNum = row.getFirstCellNum();
93+
for (int i = firstCellNum; i < row.getLastCellNum(); i++) {
94+
// if (i < firstCellNum) {
95+
// cells.add(null);
96+
// continue;
97+
// }
98+
Cell cell = row.getCell(i);
99+
if (cell == null) {
100+
throw new InvalidException("Sheet row {} cell {} not found", rowIdx, i);
101+
// cells.add(null);
102+
// continue;
103+
}
104+
105+
CellType cellType = cell.getCellType();
106+
if (cellType == CellType.FORMULA) {
107+
if (evaluator == null) {
108+
evaluator = workbook.getCreationHelper().createFormulaEvaluator();
109+
}
110+
cellType = evaluator.evaluateFormulaCell(cell);
111+
}
112+
Object value;
113+
// System.out.println(i + ":" + cellType);
114+
switch (cellType) {
115+
case NUMERIC:
116+
if (!eq(cell.getCellStyle().getDataFormatString(), "General")) {
117+
value = cell.getDateCellValue();
118+
} else {
119+
double n = cell.getNumericCellValue();
120+
boolean b = Numbers.hasPrecision(n);
121+
if (b) {
122+
value = (int) n;
123+
} else {
124+
value = n;
125+
}
126+
//will auto wrap to double
127+
// value = b ? (int) n : n;
128+
}
129+
break;
130+
case BOOLEAN:
131+
value = cell.getBooleanCellValue();
132+
break;
133+
default:
134+
// value = cell.getStringCellValue();
135+
if (cell.getCellType() == CellType.ERROR) {
136+
cell.setCellType(CellType.STRING);
137+
}
138+
value = cell.toString();
139+
break;
140+
}
141+
cells.add(value);
142+
}
143+
List<Object> newCells = fn.apply(rowIdx, cells);
144+
if (newCells == null) {
145+
sheet.removeRow(row);
146+
continue;
147+
}
148+
149+
int lastCellNum = firstCellNum + newCells.size();
150+
for (int i = firstCellNum; i < lastCellNum; i++) {
151+
Cell cell = row.getCell(i);
152+
if (cell == null) {
153+
cell = row.createCell(i);
154+
}
155+
Object newValue = newCells.get(i - firstRowNum);
156+
String value;
157+
if (newValue instanceof Date) {
158+
value = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(newValue);
159+
} else if (newValue == null) {
160+
value = null;
161+
} else {
162+
value = String.valueOf(newValue);
163+
}
164+
cell.setCellValue(value);
165+
}
166+
}
167+
workbook.write(out);
168+
}
169+
}
170+
64171
public static Map<String, List<Object[]>> readExcel(InputStream in, boolean is2003File) {
65172
return readExcel(in, is2003File, false, false);
66173
}

0 commit comments

Comments
 (0)
Please sign in to comment.