jxls操作Excel表的导出,一个简单的例子

student  对应的javabean

 

package bean;

public class Student {
 private String studentId;
 private String studentNo;
 private String name;
 private String sex;
 private String score;
 
 public String getScore() {
  return score;
 }
 public void setScore(String score) {
  this.score = score;
 }
 public String getStudentId() {
  return studentId;
 }
 public void setStudentId(String studentId) {
  this.studentId = studentId;
 }
 public String getStudentNo() {
  return studentNo;
 }
 public void setStudentNo(String studentNo) {
  this.studentNo = studentNo;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getSex() {
  return sex;
 }
 public void setSex(String sex) {
  this.sex = sex;
 }
 @Override
 public String toString() {
  StringBuffer sb = new StringBuffer();
  sb.append(" Name:" + getName());
  sb.append(" Score:" + getScore());
  sb.append(" Sex:" + getSex());
  sb.append(" Id:" + getStudentId());
  sb.append(" No:" + getStudentNo());
  return sb.toString();
 }
 
}

 

package servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jxls.transformer.XLSTransformer;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import bean.Student;

@SuppressWarnings("serial")
public class OperateExcel extends HttpServlet {

 /**
  * Constructor of the object.
  */
 public OperateExcel() {
  super();
 }

 /**
  * The doGet method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to get.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  this.doPost(request, response);
 }

 /**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to post.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
       
  
  //export
  request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
  
        response.setContentType("application/download;charset=utf-8");

        response.setHeader("Content-disposition", "attachment;filename=/"1.xls/"");
        OutputStream os = response.getOutputStream();
       
  String url = request.getRealPath("/");
  String srcUrl = url + "template/achievement.xls";
  File f = new File(srcUrl);
  String targetUrl = url + "template/temp.xls";
  File tempFile = new File(targetUrl);
  
  XLSTransformer transformer = new XLSTransformer();
  
  Map<String,Object> params = new HashMap<String,Object>();
  params.put("classId", "123456789");
  params.put("className", "中华人民四");
  params.put("subjectId", "987654321");
  params.put("subjectName", "语文");
  List<Student> sList = new ArrayList<Student>();
  Student s = null;
  for(int i = 0 ;i < 3 ;i++){
   s = new Student();
   s.setName("Name" + i);
   s.setSex("男人");
   s.setStudentId(String.valueOf(i));
   s.setStudentNo("s" + i);
   s.setScore(String.valueOf((int)(Math.random()*100)));
   sList.add(s);
  }
  params.put("records", sList);
  transformer.transformXLS(srcUrl, params, targetUrl);
  
  FileInputStream fis = new FileInputStream(tempFile);
  
  //XLSTransformer
  
  byte[] b = new byte[1024];
  while(fis.read(b) != -1){
   os.write(b);
  }

  os.flush();
       
        fis.close();
        os.close();
        //tempFile.delete();
  
       
        //import
        try {
   Workbook wb = WorkbookFactory.create(new FileInputStream(tempFile));
   Sheet sheet = (Sheet) wb.getSheetAt(0);
   int startIndex = 4;
   Student ss = null;
   List<Student> list = new ArrayList<Student>();
   for(int i = 0;i < 3;i++){
    ss = new Student();
    Row row = sheet.getRow(startIndex + i);
    int index = (int)row.getCell(0).getNumericCellValue();
    String studentNo = row.getCell(1).getStringCellValue();
    String name = row.getCell(2).getStringCellValue();
    String sex = row.getCell(3).getStringCellValue();
    String score = row.getCell(4).getStringCellValue();
    String studentId = row.getCell(6).getStringCellValue();
    
    ss.setName(name);
    ss.setScore(score);
    ss.setSex(sex);
    ss.setStudentId(studentId);
    ss.setStudentNo(studentNo);
    list.add(ss);
   }
   
   
   Iterator<Student> iii = list.iterator();
   while(iii.hasNext()){
    Student te = iii.next();
    System.out.println(te.toString());
   }
   
  } catch (InvalidFormatException e) {
   e.printStackTrace();
  }
        
 }

}

 

 

要导入的包:

 

jxls操作Excel表的导出,一个简单的例子_第1张图片

 

 

 

样板Excel文档

序号 学号 学生姓名 性别 成绩 成绩波动性质 学生编号
<jx:forEach items="${records}" var="student" varStatus="vs" >
${vs.index + 1} ${student.studentNo} ${student.name} ${student.sex} ${student.score} ${student.studentId}
</jx:forEach>            

 

 

 

 

 

你可能感兴趣的:(String,server,File,Excel,equals,Constructor)