jsp页面数据导出

1、jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
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>
    <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.8.3.js"></script>
     <script type="text/javascript">
         $(function(){
             $("#queryByName").click(function(){
                 var name=$(this).prev().val();
                 location.href="${pageContext.request.contextPath}/employee/queryByName?name="+name;
             });
             $("#import").click(function(){
                 var array=$("input[type=checkbox]:checked");
                 if(array.length==0){
                     alert("请选择您要导出的数据...");
                 }else{
                     var ids=new Array();
                     array.each(function(){
                         ids.push($(this).parent().next().text());
                     });
                     $.post("${pageContext.request.contextPath}/employee/queryByIds","ids="+ids,function(d){
                         if(d=="ok"){
                             alert("数据导入成功!");
                         }else{
                             alert("数据导入失败!");
                         }
                         location.reload();
                     },"json");
                 }
             });
         });
     </script>
  </head>

  <body>
      <div>
          姓名:<input type="text" name="name"/><input type="button" value="查询" id="queryByName"/>
          &nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="导出" id="import"/>
      </div>
    <table border="1" width="80%">
        <tr>
            <th>选择</th>
            <th>序号</th>
            <th>姓名</th>
            <th>职位</th>
            <th>薪资</th>
            <th>部门</th>
        </tr>
        <c:forEach items="${requestScope.list }" var="e">
            <tr>
                <td><input type="checkbox"></td>
                <td>${e.id }</td>
                <td>${e.name }</td>
                <td>${e.position }</td>
                <td>${e.salary }</td>
                <td>${e.department.name }</td>
            </tr>
        </c:forEach>
    </table>
  </body>
</html>

 

 

 2、controllerl类

@RequestMapping("queryByIds")
    @ResponseBody
    public String queryByName(Model model,String[] ids) throws Exception{
        FileOutputStream outputStream =null;
        OutputStreamWriter writer=null;
        BufferedWriter writer2=null;
        try {
            int[] idss=new int[ids.length];
            for(int i=0;i<ids.length;i++){
                idss[i]=Integer.parseInt(ids[i]);
            }
            List<Employee> list = es.queryByIds(idss);
            outputStream = new FileOutputStream(new File("D://employees.txt"));
            writer=new OutputStreamWriter(outputStream, "utf-8");
            writer2 = new BufferedWriter(writer);
            for (Employee employee : list) {
                writer2.write(employee.getId()+"#"+employee.getName()+"#"+employee.getPosition()+"#"+employee.getSalary()+"#"+employee.getDepartment().getName());
                writer2.newLine();
            }
            return "ok";
        } catch (Exception e) {
            return "error";
        }finally{
            writer2.close();
            writer.close();
            outputStream.close();
        }
    }
}

转载于:https://www.cnblogs.com/Xuesk/p/7097313.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值