• SSM框架批量下载


    批量下载

    首先建立一个类

    DbToExcel.Java

    	/**
    	 * 下载
    	 */
    	
    	public  void dBToExcel1(List <Course> list,String[] titles, String file)
    			throws Exception {
    	
    		WritableWorkbook wwb = null;
    		WritableSheet ws = null;
    		wwb = Workbook.createWorkbook(new File(file));
    		ws = wwb.createSheet("sheet1", 0);
    
    		for (int i = 0; i < titles.length; i++) {
    			ws.addCell(new Label(i, 0, titles[i]));
    		}
    		int count = 1;
    		Course course=new Course();
    		for(int i=0;i<list.size();i++){
    			course=list.get(i);
    			ws.addCell(new Label(0, i+1,String.valueOf(course.getId())));
    			ws.addCell(new Label(1, i+1,course.getName() ));
    			ws.addCell(new Label(2, i+1,String.valueOf(course.getHour() )));	
    //(0,i)对应表格的第1行第i列	
    		}		
    		wwb.write();
    
    		if (wwb != null)
    			wwb.close();
    	
    
    	}

    Dao层:

    	<select id="findCourse1"  resultType="course"  parameterType="course">
    		select * from tb_course
    		<where>
    			<if test="name!=null and name!=''">
    				<bind name="pattern_name" value="'%'+_parameter.getName()+'%'" />
    				name like #{pattern_name}
    			</if>
    			<if test="hour!=null and hour!=0 and hour!=''">
    				and hour=#{hour}
    			</if>
    		</where>
    	</select>
    public List<Course> findCourse1(RowBounds rowBounds,Course course);

    服务层:

    	public Page<Course> findCoursePage(Integer page,Integer rows, Course course);
    	@Override
    	public Page<Course> findCoursePage(Integer page, Integer rows) {
    		// TODO Auto-generated method stub
    		RowBounds rowBounds = new RowBounds((page-1) * rows, rows);
    //		// 查询列表
    		List<Course> courses =courseDao.findCourse(rowBounds);
    //		
    //
    //		// 查询课程列表总记录数
    		Integer count = courseDao.findCourseCount();
    //		// 创建Page返回对象
    		Page<Course> result = new Page<Course>();
    		result.setPage(page);
    		result.setRows(courses);
    		result.setSize(rows);
    		result.setTotal(count);
    		return result;
    	}

    控制层

    	@Autowired
    	private CourseService courseService;
    	@Autowired
    	private DbToExcel dbToExcel1;
    	public DbToExcel getDbToExcel1() {
    		return dbToExcel1;
    	}
    	public void setDbToExcel1(DbToExcel dbToExcel1) {
    		this.dbToExcel1 = dbToExcel1;
    	}
    
    /**
    	 * 下载
    	 */
    	@RequestMapping("/downloadCourse.action")
    	public ResponseEntity<byte[]> downloadCourse(Model model,Integer[] courseId,HttpServletRequest request)throws Exception {
    		List<Course> courses=courseService.findCourseByIds(courseId);
    		String[] titles={"课程编号","课程名","学时"};
    		String path="D:/test/";
    		String filename="test.xls";
    		File filePath = new File(path);
    		try {
    			
    			if (!filePath.exists()) {
    				filePath.mkdirs();
    			}
    			File testFile = new File(path+filename);
    			if(!testFile .exists()) {
    			testFile.createNewFile();
    			System.out.println("测试文件不存在");
    			}
    			
    			dbToExcel1.dBToExcel1(courses,titles,path+filename);
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    		 File file = new File(path+filename);
    		    // 对文件名编码,防止中文文件乱码
    		  filename = this.getFilename(request, filename);
    		    // 设置响应头
    		    HttpHeaders headers = new HttpHeaders();
    		    // 通知浏览器以下载的方式打开文件
    		    headers.setContentDispositionFormData("attachment", filename);
    		    // 定义以流的形式下载返回文件数据
    		    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    		    // 使用Sring MVC框架的ResponseEntity对象封装返回下载数据
    		   return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
    				   headers,HttpStatus.OK);
    	}
    	public String getFilename(HttpServletRequest request,
    			 String filename) throws Exception { 
    			// IE不同版本User-Agent中出现的关键词
    			String[] IEBrowserKeyWords = {"MSIE", "Trident", "Edge"};  
    			// 获取请求头代理信息
    			String userAgent = request.getHeader("User-Agent");  
    			for (String keyWord : IEBrowserKeyWords) { 
    			if (userAgent.contains(keyWord)) { 
    			//IE内核浏览器,统一为UTF-8编码显示
    			return URLEncoder.encode(filename, "UTF-8");
    			}
    			}  
    			//火狐等其它浏览器统一为ISO-8859-1编码显示
    			return new String(filename.getBytes("UTF-8"), "ISO-8859-1");  
    		}  
    	

    前端调用:

    <form action="${pageContext.request.contextPath }/downloadCourse.action" method="post">
    					<table class="table  table-hover">
    						<thead>
    							<tr>
    								<th></th>
    								<th>课程编号</th>
    								<th>课程名字</th>
    								<th>课时</th>
    								<th>状态</th>
    							</tr>
    						</thead>
    						<tbody>
    						
    							<c:forEach var="course" items="${page.rows}" varStatus="status" >
    <%-- 								<tr <c:if test="${status.count%2=='0'}">class="success"</c:if>> --%>
    									<tr>
    									<td><input type="checkbox" name="courseId" value="${course.id }"></td>
    									<td>
    										<c:out value="${course.id }"/>
    									</td>
    									<td>
    										<c:out value="${course.name }"/>
    									</td>
    									<td>
    										<c:out value="${course.hour }"/>
    									</td>
    									
    									<td>
    										<a href="${pageContext.request.contextPath }/selectCourse1.action?courseId=${course.id }">选课 </a>
    									</td>
    								</tr>
    							</c:forEach>
    						</tbody>
    					</table>
    					<input type="submit" value="下载">
    					</form>
    不经一番彻骨寒,哪有梅花扑鼻香?
  • 相关阅读:
    JSP界面之间如何实现传值
    绝对路径的用法
    每日日报2020.8.12 1905
    每日日报2020.8.8 1905
    每日日报2020.8.17 1905
    每日日报2020.8.15 1905
    每日日报2020.8.13 1905
    每日日报2020.8.10 1905
    每日日报2020.8.16 1905
    每日日报2020.8.9 1905
  • 原文地址:https://www.cnblogs.com/zongyao/p/13831212.html
Copyright © 2020-2023  润新知