fix: 修复地材数据上传编码未入库的问题

This commit is contained in:
han0
2025-03-07 09:23:43 +08:00
parent e37d499bbc
commit 15c25e895c
2 changed files with 57 additions and 2 deletions

View File

@@ -18,6 +18,8 @@ import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
/**
* <p>
@@ -101,6 +103,7 @@ public class LocalMaterial extends Model<LocalMaterial> {
public LocalMaterial (Row row, MaterialTask task) {
var policy = Row.MissingCellPolicy.CREATE_NULL_AS_BLANK;
row.getCell(0).setCellType(CellType.STRING);
this.setMaterialId(row.getCell(0, policy).getStringCellValue());
this.setCity(row.getCell(1, policy).getStringCellValue());
this.setCounty(row.getCell(2, policy).getStringCellValue());
@@ -132,4 +135,55 @@ public class LocalMaterial extends Model<LocalMaterial> {
this.setRemark(item.getRemark());
return this;
}
private String getCellValueAsString(Cell cell) {
if (cell == null) {
return "";
}
switch (cell.getCellType()) {
case STRING:
return cell.getStringCellValue();
case NUMERIC:
// 处理数值类型,转换为字符串
double numericValue = cell.getNumericCellValue();
// 如果是整数,去掉小数点
if (numericValue == (long) numericValue) {
return String.format("%.0f", numericValue);
}
return String.valueOf(numericValue);
case BOOLEAN:
return String.valueOf(cell.getBooleanCellValue());
case FORMULA:
try {
return cell.getStringCellValue();
} catch (Exception e) {
return String.valueOf(cell.getNumericCellValue());
}
default:
return "";
}
}
private BigDecimal getCellValueAsDecimal(Cell cell) {
if (cell == null) {
return null;
}
try {
switch (cell.getCellType()) {
case NUMERIC:
return BigDecimal.valueOf(cell.getNumericCellValue());
case STRING:
String stringValue = cell.getStringCellValue().trim();
return stringValue.isEmpty() ? null : new BigDecimal(stringValue);
case FORMULA:
return BigDecimal.valueOf(cell.getNumericCellValue());
default:
return null;
}
} catch (Exception e) {
return null;
}
}
}

View File

@@ -25,8 +25,8 @@ public class UploadFileTest {
@Test
public void test() throws Exception {
// var filePath = "C:\\Users\\Administrator\\Desktop\\材料管理系统模版\\历史数据\\地材-202405.xlsx";
// var filePath = "C:\\Users\\Administrator\\Documents\\对比材料精简9月.xlsx";
var filePath = "D:\\defaultUploadFolder\\defaultBucketName\\2025\\2\\12\\1889631594115809281.xls";
var filePath = "C:\\Users\\Administrator\\Documents\\地材.xlsx";
// var filePath = "D:\\defaultUploadFolder\\defaultBucketName\\2025\\2\\12\\1889631594115809281.xls";
var file = new File(filePath);
// var multipartFile = this.mockMultipartFile(file, "相邻城市价格.xlsx", "application/x-zip-compressed");
var multipartFile = this.mockMultipartFile(file);
@@ -40,6 +40,7 @@ public class UploadFileTest {
* 1810481965629005826 公路局.xlsx 40201
* 1810482216079302658 交通局.xlsx 40101
* 1810497092512178177 地材.xlsx 1101
* 1897813936821006338 地材.xlsx 1101
* 1810506191866003457 地材-202405.xlsx 1101
* 1871732061747179521 相邻城市.xlsx 600
* 1866412571689631746 三明钢铁.xlsx 301