我有一个springmvc项目,是为我的团队开发的。我有一个端点,它使用ApachePOI创建工作簿,然后导出到.xlsx文件,但我的代码似乎是在应用程序主机上而不是在用户的计算机上写入文件。我知道我错过了什么,但我尝试了我在互联网上发现的没有任何运气。如果您能帮忙,我们将不胜感激。
报告终结点
@RequestMapping(value = "/report", method = RequestMethod.GET)
String report(HttpServletRequest rq, Model model) throws FileNotFoundException, IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Product");
sheet.setColumnWidth(0, 6000);
sheet.setColumnWidth(1, 4000);
Row header = sheet.createRow(0);
XSSFFont font = ((XSSFWorkbook) workbook).createFont();
font.setFontName("Calibri");
font.setFontHeight(16);
CellStyle headerStyle = workbook.createCellStyle();
headerStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
// headerStyle.setFont(font);
Cell headerCell = header.createCell(0);
headerCell.setCellValue("Product Name");
headerCell.setCellStyle(headerStyle);
headerCell = header.createCell(1);
headerCell.setCellValue("Manufacturer");
headerCell.setCellStyle(headerStyle);
headerCell = header.createCell(2);
headerCell.setCellValue("Model No.");
headerCell.setCellStyle(headerStyle);
headerCell = header.createCell(3);
headerCell.setCellValue("Part No..");
headerCell.setCellStyle(headerStyle);
headerCell = header.createCell(4);
headerCell.setCellValue("Qauntity");
headerCell.setCellStyle(headerStyle);
headerCell = header.createCell(5);
headerCell.setCellValue("Location");
headerCell.setCellStyle(headerStyle);
CellStyle rowStyle = workbook.createCellStyle();
rowStyle.setWrapText(true);
List<Items> allItems = IT.getAllItem();
int rowsCount = 1;
for(Items eachItem : allItems){
Row row = sheet.createRow(rowsCount++);
Cell cell = row.createCell(0);
cell.setCellValue(eachItem.getItemName());
cell.setCellStyle(rowStyle);
cell = row.createCell(1);
cell.setCellValue(eachItem.getManufacturer());
cell.setCellStyle(rowStyle);
cell = row.createCell(2);
cell.setCellValue(eachItem.getModelNo());
cell.setCellStyle(rowStyle);
cell = row.createCell(3);
cell.setCellValue(eachItem.getPartNo());
cell.setCellStyle(rowStyle);
cell = row.createCell(4);
cell.setCellValue(eachItem.getQuantity());
cell.setCellStyle(rowStyle);
for(Locations locations : eachItem.getLocations()){
cell = row.createCell(5);
cell.setCellValue(locations.getLocationName());
cell.setCellStyle(rowStyle);
}
}
String getFilePath = "C://reports//";
Path path = Paths.get(getFilePath);
if(!Files.exists(path)) {
Files.createDirectories(path);
}else{
System.out.print("file exist");
}
try (FileOutputStream outputStream = new FileOutputStream(getFilePath + "Invenotry_Report" + date.format(formatter) + ".xlsx")) {
workbook.write(outputStream);
workbook.close();
outputStream.flush();
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "redirect:home";
}
}
1条答案
按热度按时间rsaldnfx1#
通过使用servlet响应找到了答案。