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和servlet实现的

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

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

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

导出为Excel例子 java

前台: //getExcelXML有一个JSON对象的配置,配置项看了下只有title配置,为excel文档的标题 var data = $('#dg').datagrid('getExcelXml...

java excel 导出例子

Excel导出学习之道:Java Web利用POI导出Excel简单例子

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

最近看了点java对excel表的操作,没事写了个例子练习了一下

在这个例子中我使用了一个开源的插件jxl。 初次使用,还有很多不足之处,恳请大家的指教。代码如下: /** * */ package cn.wyh.IO; import java.io.File; /...

Java POI读取excel的例子

此例子支持xls、xlsx格式(即:2003、2010),使用poi的jar版本是3.8 导入excel内容: 导出excel模板: 1、TextExcel.java package co...

Java 使用poi导入excel,结合xml文件进行数据验证的例子(增加了jar包)

假设现在要做一个通用的导入方法: 要求: 1.xml的只定义数据库表中的column字段,字段类型,是否非空等条件。 2.excel定义成模板,里面只填写了所需要的数据,有可能数据有问题。 ...

通过java调用VBS,再用VBS执行Excel中的宏的例子

http://blog.sina.com.cn/s/blog_58b9cb3a01007kdt.html   (一)  首先需要降低客户端及服务器端对于Excel的宏的安全级别的要求,按照下述的图...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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