您的位置:首页 > 编程语言 > Java开发

java导出excel例子

2015-07-08 20:34 411 查看
首先,需要引入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);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: