需要下载一个poi的jar包。

控制器

@Override
public void getContractListExecl(Contract contract, BindingResult result, HttpServletRequest req,
HttpServletResponse response) throws IOException { List<Map<String, Object>> sttList = contractService.getContracts(contract);// 得到需要导出的数据
int i = 0;
// 第一步,创建一个webbook,对应一个Excel文件
@SuppressWarnings("resource")
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("title");
// 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth((short) 20);
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow(i);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_LEFT); // 创建一个居中格式 HSSFCell cell = row.createCell(0);
cell.setCellValue("用户姓名");
cell.setCellStyle(style);
cell = row.createCell(1); cell.setCellValue("电话");
cell.setCellStyle(style);
cell = row.createCell(2); cell.setCellValue("家庭医生团队名称");
cell.setCellStyle(style);
cell = row.createCell(3); cell.setCellValue("服务办理人姓名");
cell.setCellStyle(style);
cell = row.createCell(4); cell.setCellValue("服务办理人电话");
cell.setCellStyle(style);
cell = row.createCell(5); cell.setCellValue("服务办理人代码");
cell.setCellStyle(style);
cell = row.createCell(6); cell.setCellValue("完成签约时间");
cell.setCellStyle(style);
cell = row.createCell(7); cell.setCellValue("服务完成ITV编码");
cell.setCellStyle(style);
cell = row.createCell(8); cell.setCellValue("服务完成时间");
cell.setCellStyle(style);
cell = row.createCell(9); for (Map<String, Object> row_ : sttList) {
row = sheet.createRow(++i);
// 第四步,创建单元格,并设置值
row.createCell(0)
.setCellValue(row_.get("userName") == null ? "###" : row_.get("userName").toString());
row.createCell(1)
.setCellValue(row_.get("userPhone") == null ? "###" : row_.get("userPhone").toString());
row.createCell(2).setCellValue(row_.get("dgName") == null ? "###" : row_.get("dgName").toString());
row.createCell(3)
.setCellValue(row_.get("proxyName") == null ? "###" : row_.get("proxyName").toString());
row.createCell(4).setCellValue(row_.get("proxyPhone") == null ? "###" : row_.get("proxyPhone").toString());
row.createCell(5).setCellValue(row_.get("proxyCode") == null ? "###" : row_.get("proxyCode").toString());
row.createCell(6).setCellValue(row_.get("updateTime") == null ? "###" : row_.get("updateTime").toString());
row.createCell(7).setCellValue(row_.get("itvToken") == null ? "###" : row_.get("itvToken").toString());
row.createCell(8).setCellValue(row_.get("itvUpdateTime") == null ? "###" : row_.get("itvUpdateTime").toString()); }
String fileName = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
response.setHeader("content-disposition",
"attachment;filename=" + new String(fileName.getBytes("UTF-8"), "ISO8859-1") + ".xls");
OutputStream fout = response.getOutputStream();
wb.write(fout);
fout.close();
} }

前端调用该接口就可以了!

效果图:

JAVA 使用POI导出数据格式为Execl的更多相关文章

  1. Java之POI导出Excel(一):单sheet

    相信在大部分的web项目中都会有导出导入Excel的需求,今天我们就来看看如何用Java代码去实现 用POI导出Excel表格. 一.pom引用 pom文件中,添加以下依赖 查看代码  <!-- ...

  2. java 使用POI导出百万级数据

    先看结果吧,这只是测试其中有很多因数影响了性能. 表总数为:7千多万,测试导出100万 表字段有17个字段 最终excel大小有60多兆 总耗时:126165毫秒 差不多2分多钟 其核心简单来说就是分 ...

  3. java解决poi导出excel文字水印,导出excel不可操作问题

    首先需求是用户提出导出excel数据需使用水印备注其用途: 其实就是在导出excel的同时带有自定义文字水印的导出. 那么我们首先想到的肯定是以一个什么样的思路去解决该问题,首先查找poi导出exce ...

  4. java使用poi导出excel

    继上一篇导出pdf,这篇导出excel. 1.导入依赖 <dependency> <groupId>org.apache.poi</groupId> <art ...

  5. Java使用POI导出excel(下)——实例与小技巧

    [更新]:thinkgem的导出工具类: /** * Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesit ...

  6. Java使用POI导出excel(上)——基本操作

    相关的介绍参考自:http://zc985552943.iteye.com/blog/1491546 一.概述 1.概念 受上文博文博主的启发,有必要先对excel的各个概念先做了解! //上述基本都 ...

  7. java springmvc poi 导出Excel,先简单记录,后期会详细描写

    POI jar包下载 : http://poi.apache.org/download.html jsp代码 <%@ page language="java" content ...

  8. Java之POI导出Excel(二):多个sheet

    相信在大部分的web项目中都会有导出导入Excel的需求,之前我也写过一篇导出单个sheet工作表的文章,没看过的小伙伴可以去看哈,链接也给大家放出来了:导出单个sheet 但是在我们日常的工作中,需 ...

  9. java利用poi导出数据到excel

    背景: 上一篇写到利用jtds连接数据库获取对应的数据,本篇写怎样用poi将数据到处到excel中,此程序为Application 正文: 第三方poi jar包:poi驱动包下载 代码片段: /** ...

随机推荐

  1. Json 、 Jsonp

    SONP is simply a hack to allow web apps to retrieve data across domains. It could be said that it vi ...

  2. ssm框架中的struts我的配置问题

    <?xml version="1.0" encoding="UTF-8" ?>  <!DOCTYPE struts PUBLIC    &qu ...

  3. toggle函数

    $(function() { $('.love').toggle(function() { $(this).attr("src", "images/loved.png&q ...

  4. iOS:融云即时通讯快速集成

    一.介绍 即时通讯在众多社交软件.生活软件以及教育软件中已经是必备的功能了,在当前国内,即时通讯SDK做的比较不错的有那么几家,例如环信SDK.融云SDK...,这两家做的都很不错,各有千秋吧,要是真 ...

  5. vim - multiple windows

    https://www.cs.oberlin.edu/~kuperman/help/vim/windows.html

  6. http-2.4.18 安装

    编译最新的httpd 2.4.18     编译提示apr版本低于1.4 rpm repo源是1.39的 因此编译apr 下载 apr apr-util  apr-iconv [root@ansibl ...

  7. leetcode 374

    这个题目很简单,但是要注意细节和对题目的理解,一开始我把guess函数的作用理解错了,第一版代码长这样: int guessNumber(int n) { ; int high = n; while( ...

  8. 反射调用方法时的两种情况,走get set和不走get set

    @Test public void test1() throws Exception{  //获取User类  Class class1=Class.forName("cn.jbit.bea ...

  9. Wamp 设置 www 目录

    打开httpd.conf,搜索wwwroot 和 directory 直接改为新目录即可. 对于wamp3是无效的,可以这样解决: “You need to change these values a ...

  10. ruby基础语法

    首发:个人博客,更新&纠错&回复 学不同语言,常将它们的基础语法搞混,例如if后面有没有(),后面是then还是:还是{,结尾是end还是}. 这种事情毫无技术含量又毫无乐趣可言,但极 ...