java导出excel例子

首先,需要引入jxl.jar 


import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
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.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WriteException;


public class ExcelColumn extends HttpServlet {

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

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doExport(request, response);
}

public void doExport(HttpServletRequest request,
HttpServletResponse response) throws UnsupportedEncodingException,
ServletException, IOException {
response.setContentType("APPLICATION/OCTET-STREAM");
byte[] writeInfo = null;
response.setHeader("Content-disposition", "attachment; filename="+new String("导出学生信息".getBytes("gb2312"), "iso8859-1")+".xls");
writeInfo=export(request);
ServletOutputStream out = response.getOutputStream();
out.write(writeInfo);
out.flush();
out.close();
}

// 导出Excel
public byte[] export(HttpServletRequest request){
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
jxl.write.WritableWorkbook workbook = null;
byte[] writeInfo = null;
// 增加sheet名
WritableSheet ws = null;
try {
workbook = Workbook.createWorkbook(baos);
// 第一行格式大标题
WritableCellFormat title = new WritableCellFormat(getBoldFont(14));
title.setAlignment(jxl.format.Alignment.CENTRE);
title.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
title.setBorder(Border.ALL, BorderLineStyle.NONE);
// 普通单元格格式
WritableCellFormat cellformate = new WritableCellFormat(getFont(9));
cellformate.setWrap(true);
cellformate
.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
cellformate.setBorder(Border.ALL, BorderLineStyle.THIN);
ws = workbook.createSheet("Sheet1", 0);
ws.setColumnView(0, 12);
ws.setColumnView(1, 15);
ws.setColumnView(2, 20);
ws.setColumnView(3, 15);
ws.setColumnView(4, 15);
ws.setColumnView(5, 15);
ws.setColumnView(6, 20);
ws.setColumnView(7, 15);
ws.setColumnView(8, 15);
ws.mergeCells(0, 0, 8, 0);
Label label = new Label(0, 0, "导出学生信息", title);//列、行、内容、格式
ws.addCell(label);


int ts = 100;
ws.addCell(new Label(0, 2, "总条数:"+ts,
cellformate));
int n = 3;
ws.addCell(new Label(0, n, "学员ID",
cellformate));
ws.addCell(new Label(1, n, "所属工县",
cellformate));
ws.addCell(new Label(2, n, "单位名称",
cellformate));
ws.addCell(new Label(3, n, "学号",
cellformate));
ws.addCell(new Label(4, n, "姓名",
cellformate));
ws.addCell(new Label(5, n, "科室",
cellformate));
ws.addCell(new Label(6, n, "身份证号码",
cellformate));
ws.addCell(new Label(7, n, "联系电话",
cellformate));
ws.addCell(new Label(8, n, "一卡通号",
cellformate));
n++;
while(n<=100){
ws.addCell(new Number(0, n, n,
cellformate));
ws.addCell(new Label(1, n, n+"",
cellformate));
ws.addCell(new Label(2, n, n+"",
cellformate));
ws.addCell(new Label(3, n, n+"",
cellformate));
ws.addCell(new Label(4, n, n+"",
cellformate));
ws.addCell(new Label(5, n, n+"",
cellformate));
ws.addCell(new Label(6, n, n+"",
cellformate));
ws.addCell(new Label(7, n, n+"",
cellformate));
ws.addCell(new Label(8, n, n+"",
cellformate));
n++;
}
workbook.write();
workbook.close();
writeInfo = baos.toByteArray();
baos.flush();
baos.close();
baos = null;
workbook = null;
ws = null;
} catch (WriteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(baos!=null){
baos.close();
baos =null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
return writeInfo;
}
public WritableFont getBoldFont(int i) {
return new WritableFont(WritableFont.createFont("宋体"), i,
WritableFont.BOLD);
}
public WritableFont getFont(int i) {
return new WritableFont(WritableFont.createFont("宋体"), i,
WritableFont.NO_BOLD);

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值