我目前使用的是Apache POI。为了开发我的应用程序,我使用了大多数教程中提出的方法。
public void generateExcel() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("ma feuille");
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell((short)0);
cell.setCellValue(10);
row.createCell((short)1).setCellValue(20);
FileOutputStream fileOut;
try {
fileOut = new FileOutputStream("monfichier.xls");
wb.write(fileOut);
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
但是为了进一步开发,我需要能够下载文件,而不仅仅是在目录中创建。我看了一点HttpServletRequest,但我完全迷失了。
提前谢谢!
答案 0 :(得分:0)
您需要创建某种端点来调用,即Spring Controller端点。你应该调用这个函数:
public void createExcelFile(final HttpServletResponse response) {
XSSFWorkbook xssfWorkbook = null;
try {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment; filename=" file.xlsx");
xssfWorkbook = new XSSFWorkbook();
final XSSFSheet sheet = xssfWorkbook.createSheet("sheet1");
writeExcelOutputData(//WRITE DATA TO FILE/SHEET/CELLS);
xssfWorkbook.write(response.getOutputStream());
xssfWorkbook.close();
} catch (final Exception e) {
LOGGER.error("File not created for download"));
}
}