java导出excel例子

转载 2015年07月08日 20:34:49
首先,需要引入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);

}
}

java 导出excel例子,使用poi

  • 2014年05月02日 07:48
  • 5KB
  • 下载

Java Web利用POI导出Excel简单例子

采用Spring mvc架构:  Controller层代码如下  [java] view plain copy @Controller     p...
  • K_Men
  • K_Men
  • 2016年12月26日 17:52
  • 3679

java基于jxl 导出excel例子

这是一个导入Excel的Java代码,很简单的小例子。适合我这种新手
  • LTNGM
  • LTNGM
  • 2017年04月18日 19:49
  • 139

Java Web利用POI导出Excel简单例子

采用Spring mvc架构:  Controller层代码如下  @Controller public class StudentExportController{ ...
  • ptzrbin
  • ptzrbin
  • 2013年04月02日 14:28
  • 55701

java 导出excel到客户端(本地)例子 用poi和servlet实现的

最近做java的web项目,用到了excel导出到客户端(本地),通过IE可以实现用浏览器保存、另存为、取消。我使用的是POI来完成的,poi架包是poi-3.7-20101029.jar这个架包在网...

Java Web利用POI导出Excel简单例子

采用Spring mvc架构:  Controller层代码如下  Java代码   @Controller   public class StudentExportCon...

java中jxl导出数据到excel的例子

要往xls文件里面写入数据的时候需要注意的是第一要新建一个xls文件 outputstream os=new fileoutputstream("c:\\excel2.xls" ); 再建完这个文...

java中jxl导出数据到excel的例子

要往xls文件里面写入数据的时候需要注意的是第一要新建一个xls文件  OutputStream os=new FileOutputStream("c:\\excel2.xls" );  再建...

Java Web利用POI导出Excel例子(采用Spring mvc架构)

采用Spring mvc架构:  Controller层代码如下  [java] view plaincopy @Controller     public class St...

java 导出excel到客户端(本地)例子 用poi和servlet实现的

http://m.blog.csdn.net/article/details?id=49635837 web.xml
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:java导出excel例子
举报原因:
原因补充:

(最多只允许输入30个字)