借助POI的excel接口,可以方便得实现excel导出功能:

首先需要引入poi对应的jar包

1.前端jsp页面需要一个a链接。

web页面文件MIM类型的下载,只需要一个a元素,该a可以链到该文件在服务器端的实体路径,也可以链接到一个servelt,将该文件写到response的输出流中。其他的下载浏览器会自动帮助完成。

<a type="button" class="btn btn-warning" href='/WebTest/ExportExcel' id='export'>导出</a>

2,该链接对应的后台处理servlet代码实现

package com.bobo.servlet;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import com.bobo.helper.ExcelHelper;
import com.bobo.modal.ShitiModal;
import com.bobo.modal.XixiangModal; public class ExportExcel extends HttpServlet { /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { process(request, response);
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { process(request, response);
} @SuppressWarnings("deprecation")
private void process(HttpServletRequest request,
HttpServletResponse response) throws UnsupportedEncodingException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8"); response.reset();
response.setContentType("application/x-msexcel"); String fileName = URLEncoder.encode("巡查实体列表", "utf-8");
response.addHeader("Content-Disposition", "attachment; filename="
+ new String(fileName.getBytes("utf-8"), "ISO8859-1") + ".xls");
HSSFWorkbook wb = new HSSFWorkbook();
// 表头数足
String[] title = { "实体编号", "实体名称", "省", "市", "巡查人", "巡查结果", "巡查时间",
"细项名称", "巡查人", "巡查结果", "巡查时间", "不合格原因" };
ArrayList<ShitiModal> content = new ArrayList<ShitiModal>();
for (int i = ; i < ; i++) {
ShitiModal shiti = new ShitiModal();
shiti.setShitiCity("杭州");
shiti.setShitiName("二龙路基站");
shiti.setShitiNum("");
shiti.setShitiPerson("张三");
shiti.setShitiProvince("浙江");
shiti.setShitiResult("不合格");
shiti.setShitiTime("2015年4月28日");
// 填充细分项
ArrayList<XixiangModal> xixiangList = new ArrayList<XixiangModal>();
for (int j = ; j < ; j++) {
XixiangModal xixiang = new XixiangModal();
xixiang.setXixiangMember("张三");
xixiang.setXixiangName("开关电源");
xixiang.setXixiangReason("管道线路井内余线没有靠墙");
xixiang.setXixiangResult("不合格");
xixiang.setXixiangTime("2015年4月28日");
xixiangList.add(xixiang);
}
shiti.setXixiangList(xixiangList);
content.add(shiti);
}
ExcelHelper eHelper = new ExcelHelper();
// 生成excel中的一张表格
HSSFSheet sheet = eHelper.export(wb, "sheet1", title, content); // 直接存到服务器端
// FileOutputStream fileOut;
// try {
// fileOut = new FileOutputStream("workbook.xls");
// wb.write(fileOut);
// fileOut.close();
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// 写入输出流
try {
OutputStream os = response.getOutputStream();
wb.write(os);
os.flush();
os.close(); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } }

3,相关实体类

package com.bobo.modal;

import java.util.ArrayList;

public class ShitiModal {
private String shitiNum;
private String shitiName;
private String shitiProvince;
private String shitiCity;
private String shitiPerson;
private String shitiTime;
private String shitiResult;
private ArrayList<XixiangModal> xixiangList;
public String getShitiPerson() {
return shitiPerson;
} public void setShitiPerson(String shitiPerson) {
this.shitiPerson = shitiPerson;
} public String getShitiTime() {
return shitiTime;
} public void setShitiTime(String shitiTime) {
this.shitiTime = shitiTime;
} public String getShitiResult() {
return shitiResult;
} public void setShitiResult(String shitiResult) {
this.shitiResult = shitiResult;
} public String getShitiNum() {
return shitiNum;
} public void setShitiNum(String shitiNum) {
this.shitiNum = shitiNum;
} public String getShitiName() {
return shitiName;
} public void setShitiName(String shitiName) {
this.shitiName = shitiName;
} public String getShitiProvince() {
return shitiProvince;
} public void setShitiProvince(String shitiProvince) {
this.shitiProvince = shitiProvince;
} public String getShitiCity() {
return shitiCity;
} public void setShitiCity(String shitiCity) {
this.shitiCity = shitiCity;
} public ArrayList<XixiangModal> getXixiangList() {
return xixiangList;
} public void setXixiangList(ArrayList<XixiangModal> xixiangList) {
this.xixiangList = xixiangList;
} }

ShitiModal

package com.bobo.modal;

public class XixiangModal {
private String XixiangName;
private String XixiangMember;
private String XixiangResult;
private String XixiangTime;
private String XixiangReason;
public String getXixiangName() {
return XixiangName;
}
public void setXixiangName(String xixiangName) {
XixiangName = xixiangName;
}
public String getXixiangMember() {
return XixiangMember;
}
public void setXixiangMember(String xixiangMember) {
XixiangMember = xixiangMember;
}
public String getXixiangResult() {
return XixiangResult;
}
public void setXixiangResult(String xixiangResult) {
XixiangResult = xixiangResult;
}
public String getXixiangTime() {
return XixiangTime;
}
public void setXixiangTime(String xixiangTime) {
XixiangTime = xixiangTime;
}
public String getXixiangReason() {
return XixiangReason;
}
public void setXixiangReason(String xixiangReason) {
XixiangReason = xixiangReason;
} }

XixiangModal

4,ExcelHelper类

package com.bobo.helper;

import java.util.ArrayList;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress; import com.bobo.modal.ShitiModal;
import com.bobo.modal.XixiangModal; public class ExcelHelper { public HSSFSheet export(HSSFWorkbook wb, String fileName, String[] title,
ArrayList<ShitiModal> content) {
// int cellNum = 5; //设置列数
/**
* 建立表格设置。
*/
HSSFSheet sheet = null;
try {
sheet = wb.createSheet(fileName); int columnSize = title.length;
HSSFRow row_one = sheet.createRow();
for (int i = ; i < columnSize; i++) {
HSSFCell cell = row_one.createCell(i);
cell.setCellValue(title[i]);
}
int curRow = ;
int endRow = ;
int temp = curRow;
for (int i = ; i < content.size(); i++) {
temp = curRow;
ShitiModal shiti = content.get(i);
int xixiangNum = content.get(i).getXixiangList().size();
endRow += xixiangNum;
for (; curRow < endRow; curRow++) {
// 设置需要合并的单元格部分
for (int k = ; k < ; k++) {
CellRangeAddress cra = new CellRangeAddress(curRow,
endRow - , k, k);
sheet.addMergedRegion(cra);
}
HSSFRow row = sheet.createRow(curRow);
if (curRow == temp) {
row.createCell().setCellValue(shiti.getShitiNum());
row.createCell().setCellValue(shiti.getShitiName());
row.createCell()
.setCellValue(shiti.getShitiProvince());
row.createCell().setCellValue(shiti.getShitiCity());
row.createCell().setCellValue(shiti.getShitiPerson());
row.createCell().setCellValue(shiti.getShitiResult());
row.createCell().setCellValue(shiti.getShitiTime());
}
// 开始添加细分
XixiangModal xixiang = shiti.getXixiangList().get(
curRow - temp);
row.createCell().setCellValue(xixiang.getXixiangName());
row.createCell().setCellValue(xixiang.getXixiangMember());
row.createCell().setCellValue(xixiang.getXixiangResult());
row.createCell().setCellValue(xixiang.getXixiangTime());
row.createCell().setCellValue(xixiang.getXixiangReason()); }
curRow = endRow;
} } catch (Exception ex) {
ex.printStackTrace();
}
return sheet;
} // end of export }

jsp导出Excel功能的实现的更多相关文章

  1. java、jsp导出excel功能备份

    问题踩坑: ajax请求不能下载文件 必须这样: <a href="/media">点击下载Excel</a> 或者 location.href = '/m ...

  2. JSP导出Excel后身份证后三位为0

    JSP导出Excel身份证号码超出Excel最大限制,用科学计数法表示,但后三位为0,修改方式: <style type="text/css">.txt    {    ...

  3. Atitit.导出excel功能的设计 与解决方案

    Atitit.导出excel功能的设计 与解决方案 1.1. 项目起源于背景1 1.2. Js  jquery方案(推荐)jquery.table2excel1 1.3. 服务器方案2 1.4. 详细 ...

  4. 项目笔记:导出Excel功能

    1.前台这块: var ids=""; $.post("${basePath}/assets/unRegDeviceAction_getDeviceIds.do" ...

  5. 项目笔记:导出Excel功能分sheet页插入数据

    导出Excel功能分sheet页处理数据: /*导出EXCEL*/ public void createExcel() { log.info("导出Excel功能已经启动-BEGIN&quo ...

  6. vue项目导出EXCEL功能

    因为一些原因导出EXCEL功能必须前端来做,所以就研究了一下,在网上也找了一些文章来看,有一些不完整,我做完了就记录下来,供大家参考: 1.首先先安装依赖: npm install file-save ...

  7. Vue通过Blob对象实现导出Excel功能

    不同的项目有不同的导出需求,有些只导出当前所显示结果页面的表格进入excel,这个时候就有很多插件,比如vue-json-excel或者是Blob.js+Export2Excel.js来实现导出Exc ...

  8. jsp导出excel

    很多时候,我们都知道在java项目里面采用poi来导出excel很方便,但是如果你的项目采用的是jsp你可以用更简单的方法来导出.首先你要在顶部引入:<jsp:directive.page im ...

  9. dataTables的导出Excel功能

    Datatables它是一款基于jQuery表格插件,钟情于它操作dom的灵活.做后台的同学想必使用它能事半功倍,而且交互强.容易扩展. 我也是最近要做公司后台界面,表格涉及的很多,所以考虑使用DT, ...

随机推荐

  1. 【转】asp.net c# 网上搜集面试题目大全(附答案)

    asp.net c# 网上搜集面试题目大全(附答案) http://www.cnblogs.com/hndy/articles/2234188.html

  2. IP_TOS选项

    voip IP_tos 选项 在IP头中,有一Type-of-Service字段,该字段描述了IP包的优先级和QoS选项,使用IP_TOS可以来设定该字段的值,以区分不同服务的优先级,Linux 中可 ...

  3. hdu1078 记忆化搜索

    /* hdu 1078 QAQ记忆化搜索 其实还是搜索..因为里面开了一个数组这样可以省时间 (dp[x][y]大于0就不用算了直接返回值) */ #include<stdio.h> #i ...

  4. PHP 错误与异常 笔记与总结(15 )使用观察者模式处理异常信息

    使异常处理变得更灵活.可观察,可以使用设计模式中的观察者模式. 文件 ① 定义观察者的接口 ExceptionObserver.php: <?php /* 给观察者定义的规范 */ interf ...

  5. JavaScript数据类型(转)

    JavaScript中有5种简单数据类型(也称为基本数据类型):Undefined.Null.Boolean.Number和String.还有1种复杂数据类型——Object,Object本质上是由一 ...

  6. 提高Vector容器的删除效率

    vector容器是类似与一个线性数组,索引效率高,插入,删除的效率很低,需要遍历数据列表,一般情况下vector的删除操作由一下函数完成: iterator erase(iterator positi ...

  7. oracle sqlplus常用命令

    登录到sqlplus sqlplus user/pwd@dbname 不登录使用 sqlplus /nolog 查看当前登录用户 show user; 更改用户密码 ALTER USER USER I ...

  8. 解决mysql数据库连接问题

    设置mysql远程连接root权限 在远程连接mysql的时候应该都碰到过,root用户无法远程连接mysql,只可以本地连,对外拒绝连接.需要建立一个允许远程登录的数据库帐户,这样才可以进行在远程操 ...

  9. SQL查询(一)

    查询基础练习 练习一: <学生信息表>:students(学号,姓名,性别,出生年,省份,入学年份,班级) <学生选修信息表>:stucourses(学号,课程号,课程名称,分 ...

  10. Neteaset News

    到了中后期了,新浪微博结束就可以找工作了,坚持住,最困难的一周 前天为了敲网易新闻,一直敲到了快五点,我想丽丽一个女生都那么拼,我怎么不行?知乎上一个哥们虽然年年第一,上台只讲一句话,nothing ...