需要下载一个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. 解决pip国外安装源慢的问题

    用默认的pip安装源pypi.python.org由于在国外经常会出现超时的问题,而且安装速度极其的慢,如下图中的超时问题=>

  2. HTML其他基本格式说明

    1.<!DOCTYPE>说明页面中使用的HTML版本,只是信息声明 2.元信息元素<meta>,通过属性定义文件信息的名称.内容.文档的关键字.作者及描述等..content内 ...

  3. angularJs指令执行的机制==大概的三个阶段

    第一阶段:加载阶段 angularJs要运行的话,需要去等待angular.js加载完成,加载完之后呢,angular就会去查找到ng-app这个指令,ng-app在每个应用里面只能出现一次, 它也就 ...

  4. IEnumerable、GetEnumerator、IEnumerator的理解

    概念文字性的东西,我们就不说了,这里我们来点具体的实例第呀: 实例一: using System; using System.Collections; using System.Collections ...

  5. java读取记事本文件的部分数据添加到mysql

    package com.tideway.readtxt; import java.io.BufferedReader; import java.io.FileInputStream; import j ...

  6. VBA好的插件

    VBE 小插件--- Office 编程助手 :http://club.excelhome.net/thread-1055425-1-1.html VB以及VBA的编程助手MZ-tools7.0破解版 ...

  7. windows系统调用 利用事件对象实现进程通信

    #include "iostream" #include "windows.h" #include "cstring" using name ...

  8. c3p0数据库连接池使用

  9. 自定义属性 view

    首先自定义一个圆,相信以前的学习大家都会画圆,在values下写一些自定义的属性 package com.exaple.day01rikao; import android.content.Conte ...

  10. Dynamics AX 2012 R2 IIS WebSite Unauthorized 401

    今天,Reinhard部署好Aif Customer Service ,打开http://host:port/MicrosoftDynamicsAXAif60/,发现提示以下错误: 401 - Una ...