excel 导出(csv)

<!
response.setContentType ( "application/ms-excel;charset=GBK");
    response.setHeader ( "Content-Disposition" ,"attachment;filename="+new String("Address.csv".getBytes(),"iso-8859-1")) ;
// turn(String str) 处理回车换行, \t 处理数字

!>

<%@ page language="java" contentType="text/html;charset=GBK"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.sql.*"%>


<%!

String trimStr(String str){
    if(str==null){
      return "";
   }else{
     return turn(str);
   }
}


String turn(String str){
//下面的代码将字符串以正确方式显示(包括回车,换行,空格)
while(str.indexOf("\r\n")!=-1){
str = str.substring(0,str.indexOf("\r\n"))+" "+str.substring(str.indexOf("\r\n")+1);
}
while(str.indexOf("\n")!=-1){
str = str.substring(0,str.indexOf("\n"))+" "+str.substring(str.indexOf("\n")+1);
}
while(str.indexOf("\r")!=-1){
str = str.substring(0,str.indexOf("\r"))+" "+str.substring(str.indexOf("\r")+1);
}
return str;
}
%>
<%
       
// String examId=request.getParameter("examtypename"); 
//String userId=request.getParameter("danwei");

String examId=request.getParameter("examtypename"); 
String userId=request.getParameter("danwei");
String depname1="";
Connection conn1=null;
PreparedStatement stat1=null;
ResultSet rs1 = null;
String sql1="select * from wsexamuser where userid=?";
  try
            {
                conn1 = ConnectionProvider.getConnection();
                stat1 = conn1.prepareStatement(sql1);
                stat1.setString(1,userId);
                rs1 = stat1.executeQuery();
                 while(rs1.next()){
                    depname1=trimStr(rs1.getString("username"));
                 }
             }catch(Exception ex){
                System.out.println("登陆错误!"+ex);
                try{
                    conn1.rollback();
                }catch(Exception e){System.out.println("登陆错误!"+ex);}
            }finally{
                ConnectionProvider.close(conn1,stat1,rs1);
            }







//System.out.println("examId: "+ examId +"  userId:"+userId);
if(userId!=null&&examId!=null){
     String sql = "select e.* ,l.username lusername,l.examprof lexamprof,l.examjob lexamjob,"+" l.examnum lexamnum,l.examedu lexamedu,l.examdegree lexamdegree,l.examlang lexamlang"+" from wsexam e left join wsexamlist l on l.examlistid = e.examlistid "+
"where e.examtypeid= ? and e.examdepid=? order by e.examname";
response.setContentType ( "application/ms-excel;charset=GBK");
    response.setHeader ( "Content-Disposition" ,"attachment;filename="+new String((depname1+".csv").getBytes(),"iso-8859-1")) ;
//设置文件头
out.println("序号 ,"+"姓名,"+"报考单位,"+"报考岗位,"+"审核状态,"+"身份证,"+"政治面貌,"+"民族,"+"性别,"
+"是否省优毕业生,"+"学历,"+"毕业时间,"+"毕业院校,"+"学位,"+"英语等级,"+"专业,"+"联系地址,"+"固定电话,"+"移动电话,"+"在校荣获奖励,"+"个人简历,"+"报考单位审核意见,"+"备注,"+"转岗说明,");
              // System.out.println(sql);              
               Connection conn=null;
   PreparedStatement stat=null;
   ResultSet rs = null;
               try
            {
                conn = ConnectionProvider.getConnection();
                stat = conn.prepareStatement(sql);
                stat.setString(1,examId);
stat.setString(2,userId);
                rs = stat.executeQuery();
                int index = 1;
                while(rs.next()){
                out.println(index+","
                         +trimStr(rs.getString("examname"))+","
+trimStr(rs.getString("examdep"))+","
+trimStr(rs.getString("examjob"))+","
                         +trimStr(rs.getString("examshzt"))+","
+" "+trimStr(rs.getString("examsfz"))+" "+"\t ,"// "\t"处理数字,以防到excel变成科学计数
+trimStr(rs.getString("examzzmm"))+","
+trimStr(rs.getString("exammz"))+","
+trimStr(rs.getString("examxb"))+","
+trimStr(rs.getString("examsybys"))+","
+trimStr(rs.getString("examxl"))+","
+trimStr(rs.getString("exambysj"))+","
+trimStr(rs.getString("exambyyx"))+","
+trimStr(rs.getString("examxw"))+","
+trimStr(rs.getString("examyydj"))+","
+trimStr(rs.getString("examzy"))+","
+trimStr(rs.getString("examlxdz"))+","
+trimStr(rs.getString("examphone"))+","
+trimStr(rs.getString("exammobile"))+","
+turn(trimStr(rs.getString("examhjqk")))+","
+turn(trimStr(rs.getString("examjl")))+","
+trimStr(rs.getString("examshyj"))+","
+trimStr(rs.getString("remark"))+","
+trimStr(rs.getString("examzg"))
                         );
                         index++;
               }
            }catch(Exception ex){
                System.out.println("登陆错误!"+ex);
                try{
                    conn.rollback();
                }catch(Exception e){System.out.println("登陆错误!"+ex);}
            }finally{
                ConnectionProvider.close(conn,stat,rs);
            }
}

%>

你可能感兴趣的:(sql,Excel)