在我的项目中,我将创建一个ExcelWorkBook
并将一些数据写入该工作簿。
编写后,我希望它保存到用户下载路径,因此尝试了以下代码:
XSSFWorkbook hwb=new XSSFWorkbook();
XSSFSheet sheet = hwb.createSheet("Exam Marks Entry");
//writing data to workbook
//then targeting users download path as follows
String home = System.getProperty("user.home");
File file = new File(home+"/Downloads/"+mainDisplayDto.getClassName()+" "+mainDisplayDto.getExamName()+".xlsx");
FileOutputStream fileOut = new FileOutputStream(file);
hwb.write(fileOut);
以上代码仅在应用程序为本地时有效(保存工作簿以下载路径),而在应用程序位于 VPS 上时不起作用。
当应用程序不在本地运行时,如何将其保存到用户系统下载文件夹中?
我的问题也将得到解决,如果将数据写入工作簿后我可以在系统中打开excel文件,以便用户将其保存在所需的位置。
有人可以帮我吗?
答案 0 :(得分:0)
您可以在锚标记或按钮元素上使用window.location.href =“”,并且当用户单击它时,您应该调用程序,响应将返回excel工作表并在用户的下载文件夹中下载。
例如: window.location.href =“ / downloadExcelMarks” 是我指的控制器的网址。它将调用我的控制器,然后我将从控制器发送响应以发送excel,如下所示:
SELECT person_id
FROM game_table
WHERE '{21, 10}' && game_ids
答案 1 :(得分:0)
我想您可以理解,将文件保存到下载位置是浏览器的属性。您可以将浏览器的默认下载位置设置为文件系统中的任何位置。如何指定要存储在不同用户使用不同文件系统的用户系统中的响应。
答案 2 :(得分:0)
我在@Jai Prakash建议的帮助下解决了,并更改了我的ajax调用和一些Java代码,请查看代码
ajax通话:
$(document).on("click","#downlodMarksSheet",function(event){
var examCatId=$("#meExamMainEV").val();
var classId=$(".mainContainer #meClass").val();
var secId=$(".mainContainer #sectionId").val() ;
var stringFromDate=$(".mainContainer #FromDate").val();
var stringToDate=$(".mainContainer #ToDate").val();
var url = contextPath+"/excel/exportExcel/"+classId+"/"+secId+"/"+examCatId+"/"+stringFromDate.replace(/\//g, "-")+"/"+stringToDate.replace(/\//g, "-");
window.location.href=url;
});
**java code :**
@GetMapping(value = "/exportExcel/{classId}/{secId}/{examCatId}/{stringFromDate}/{stringToDate}")
public HttpServletResponse updateStudentGeneralDetailss(@PathVariable("classId") int classId,
@PathVariable("secId") int secId,@PathVariable("examCatId") int examCatId,
@PathVariable("stringFromDate") String stringFromDate,
@PathVariable("stringToDate") String stringToDate,HttpServletRequest request,HttpServletResponse response) {
try {
String filename="";
XSSFWorkbook hwb=new XSSFWorkbook();
XSSFSheet sheet = hwb.createSheet("Exam Marks Entry");
XSSFRow subsHead= sheet.createRow(0);
subsHead.createCell(1).setCellValue("");
XSSFRow subSubsIdsHead= sheet.createRow(1);
XSSFRow subSubs= sheet.createRow(2);
response.setContentType("application/vnd.ms-excel");
ServletOutputStream outStream = response.getOutputStream();
XSSFColor myColor = new XSSFColor(Color.YELLOW);
XSSFCellStyle style = hwb.createCellStyle();
style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
style.setFillBackgroundColor(IndexedColors.YELLOW.getIndex());
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderRight(CellStyle.BORDER_THIN);
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderTop(CellStyle.BORDER_THIN);
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
ExmaExportDTO mainDisplayDto = service.getExamPartten(examCatId, classId, secId,stringFromDate.replaceAll("-", "/"),stringToDate.replaceAll("-", "/"));
List<ExamStudentMarksDisplaySubjectsDTO> subjects= mainDisplayDto.getSubjectsList();
int i=2;
int formCol=2;
int subsubCol=2;
sheet.addMergedRegion(new CellRangeAddress(0,0,0,1));
sheet.addMergedRegion(new CellRangeAddress(1,1,0,1));
Cell cel=subSubs.createCell(0);
cel.setCellStyle(style);
cel.setCellValue("Student Ids");
Cell cel1=subSubs.createCell(1);
cel1.setCellStyle(style);
cel1.setCellValue("Student Name");
/*iterating subjects*/
for(ExamStudentMarksDisplaySubjectsDTO subObj : subjects)
{
Cell cell=subsHead.createCell(formCol);
cell.setCellStyle(style);
cell.setCellValue(subObj.getSubjectName());;
List<ExamStudentMarksDisplaySubjectsSubCatDTO> subSubj=subObj.getSubCatMarks();
int subSubjLen=subSubj.size();
int toCol=formCol+subSubjLen;
sheet.addMergedRegion(new CellRangeAddress(0,0,formCol,toCol-1));
for(ExamStudentMarksDisplaySubjectsSubCatDTO subsubObj :subSubj)
{
Cell cell1=subSubsIdsHead.createCell(subsubCol);
cell1.setCellStyle(style);
cell1.setCellValue(subsubObj.getExmSubjectSubCategory());
Cell cell2=subSubs.createCell(subsubCol);
cell2.setCellStyle(style);
cell2.setCellValue(subsubObj.getSubCatName()+"("+subsubObj.getSubCatMaxMarks()+")");
subsubCol++;
}
formCol=subSubjLen+formCol;;
i++;
}
Cell cella=subSubs.createCell(subsubCol);
cella.setCellStyle(style);
cella.setCellValue("Description");
Cell cell1a=subSubs.createCell(subsubCol+1);
cell1a.setCellStyle(style);
cell1a.setCellValue("No.of Working Days");
Cell cell2a=subSubs.createCell(subsubCol+2);
cell2a.setCellStyle(style);
cell2a.setCellValue("No.of Present Days");
List<ExamStudentMarksDisplayStudentsDTO> students=mainDisplayDto.getStudentList();
/*iterating students*/
int j=3;
for(ExamStudentMarksDisplayStudentsDTO stu : students)
{
XSSFRow rows= sheet.createRow(j);
Cell cell3=rows.createCell(0);
cell3.setCellStyle(style);
cell3.setCellValue(stu.getStudentId());
String lastName="";
if(stu.getLastMame() !=null)
{
lastName=stu.getLastMame();
}
Cell cell4=rows.createCell(1);
cell4.setCellStyle(style);
cell4.setCellValue(stu.getFirstName()+" "+lastName);
j++;
}
response.setHeader("Content-Disposition", "attachment; filename="+mainDisplayDto.getClassName()+""+mainDisplayDto.getExamName()+".xlsx");
hwb.write(outStream);
outStream.close();
}
catch (Exception e) {
e.printStackTrace();
}
return response;
}
谢谢!