JAva jxlによるexcelテーブルのエクスポート

6537 ワード

詳細
JAva jxlによるexcelテーブルのエクスポート
あまり言わないで、直接コードをつけます
1、エンティティークラスStudent
package com.model;

public class Student {
	
	private int id;
	private String userName;
	private int age;
	
	public Student() {
	}
	public Student(int id, String userName, int age) {
		this.id = id;
		this.userName = userName;
		this.age = age;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Student [age=" + age + ", id=" + id + ", userName=" + userName
				+ "]";
	}

}

 2、データアクセス対象クラス(Listアナログデータベースを採用)StudentDao
package com.dao;

import java.util.ArrayList;
import java.util.List;

import com.model.Student;
/**
 * @author kangkang.tao
 */
public class StudentDao {
	
	public final static List allStudents = new ArrayList();
	//      ,      
	static{
		allStudents.add(new Student(1,"  ",23));
		allStudents.add(new Student(2,"  ",12));
		allStudents.add(new Student(3,"  ",15));
		allStudents.add(new Student(4,"gina",16));
		allStudents.add(new Student(5,"jim",10));
		allStudents.add(new Student(6,"tamy",14));
		allStudents.add(new Student(7,"anni",20));
		allStudents.add(new Student(8,"pile",10));
		allStudents.add(new Student(9,"amy",23));
		allStudents.add(new Student(10,"temy",22));
		allStudents.add(new Student(11,"kill",15));
		allStudents.add(new Student(12,"jane",16));
		allStudents.add(new Student(13,"kang",17));
	}
	
	//     
	public List getAll(){
		return allStudents;
	}
}

 3、servlet
package com.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

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

import jxl.Workbook;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import com.dao.StudentDao;
import com.model.Student;


public class ExportServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private StudentDao dao = new StudentDao();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		OutputStream os = response.getOutputStream();//         
        response.reset();//        

        response.setHeader("Content-disposition", "attachment; filename=studentList.xls");//           
        response.setContentType("application/msexcel");//        
        
        
		WritableWorkbook book = null;
		try {
			 //     
            book = Workbook.createWorkbook(os);
            
            WritableCellFormat  wcf = new WritableCellFormat(); 
            wcf.setAlignment(jxl.format.Alignment.CENTRE);
            wcf.setBorder(Border.ALL, BorderLineStyle.THIN);
            wcf.setBackground(Colour.GREY_25_PERCENT);
            
            //     "  "    ,  0       
            WritableSheet sheet = book.createSheet("       ", 0);
            //          
            List studentList = dao.getAll();
            Label head1 = new Label(0, 0, "  ");
            head1.setCellFormat(wcf);
            sheet.addCell(head1);
            Label head2 = new Label(1, 0, "  ");
            head1.setCellFormat(wcf);
            sheet.addCell(head2);
            Label head3 = new Label(2, 0, "  ");
            head1.setCellFormat(wcf);
            sheet.addCell(head3);
            
            if(studentList != null && !studentList.isEmpty()){
            	for (int i = 0; i < studentList.size(); i++) {
					sheet.addCell(new Label(0, i+1, String.valueOf(studentList.get(i).getId())));
					sheet.addCell(new Label(1, i+1, studentList.get(i).getUserName()));
					sheet.addCell(new Label(2, i+1, String.valueOf(studentList.get(i).getAge())));
				}
            }
            //          
            book.write();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(book!=null){
                try {
                    book.close();
                } catch (Exception e) {
                    e.printStackTrace();
                } 
			}
		}
 	}
}

 4、web.xml
   
  
  	exportServlet
  	com.servlet.ExportServlet
  
  
  	exportServlet
	/export.do
  

 5、jsp





Insert title here


 EXCELのエクスポート


 
 
  • JXL.7z (441 KB)
  • ダウンロード回数:0