Java下載創建好的Excel模板


1.廢話不多說,直接上源碼

//從數據庫取數據創建表格	
private HSSFWorkbook exportStudentInfo(List<ExamStudentVo> studentList, List<ExamStudentVo> subjectName, Integer testId) { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheetlist = wb.createSheet(); sheetlist.protectSheet(Constant.EXCEL_LOCK_PASSWORD); HSSFRow row1 = sheetlist.createRow(0); row1.createCell(0).setCellValue(Constant.EXCEL_ID);// 身份標識 row1.createCell(1).setCellValue(testId); HSSFRow row2 = sheetlist.createRow(1); sheetlist.setColumnWidth(3, 15 * 256);// 設置寬度 sheetlist.setColumnWidth(4, 12 * 256); row1.setZeroHeight(true);// 隱藏行 sheetlist.setColumnHidden(1, true);// 隱藏列 org.apache.poi.hssf.usermodel.HSSFCellStyle unLockCellStyle = wb.createCellStyle(); unLockCellStyle.setLocked(false); row2.createCell(0).setCellValue("序號"); row2.createCell(1).setCellValue("學生ID"); row2.createCell(2).setCellValue("姓名"); row2.createCell(3).setCellValue("年級"); row2.createCell(4).setCellValue("班級"); int t = 5; for (int i = 0; i < subjectName.size(); ++i) { row1.createCell(t).setCellValue(subjectName.get(i).getSubjectId()); row2.createCell(t).setCellValue(subjectName.get(i).getSubjectName()); t++; } int temRow = 2; for (int i = 0; i < studentList.size(); ++i) { HSSFRow row3 = sheetlist.createRow(temRow); if (StringUtil.isNullOrEmpty(studentList.get(i))) { continue; } row3.createCell(0).setCellValue(i + 1); row3.createCell(1).setCellValue(studentList.get(i).getStudentId()); row3.createCell(2).setCellValue(studentList.get(i).getStudentName()); row3.createCell(3) .setCellValue(studentList.get(i).getStageName() + "部" + studentList.get(i).getEntranceYear() + "級"); row3.createCell(4).setCellValue(studentList.get(i).getClassName()); temRow++; int tem = 5; for (int j = 0; j < subjectName.size(); ++j) { row3.createCell(tem).setCellValue(HSSFCell.CELL_TYPE_STRING); row3.createCell(tem).setCellStyle(unLockCellStyle); tem++; } } return wb; }

 2.下載

              HSSFWorkbook wb = importScoreService.exportTestInfo(eq.getTestId());
    			String testName = importScoreService.getTestName(eq.getTestId());
    			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    			wb.write(byteArrayOutputStream);
    			testName = StringUtils.deleteWhitespace(testName);
    			String dateTime = DateFormatUtils.format(new Date(), "yyyyMMddHHmm");
    			//設置文件標題
    			String outFile = "學生成績/" + dateTime +"/" + testName + ".xls";
    			response.setContentType("application/vnd.ms-excel;charset=utf-8");
    			//對文件名編碼  
    			outFile = response.encodeURL(new String(outFile.getBytes("gb2312"), "iso8859-1"));
    			//使用Servlet實現文件下載的時候,避免瀏覽器自動打開文件
    			response.addHeader("Content-Disposition", "attachment;filename=" + outFile);
    			//將流寫進response輸出流中  
    			ServletOutputStream outputstream = response.getOutputStream(); 
    			byteArrayOutputStream.writeTo(outputstream); 
    			byteArrayOutputStream.close();  
    			outputstream.flush(); 

 3.下載完畢


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM