ssm导出

1.导出数据的方法

    package com.han.controller;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.han.pojo.Employee;
import com.han.service.ServiceDao;


@Controller
@RequestMapping("bmyg")
public class BmYgController {


@Autowired
ServiceDao ser;


@RequestMapping("selectYg")
public String selectYg(Model model,String name){

System.out.println(name);
List<Employee> list = ser.selectYg(name);
model.addAttribute("list", list);
return "ok";




}

@RequestMapping("daochu")
@ResponseBody
public String daochu(Model model,String[] idss ) throws IOException{
BufferedWriter bufferedWriter=null;
//将String类型的数组转成int类型
int[] ids=new int[idss.length];
for (int i = 0; i < idss.length; i++) {
ids[i]=Integer.parseInt(idss[i]);
}
List<Employee> list = ser.selectbfYg(ids);
//创建一个File
File file=new File("D://employees.txt");
//判断文件是否存在
if(!file.exists()){
//创建文件
file.mkdir();

}
//创建一个FileOutputStream(文件输出流) 向指定的文件输出数据
FileOutputStream outputStream = new FileOutputStream(file);
//将字节流转换为字符流          如果不指定字符编码格式 默认为   GBK
OutputStreamWriter writer = new OutputStreamWriter(outputStream, "utf-8");
//缓冲字符流
bufferedWriter = new BufferedWriter(writer);
for (Employee employee : list) {
//写入
bufferedWriter.write(employee.getId()+"#"+employee.getName()+"#"+employee.getPosition()+"#"+employee.getSalary()
+"#"+employee.getDepartment().getDname());
//换行
bufferedWriter.newLine();
}

//刷新
bufferedWriter.flush();
//关闭
bufferedWriter.close();
outputStream.close();

return "ok";




}
}

2.Dao层方法

package com.han.dao;


import java.util.List;


import org.apache.ibatis.annotations.Param;


import com.han.pojo.Employee;


public interface BmYgMapper {
public List<Employee> selectYg(@Param("name")String name);
public List<Employee> selectbfYg(@Param("ids")int[] ids);
}

Mapper动态代理

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">


<mapper namespace="com.han.dao.BmYgMapper">


<select id="selectYg" parameterType="String" resultMap="yg">
select * from t_employee e,t_department d where e.did=d.id
<if test="name!=null and name!=''">
and e.name like '%${name}%'
</if>

</select>
<select id="selectbfYg" parameterType="int" resultMap="yg">
select * from t_employee e,t_department d where e.did=d.id

<foreach collection="ids" item="id" open="and e.id in (" separator="," close=")">
#{id}
</foreach>

</select>




<resultMap type="com.han.pojo.Employee" id="yg">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="position" property="position"/>
<result column="salary" property="salary"/>
<association property="department" javaType="com.han.pojo.Department">
<id column="id" property="id"/>
<result column="dname" property="dname"/>
</association>
</resultMap>
</mapper>

3.jsp界面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'ok.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.8.3.js"></script>
  <script type="text/javascript">
  $(function(){
alert("aaaaa");
  $("#chu").click(function(){
 
  var a=new Array;
  $("input:checkbox:checked").each(function(){
 
  a.push($(this).val());
 
 
 
 
  });
  location.href="${pageContext.request.contextPath }/bmyg/daochu?idss="+a;
 
 
 
  });
 
   });
 
 
  </script>
  </head>
  
  
  <body>
    <table border="1">
     <tr><td colspan="5">
<form action="${pageContext.request.contextPath }/bmyg/selectYg">
      姓名<input type="text" name="name">
       <input type="submit" value="查询">
</form> 
   <input type="button" value="导出" id="chu"></td>
</tr><tr>
   <td>选择</td> 
   <td>序号</td> 
   <td>姓名</td> 
   <td>职位</td> 
   <td>薪资</td> 
   <td>部门</td>
</tr>
    <c:forEach items="${list }" var="list">
    <tr>
   <td><input type="checkbox" name="xuan" value="${list.id }"></td> 
   <td>${list.id }</td> 
<td>${list.name }</td> 
<td>${list.position }</td> 
<td>${list.salary }</td> 
<td>${list.department.dname }</td>
</tr>
    </c:forEach>
    
    </table>
  </body>
</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
导出Excel是一个常见的功能需求,以下是一种基于SSM框架的导出Excel的实现方式: 1. 首先需要引入POI相关的依赖,可以在pom.xml中添加以下依赖: ``` <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.1</version> </dependency> ``` 2. 在controller中添加导出Excel的方法,如下所示: ``` @RequestMapping("/exportExcel") public void exportExcel(HttpServletResponse response) throws IOException { // 获取要导出的数据 List<Student> studentList = studentService.getAllStudents(); // 创建工作簿 XSSFWorkbook workbook = new XSSFWorkbook(); // 创建工作表 XSSFSheet sheet = workbook.createSheet("学生信息表"); // 创建表头 XSSFRow row = sheet.createRow(0); row.createCell(0).setCellValue("ID"); row.createCell(1).setCellValue("学号"); row.createCell(2).setCellValue("姓名"); row.createCell(3).setCellValue("性别"); row.createCell(4).setCellValue("出生日期"); row.createCell(5).setCellValue("地址"); row.createCell(6).setCellValue("电话"); row.createCell(7).setCellValue("照片"); row.createCell(8).setCellValue("备注"); row.createCell(9).setCellValue("所属宿舍"); // 填充数据 for (int i = 0; i < studentList.size(); i++) { row = sheet.createRow(i + 1); row.createCell(0).setCellValue(studentList.get(i).getId()); row.createCell(1).setCellValue(studentList.get(i).getStuNo()); row.createCell(2).setCellValue(studentList.get(i).getStuName()); row.createCell(3).setCellValue(studentList.get(i).getStuSex()); row.createCell(4).setCellValue(studentList.get(i).getStuBirth()); row.createCell(5).setCellValue(studentList.get(i).getStuAddress()); row.createCell(6).setCellValue(studentList.get(i).getStuPhone()); row.createCell(7).setCellValue(studentList.get(i).getStuPhoto()); row.createCell(8).setCellValue(studentList.get(i).getStuRemark()); row.createCell(9).setCellValue(studentList.get(i).getStuDormitory()); } // 设置响应头信息 response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=student.xls"); // 输出Excel文件 workbook.write(response.getOutputStream()); workbook.close(); } ``` 3. 在前端页面中添加导出Excel的按钮,并绑定到controller中的导出方法上。 ``` <a href="/exportExcel" class="btn btn-primary">导出Excel</a> ``` 以上就是基于SSM框架导出Excel的一种实现方式,通过POI库构造Excel文件,并通过HttpServletResponse对象输出Excel文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值