0.3.114 • Published 2 years ago

@zecaishao/kunchi-table v0.3.114

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

以下是一份示范代码,基于 Spring Boot,完成了 Excel 文件上传、解析、查询、排序、导出功能。

@RestController
@RequestMapping(\"/orders\")
public class OrderController {

@Autowired
private ProductService productService;

@PostMapping(\"/upload\")
public ResponseEntity uploadFile(@RequestParam(\"file\") MultipartFile file) throws IOException {
// 读取上传的 Excel 文件
InputStream inputStream = file.getInputStream();
Workbook workbook = WorkbookFactory.create(inputStream);

// 解析文件中的订单号
Set orderIds = new HashSet<>();
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
Cell orderIdCell = row.getCell(0);
if (orderIdCell != null && orderIdCell.getCellType() == CellType.STRING) {
orderIds.add(orderIdCell.getStringCellValue());
}
}

// 根据订单号查询商品信息,并按库存数量降序排列
List products = productService.findByOrderIds(orderIds);
Collections.sort(products, Comparator.comparing(Product::getStock).reversed());

// 导出查询结果到 Excel 文件中
Workbook resultWorkbook = new XSSFWorkbook();
Sheet resultSheet = resultWorkbook.createSheet(\"Products\");
int rowIndex = 0;
for (Product product : products) {
Row row = resultSheet.createRow(rowIndex++);
row.createCell(0).setCellValue(product.getName());
row.createCell(1).setCellValue(product.getStock());
}

// 将导出结果保存至文件中
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
resultWorkbook.write(outputStream);

LocalDate date = LocalDate.now();
String filename = String.format(\"ProductStock-%s.xlsx\", date.toString());
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData(\"attachment\", filename);
return new ResponseEntity<> (outputStream.toString(), headers, HttpStatus.OK);
}

}

这段代码实现了在 /orders/upload 路径下接收一个名为 file 的文件,并完成对这个文件的解析、查询、排序、导出操作。最终导出的 Excel 文件以附件形式发送给管理员。

开发者需要根据具体业务需求,对代码中的 ProductService 做相应的调整,以保证获取商品信息的正确性。

0.3.114

2 years ago

0.3.113

2 years ago

0.3.112

2 years ago

0.3.111

2 years ago

0.3.110

2 years ago

0.3.109

2 years ago

0.3.108

2 years ago

0.3.107

2 years ago

0.3.106

2 years ago

0.3.105

2 years ago

0.3.104

2 years ago

0.3.103

2 years ago

0.3.102

2 years ago

0.3.101

2 years ago

0.3.100

2 years ago

0.3.99

2 years ago

0.3.98

2 years ago

0.3.97

2 years ago

0.3.96

2 years ago

0.3.95

2 years ago

0.3.94

2 years ago

0.3.93

2 years ago

0.3.92

2 years ago

0.3.91

2 years ago

0.3.90

2 years ago

0.3.89

2 years ago

0.3.88

2 years ago

0.3.87

2 years ago

0.3.86

2 years ago

0.3.85

2 years ago

0.3.84

2 years ago

0.3.83

2 years ago

0.3.82

2 years ago

0.3.81

2 years ago

0.3.80

2 years ago

0.3.79

2 years ago

0.3.78

2 years ago

0.3.77

2 years ago

0.3.76

2 years ago

0.3.75

2 years ago

0.3.74

2 years ago

0.3.73

2 years ago

0.3.72

2 years ago

0.3.71

2 years ago

0.3.70

2 years ago

0.3.68

2 years ago

0.3.67

2 years ago

0.3.66

2 years ago

0.3.65

2 years ago

0.3.64

2 years ago

0.3.63

2 years ago

0.3.62

2 years ago

0.3.61

2 years ago

0.3.60

2 years ago

0.3.59

2 years ago

0.3.58

2 years ago

0.3.57

2 years ago

0.3.56

2 years ago

0.3.55

2 years ago

0.3.54

2 years ago

0.3.53

2 years ago

0.3.52

2 years ago

0.3.51

2 years ago

0.3.50

2 years ago

0.3.49

2 years ago

0.3.48

2 years ago

0.3.47

2 years ago

0.3.46

2 years ago

0.3.45

2 years ago

0.3.44

2 years ago

0.3.43

2 years ago

0.3.42

2 years ago

0.3.41

2 years ago

0.3.40

2 years ago

0.3.39

2 years ago

0.3.38

2 years ago

0.3.37

2 years ago

0.3.36

2 years ago

0.3.35

2 years ago

0.3.34

2 years ago

0.3.33

2 years ago

0.3.32

2 years ago

0.3.31

2 years ago

0.3.30

2 years ago

0.3.29

2 years ago

0.3.28

2 years ago

0.3.27

2 years ago

0.3.26

2 years ago

0.3.25

2 years ago

0.3.24

2 years ago

0.3.23

2 years ago

0.3.22

2 years ago

0.3.21

2 years ago

0.3.20

2 years ago

0.3.19

2 years ago

0.3.18

2 years ago

0.3.17

2 years ago

0.3.16

2 years ago

0.3.15

2 years ago

0.3.14

2 years ago

0.3.13

2 years ago

0.3.12

2 years ago

0.3.11

2 years ago

0.3.10

2 years ago

0.3.9

2 years ago

0.3.8

2 years ago

0.3.7

2 years ago

0.3.6

2 years ago

0.3.5

2 years ago

0.3.4

2 years ago

0.3.3

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.9

2 years ago

0.2.8

2 years ago

0.2.7

2 years ago

0.2.6

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago