现在的位置: 首页 > 综合 > 正文

shh中导出excel(使用poi)

2012年02月07日 ⁄ 综合 ⁄ 共 6231字 ⁄ 字号 评论关闭

1.在struts.xml

<action name="exportExcelAction" class="com.ht.action.exam.ExamResultAction">      

    <result name="success" type="stream">    <!-- type="stream"表示要以二进制的文件去进行下载 -->      

      <param name="contentType">application/vnd.ms-excel</param>    <!-- 指明文件的类型 -->      

      <param name="contentDisposition">attachment;filename="grade_analysis.xls"</param>

          <!--attachment属性是要选择打开时单独打开,而不是在浏览器中打开,默认是inline. 指明下载时文件的名字 -->      

      <param name="inputName">exportResult</param><!-- 指明文件下载的方法的名字,这里用特别注意必须与Action中的方法名相同 -->    

  </result>      

 </action>

 

 

2.在action

public InputStream getExportResult() throws UnsupportedEncodingException

{            //这个get方法名必须与struts.xml中指定的文件下载方法名对应   

    claId = java.net.URLDecoder.decode(claId, "UTF-8");     //为了解决js传值的乱码问题,进行解码   

    str = java.net.URLDecoder.decode(str, "UTF-8");   

    return this.resultService.getInputStream(claId,str);

 }

 

 

3.在service

//实现excel的导出  

public InputStream getInputStream(String claId,String str);//这是定义输入流的一个方法

 

 

4.在service 实现层

 /*   * 对excel导出做处理   * */  

public InputStream getInputStream(String claId,String str) {   

  //System.out.println("班级"+claId+"  字符串"+str);   //先处理传过来的字符串  

   String[] type=str.split("-");  

   int len =type.length;   

  //System.out.println("数组长度为:"+len);   

  HSSFWorkbook wb=new HSSFWorkbook();//使用poi,导入poi的包,首先要生成一个HSSFWorkbook对象   

  HSSFSheet sheet=wb.createSheet("sheet1");//创建excel的sheet      

  HSSFRow row=sheet.createRow(0);//创建第一行      

  HSSFCell cell=row.createCell((short)0);//创建第一个单元格   

  cell.setEncoding(HSSFCell.ENCODING_UTF_16);//每个单元格都要设置编码格式   

  cell.setCellValue("序号");   

  for(int i=0;i<len;i++)

  {    

    cell=row.createCell((short)(i+1));//依次创建单元格    

    cell.setEncoding(HSSFCell.ENCODING_UTF_16);//每个单元格都要设置编码格式    

    cell.setCellValue(type[i]);   }   

/*到此表头已经设计好了   

*  

* 下面是读取数据库中的数据

*/  

 String queryString="from Result as res where res.student.classes.claId="+claId;  

 List<Result> res=null;   

res=resultDAO.getListByHQL(queryString);  

 //将符合要求的成绩记录存储到List集合中   

for(int i=0;i<res.size();i++)

{   

 Result result=res.get(i); //获得一条记录   

 row=sheet.createRow(i+1);//产生下面的其他行   

 cell=row.createCell((short)0);//第一个单元格    

cell.setEncoding(HSSFCell.ENCODING_UTF_16);    

cell.setCellValue(i+1);       

 for(int a=0;a<len;a++){  

 //后面的单元格依次显示要显示的内容     

//System.out.println("现在是谁"+type[a]);  

   cell=row.createCell((short)(a+1));     

  cell.setEncoding(HSSFCell.ENCODING_UTF_16);     

if(type[a].equals("考生学号"))

{      

  cell.setCellValue(result.getStudent().getStuNum());    

 }

else if(type[a].equals("考生姓名"))

{      

  cell.setCellValue(result.getStudent().getStuName());     

}

else if(type[a].equals("考生姓名"))

{      

  cell.setCellValue(result.getStudent().getStuName());     

}

else if(type[a].equals("考生班级"))

{      

  cell.setCellValue(result.getStudent().getClasses().getClaName());    

 }

else if(type[a].equals("考生性别"))

{      

  cell.setCellValue(result.getStudent().getStuSex());     

}

else if(type[a].equals("考试得分"))

{      

  cell.setCellValue(result.getResScore());     

}

else if(type[a].equals("测试时间"))

{      

  cell.setCellValue(result.getResBegin());     

}

else if(type[a].equals("交卷时间"))

{     

   cell.setCellValue(result.getResEnd());    

 }

else if(type[a].equals("考试用时"))

{      

try {       

  cell.setCellValue(getTimeDifference(result.getResEnd(),result.getResBegin()));      

} catch (Exception e) {       

  e.printStackTrace();      

}     

}    

}  

 }   

/*   

* 以上就生成了所需的excel文件   

*下面写另外一个方法直接不产生临时文件,就在内存中   

*/   

ByteArrayOutputStream os=new ByteArrayOutputStream();  

 try {    

  wb.write(os);  

 } catch (IOException e) {    

  e.printStackTrace();   

}   

byte[] content=os.toByteArray();   

  InputStream is=new ByteArrayInputStream(content);   return is;   

  }

 

 

5.resultDAO

  

public List<Result> getListByHQL(String queryString);

 

 

6.DAO的实现层

public List<Result> getListByHQL(String queryString)

{   

  return getHibernateTemplate().find(queryString);

 }

 

 

7.resullt 的实体类

  

package com.ht.entity;

/**  

* Result entity. @author MyEclipse Persistence Tools

 */

public class Result implements java.io.Serializable {

 // Fields

 private Integer resId;  

private Student student;  

private ClassTest classTest;  

private String resBegin;  

private String resEnd;  

private String resScore;

 // Constructors

 /** default constructor */  

public Result() {  }

 /** full constructor */  

public Result(Student student, ClassTest classTest, String resBegin,    String resEnd, String resScore)

{

    this.student = student;  

   this.classTest = classTest;   

   this.resBegin = resBegin;   

   this.resEnd = resEnd;  

   this.resScore = resScore;  

}

 // Property accessors

 public Integer getResId() {   return this.resId;  }

 public void setResId(Integer resId) {   this.resId = resId;  }

 public Student getStudent() {   return this.student;  }

 public void setStudent(Student student) {   this.student = student;  }

 public ClassTest getClassTest() {   return this.classTest;  }

 public void setClassTest(ClassTest classTest) {   this.classTest = classTest;  }

 public String getResBegin() {   return this.resBegin;  }

 public void setResBegin(String resBegin) {   this.resBegin = resBegin;  }

 public String getResEnd() {   return this.resEnd;  }

 public void setResEnd(String resEnd) {   this.resEnd = resEnd;  }

 public String getResScore() {   return this.resScore;  }

 public void setResScore(String resScore) {   this.resScore = resScore;  }

}

 

 

 

 

 8.表现层

<html>

<head><title>导出excel</title><head>

<body>

<p style="text-align:center; padding-top:15px">  

选择班级:

 <select name="claId" id="claId" style="color:#999999">   

  <option value="0" style="color:#999999">==选择==</option>   

     <option value=1 style="color:#000000">药学1班</option>  

    <option value=2 style="color:#000000">药学2班</option>   

</select>

</p>

<p></p>

<p style="text-align:center; padding-top:15px">  选择需要导出的选项:</br>

   <input type="checkbox" name="stu" id="a" value="考生学号">考生学号&nbsp;&nbsp;  

  <input type="checkbox" name="stu" id="a" value="考生姓名">考生姓名</br>  

  <input type="checkbox" name="stu" id="a" value="考生班级">考生班级&nbsp;&nbsp;  

  <input type="checkbox" name="stu" id="a" value="考生性别">考生性别</br>  

  <input type="checkbox" name="stu" id="a" value="考试得分">考试得分&nbsp;&nbsp;

   <input type="checkbox" name="stu" id="a" value="测试时间">测试时间</br>  

  <input type="checkbox" name="stu" id="a" value="交卷时间">交卷时间&nbsp;&nbsp;  

  <input type="checkbox" name="stu" id="a" value="考试用时">考试用时</br>

</p>

 <p style="text-align:center; padding-top:15px">   

  <input type="button" value="导出" onclick="exportExcel('exportExcelAction.action');"/>

</p>

 </body>

</html>

 

 

 6.这里还用到了js进行传值,根据你的需要可以进行修改

js:

 //导出excel操作,不能使用ajax

 function exportExcel(tagAction)

{  

  var claId=$("#claId").val();  

  var input=$("input[name='stu']");//先得到所有的值,通过循环来判断  

  var str="";  

for(i=0;i<input.length;i++)

{     

  if(input[i].checked)

  {       

  str=str+input[i].value+"-";    //这里将选定的项连接成一个字符串来传给后台

    }  

}

   if(claId=="0")

{   

  alert("请先选择班级!");  

   return;  

}

 if(str==null||str=="")

  {  

 alert("请先选择要导出的项!");

  return;  

}  

//进行乱码处理,进行两次encodeURI包装  

window.location.href="exportExcelAction.action?claId="+encodeURI(encodeURI(claId))+"&str="+encodeURI(encodeURI(str))+"";

 }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

【上篇】
【下篇】

抱歉!评论已关闭.