Vì vậy, tôi có bộ điều khiển Spring và tôi muốn tạo tệp Excel và trả về tệp để trình duyệt tải xuống.Trả lại tệp có thể tải xuống Excel từ Spring
Tôi đang sử dụng JEXcelApi.
Đây là mã điều khiển của tôi
@RequestMapping(value="/excel/cols/{colString}/rows/{rowString}/", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<String> exportExcel(HttpServletResponse response,
@PathVariable List<String> colString,
@PathVariable List<String> rowString) throws JSONException, IOException, WriteException {
WritableWorkbook workbook = Workbook.createWorkbook(new File("exported.xls"));
WritableSheet sheet = workbook.createSheet("Exported",0);
String[] cols = colString.get(0).split(",");
String[] rows = rowString.get(0).split(",");
for(int i = 0; i < cols.length;i++){
Label label = new Label(i,0, cols[i]);
sheet.addCell(label);
}
int excelCol = 0;
int excelRow = 1;
for(int i = 0; i < rows.length;i++){
Label label = new Label(excelCol,excelRow, rows[i]);
sheet.addCell(label);
excelCol++;
if((i+1) % cols.length == 0){
excelCol = 0;
excelRow++;
}
}
workbook.write();
workbook.close();
return null;
}
Làm thế nào để làm điều đó? Tôi nghi ngờ có một số tiêu đề nội dung tôi có thể thiết lập. Tôi biết một phương pháp là sử dụng lớp xem Tóm tắt Excel của Spring, nhưng có phương pháp đơn giản hơn không?
làm cách nào để viết các byte? – praks5432
@ praks5432 Đọc byte từ tệp và ghi chúng vào đầu ra phản hồi. –
như vậy là sẽ được thực hiện trên đối tượng bảng tính? – praks5432