当前位置: 代码迷 >> Web前端 >> jxl 导出替Excel文件
  详细解决方案

jxl 导出替Excel文件

热度:288   发布时间:2012-08-25 10:06:20.0
jxl 导出为Excel文件
最近几天在做Excel导入导出的练习,今天刚好做了导出为Excel文件的模块,有好多还不是很理解,好多的是参照别人的代码。
1.首先导入相应的jar包,这里就不多说了,网上很容易就可以找的到;
2.自己写了一个导出类:
package utils;

import java.io.IOException;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import org.aspectj.weaver.NewConstructorTypeMunger;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import EmpExcel.model.EmployeeInfo;

public class ExportEmployee {
	
	public static void ExportEmployee(OutputStream os, List<EmployeeInfo> employeeInfos) {
		 
        try {
        	//打开文件
			WritableWorkbook book  =  Workbook.createWorkbook(os);
			//生成名为“员工信息”的工作表,参数0表示这是第一页
            WritableSheet sheet  =  book.createSheet("员工信息",0 );
            //第一行为标题行
            Label label00 = new Label(0, 0, "员工编号");
            Label label10 = new Label(1, 0, "员工姓名");
            Label label20 = new Label(2, 0, "员工性别");
            Label label30 = new Label(3, 0, "国籍");
            Label label40 = new Label(4, 0, "身份证号");
            sheet.addCell(label00);
            sheet.addCell(label10);
            sheet.addCell(label20);
            sheet.addCell(label30);
            sheet.addCell(label40);
            //循环导出数据到excel的label中
			for (int i = 1; i < employeeInfos.size(); i++) {
				EmployeeInfo employeeInfo = employeeInfos.get(i);
				Label label0i = new Label(0, i, employeeInfo.getEmployeeNumber());
				Label label1i = new Label(1, i, employeeInfo.getFullName());
				Label label2i = new Label(2, i, employeeInfo.getSex());
				Label label3i = new Label(3, i, employeeInfo.getNationality());
				/*if(employeeInfo.getDateOfBirth() != null){
					DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
					label3i = new Label(3, i, format1.format(employeeInfo.getDateOfBirth()));
				}*/
				Label label4i = new Label(4, i, employeeInfo.getEmployeeNumber());
				sheet.addCell(label0i);
	            sheet.addCell(label1i);
	            sheet.addCell(label2i);
	            sheet.addCell(label3i);
	            sheet.addCell(label4i);
			}
			//写入数据并关闭文件
			book.write();
			book.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		}
	}
}

3.在action中调用以上写的导出类中的导出方法:
String reportName = "员工信息";
		response.setContentType("application/msexcel;charset=UTF-8");   
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Content-Disposition", 
				"attachment;filename="+java.net.URLEncoder.encode(reportName+".xls", 
				"UTF-8"));  
		response.addHeader("Pargam", "no-cache");  
		response.addHeader("Cache-Control", "no-cache"); 
		
		List<EmployeeInfo> employeeInfos = this.searchEmployeeInfos(request,response);
		
		OutputStream os = response.getOutputStream();
		//调用导出Excel的方法
		ExportEmployee.ExportEmployee(os, employeeInfos);
		
		response.getOutputStream().flush();
		response.getOutputStream().close();

以上就是导出为Excel的主要方法,里面自己有好多不是很理解还有待加强,但基本功能已经实现。
  相关解决方案