我有这个 Excel 工作簿存储在我的资源文件夹中。考虑到这个 excel 有 9 页,我想提取一个特定的页面并将其从 byte [] 返回。
我创建了这个控制器
@GetMapping(value = "/getSinglePage/{pageNumber}")
private ResponseEntity<byte[]> getExcelByPageNumber(@PathVariable Integer pageNumber) throws IOException {
return new ResponseEntity<>(service.getExcelByPageNumber(pageNumber), HttpStatus.OK);
}
服务代码包含——
public byte[] getExcelByPageNumber(Integer pageNumber) throws IOException {
Workbook workbook = null;
byte[] byteArray = null;
// here file.xls is my excel sheet that contains 9 sheet for example
workbook = WorkbookFactory.create(loader.getResource("classpath:file.xls").getInputStream());
//gets the sheet from given page number
Sheet sheet = workbook.getSheetAt(pageNumber);
// now i want to return this sheet in the form of byte[]
return byteArray;
}
我应该如何将它返回为 byte[] 的形式?