jxl导出excel 文档

刚工作就碰到让转个excel 文档,当时贴了别人的.后来有时间赶紧自己照着别人写个,只要把别人的东西弄明白就是自己的了,

 哈哈! 看代码 ,这个功能是弹出提示框的,也解决了导出后的数据类型问题,还有 标题显示,并可以吧标题颜色做改变: 贴出来大家分享!

package com.resunly;

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

import javax.servlet.http.HttpServletResponse;

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

public class ExcelMethod {
	public void toExcelTest(List stulist, HttpServletResponse response){
		//创建工作流
		OutputStream os = null;
		
		//初始化工作表
		WritableWorkbook workbook = null;
		try {
			
			//设置弹出对话框
			response.setContentType("application/DOWLOAD");
			
			//设置工作表的标题
			response.setHeader("Content-Disposition", "attachment; filename=My Test TO Excel.xls");
			os = response.getOutputStream();
			
			//创建工作表
			workbook = Workbook.createWorkbook(os);
			
			//定义工作表 sheet 标题
			WritableSheet ws = workbook.createSheet("Mapping", 0);
			ws.getSettings().setShowGridLines(true);
			ws.getSettings().setProtected(false);
			
			//控制列的宽度,如果你要不给一样的宽度,就单独写,i代表的是列的下标,从0开始 ,从左到右
			for(int i=0;i<7;i++){
				ws.setColumnView(i, 20);
			}
			
			// 創建标题列名称
			Label titleLabel = null;
			titleLabel = new Label(0, 0, "StuNo", getHeadFormat());
			ws.addCell(titleLabel);
			titleLabel = new Label(1, 0, "StuName", getHeadFormat());
			ws.addCell(titleLabel);
			titleLabel = new Label(2, 0, "StuMoniter", getHeadFormat());
			ws.addCell(titleLabel);
			titleLabel = new Label(3, 0, "StuAddr", getHeadFormat());
			ws.addCell(titleLabel);
			titleLabel = new Label(4, 0, "Stu TELL", getHeadFormat());
			ws.addCell(titleLabel);
			titleLabel = new Label(5, 0, "Stu sex", getHeadFormat());
			ws.addCell(titleLabel);
			titleLabel = new Label(6, 0, "Stu classNo", getHeadFormat());
			ws.addCell(titleLabel);
			
			//循环添加对象数据
			for (int i = 0; i < stulist.size(); i++) {
				Stuinfo stuinfo =  (Stuinfo) stulist.get(i);
				String stuNo=stuinfo.getStuNo().toString();
				String stuName=stuinfo.getStuName().toString();
				String stuAddr=stuinfo.getAddr().toString();
				String stusex=stuinfo.getStuSex().toString();
				String stuminter=stuinfo.getMoniter().toString();
				String stuClassno=stuinfo.getClassNo();
				String stutell = stuinfo.getTell();

				Label stuNoLabel = new Label(0, i + 1, stuNo);
				Label stuNameLabel = new Label(1, i + 1, stuName);
				Label stuminterLabel = new Label(2, i +1, stuminter);
				Label stuAddrLable = new Label(3, i + 1, stuAddr);
				//Label stuTellLabel = new Label(4, i + 1, stutell); 如果用这个导出后会有数值和文本的区别
				jxl.write.Number tellNumber = new jxl.write.Number(4,i+1,Double.parseDouble(stutell));
				Label stusexLabel = new Label(5, i + 1,stusex );
				//Label classNoLabel = new Label(6, i + 1,stuClassno); 一下要注意了,必须要转成double类型的数据
				jxl.write.Number classNoNumber = new jxl.write.Number(6, i + 1,Double.parseDouble(stuClassno));
				
				ws.addCell(stuNoLabel);
				ws.addCell(stuNameLabel);
				ws.addCell(stuminterLabel);
				ws.addCell(stuAddrLable);
				ws.addCell(tellNumber);
				ws.addCell(stusexLabel);
				ws.addCell(classNoNumber);
			}
			
			workbook.write();
			workbook.close();
			os.close();
		} catch (Exception e) {
			System.out.println(e.getCause());
			System.out.println(e.getMessage());
		}
	}
	
	/**
	 * 设置单元格样式
	 * @return
	 * @throws Exception
	 */
	public static WritableCellFormat getHeadFormat() throws Exception {
		//设置字体
		WritableFont wf = new WritableFont(WritableFont.ARIAL, 8, WritableFont.BOLD);
		
		//创建单元格FORMAT
		WritableCellFormat wcf = new WritableCellFormat(wf);
		wcf.setAlignment(Alignment.CENTRE);                         
		wcf.setVerticalAlignment(VerticalAlignment.CENTRE);         
		wcf.setLocked(true);
		wcf.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
		wcf.setBackground(Colour.GREY_25_PERCENT);
		return wcf;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值