import java.io.Serializable;
import java.util.List; import com.cfets.cwap.s.util.db.TableColumn;
/**
*
* @ClassName: ParamVO
* <b>Copyright 2018 中国XX中心 All Rights Reserved</b>
* @Description: TODO
* @author liuhanlin
* @date 2018年8月21日 下午2:15:45
*
*/ public class ParamVO implements Serializable{ /**
* @Fields serialVersionUID : TODO
*/
private static final long serialVersionUID = -1613650619973876968L;
// @TableColumn(name = "DL_TCKT_CD")
private String floorName;
private String productCode;
private String userCode;
private String statu;
public String getFloorName() {
return floorName;
}
public void setFloorName(String floorName) {
this.floorName = floorName;
}
public String getProductCode() {
return productCode;
}
public void setProductCode(String productCode) {
this.productCode = productCode;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getStatu() {
return statu;
}
public void setStatu(String statu) {
this.statu = statu;
} }
  2、Excel导出方法
/**
* @description:excel导出方法
* @param fileName 导出的excel名称
* @param titleColumn 导出的excel列对应的VO类的属性名称数组(VO类属性名称)
* @param titleNames 导出的excel列标题数组(表头)
* @param columnWidth 导出的excel列宽
* @param dataList 传入的数据的列表
* @return String
* @throws MscQueryException
* @author liuhanlin
* @create on 2016年12月29日
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static String excelExport(HttpServletResponse response,
String[] titleColumn, String[] titleNames,
List<?> dataList) throws MscQueryException {
logger.info("titleNames:{}, titleColumn:{}", titleNames, titleColumn);
long begin = System.currentTimeMillis(); ServletOutputStream out = null;
WritableWorkbook workbook = null;
try {
out = response.getOutputStream();
//本地测试
FileOutputStream outTest = new FileOutputStream(new File("C:/Users/Liuhl.LIUHL-PC/Desktop/190/Imix") + "/"
+ "xxx.xls");
workbook = Workbook.createWorkbook(outTest);
if(CollectionUtils.isEmpty(dataList)){
WritableSheet sheet = workbook.createSheet("sheet1", 0);
for (int i = 0; i < titleColumn.length; i++) {
Label label = new Label(i, 0, titleNames[i]);
sheet.addCell(label);
}
}else{
int row = 650;
int total = dataList.size();
int page = 0;//sheet页数 if(total % row == 0){
page = total/row;
}else{
page = total/row+1;
}
for(int k = 0;k < page;k++){ WritableSheet sheet = workbook.createSheet("sheet"+(k+1), 0); //写入表头内容
for (int i = 0; i < titleNames.length; i++) {
Label label = new Label(i, 0, titleNames[i]);
sheet.addCell(label);
} if (!CollectionUtils.isEmpty(dataList)) {
List<?> list = new ArrayList(); int cols = 0;
if(k == page-1){
cols = total % row;
list = dataList.subList(k*row, total);
} else{
cols = row;
list = dataList.subList(k*row, row*(k+1));
} for (int i = 1; i <= cols; i++) { Object obj = list.get(i - 1);
Class c = obj.getClass(); for (int j = 0; j < titleColumn.length; j++) {
String columnName = titleColumn[j];
String title = Character.toUpperCase(columnName.charAt(0))
+ columnName.substring(1);
String methodName = "get" + title;
Method method = c.getDeclaredMethod(methodName);
String data = "null".equalsIgnoreCase(method.invoke(obj)+"") ? "--" : method
.invoke(obj).toString();
Label label = new Label(j, i, data);
sheet.addCell(label);
}
}
}
}
}
workbook.write();
} catch (Exception e) {
logger.error("导出excel失败!", e);
throw new MscQueryException("导出excel失败!" + e);
} finally {
try {
workbook.close();
out.close();
} catch (IOException e) {
logger.error("导出excel失败!", e);
}
}
long end = System.currentTimeMillis();
logger.debug("导出XXX数据时间为:{}",(end-begin));
return null;
}
3、测试类
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import javax.servlet.http.HttpServletResponse; import org.apache.commons.collections.CollectionUtils;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.Configuration;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.cfets.cwap.s.spi.SpiConfig;
import com.cfets.cwap.s.spi.SpiConfig.Env;
import com.cfets.ts.s.deal.exception.MscQueryException;
import com.cfets.ts.u.dealmgmt.entity.vo.ParamVO;
import com.cfets.ts.u.dealmgmt.service.ExcelExportUtil;
import com.cfets.ts.u.dealmgmt.service.FxDealMapService;
import com.cfets.ts.u.dealmgmt.util.GenerateExcelService;
import com.google.gson.Gson;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations ="/cwap-spring-context-app.xml")
//@ContextConfiguration(locations = { "classpath:/cwap-spring-context-app.xml"})
@Configuration
public class Test {
FxDealMapService service = new FxDealMapService();
static {
SpiConfig.env = Env.TEST;
}
@org.junit.Test
public void test(){
List<Map<String, Object>> lists = service.queryMapData();
List<ParamVO> list = new ArrayList<ParamVO>();
for (Map<String, Object> map : lists) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
ParamVO vo = new ParamVO();
vo.setFloorName((String)map.get("floorName"));
vo.setProductCode((String)map.get("productCode"));
vo.setUserCode((String)map.get("userCode"));
vo.setStatu((String)map.get("statu"));
list.add(vo);
}
}
System.err.println(new Gson().toJson(list));
MockHttpServletResponse response = new MockHttpServletResponse();
String titleColumn[] = {"floorName", "productCode","userCode","statu"};
String titleNames[] = {"floorName", "productCode", "userCode", "statu"};
try {
ExcelExportUtil.excelExport(response, titleColumn, titleNames, list);
} catch (MscQueryException e) {
e.printStackTrace();
}
}

备注:(jexcelapi-jxl.jar)

数据库数据以Excel的方式导出的更多相关文章

  1. 将mysql数据库数据以Excel文件的形式导出

    最近在工作中,领导让从数据库中导出一些数据并存放到Excel表格中,网上有许多教程,下面是我总结的其中俩种方法. 从数据库管理工具中导出(navicat) 在navicat导出数据导Excel中还是比 ...

  2. Java将数据以Excel文件形式导出后台代码实现

    下面代码实现所需jar包: tomcat-embed-core-8.5.11.jar: commons-lang3-3.0.1.jar: commons-io-2.5.jar: poi-3.9.jar ...

  3. PHP中导出Excel,将数据以Excel形式导出

    现在,很多地方都需要导出数据,这里说一种简单的方法将数据以Excel的形式导出,方法如下: <?php date_default_timezone_set('PRC');//设置时区 /*设置h ...

  4. 数据以Excel形式导出导服务器,再将文件读取到客户端另存 以HSSFWorkbook方式实现

    public void exportExcel(List<P2pInfo> repayXist,HttpServletRequest request,HttpServletResponse ...

  5. SAP ABAP: 把内表数据以excel或csv格式,通过前台或者后台的方式上传至FTP服务器

    今天接到一个FTP的需求,就是每天晚上把当天某个报表的数据自动保存excel上传到FTP服务器. SAP已经有现成的FTP函数使用,可以通过函数的方式来实现,实现前先准备一些数据: User:登录FT ...

  6. ASP.NET中将导出的数据以UTF-8编码方式进行存储

      Response.Charset = "UTF-8"; Response.ContentEncoding = Encoding.UTF8; Response.AppendHea ...

  7. POI导出数据以Excel的方式录入,下载

    简单描述:把数据导出到excel文件中.过程:获取要导出的数据列表(list),创建excel文件,数据放入. 代码: //html代码 <div class="btn-group&q ...

  8. Java使用POI插件将数据以excel形式备份

    将数据以表格形式进行备份 (1)导入poi的jar包 放入lib下:  WebRoot\WEB-INF\lib\poi-3.2-FINAL-20081019.jar 下载链接:https://gith ...

  9. springMVC(4)---生成excel文件并导出

    springMVC(4)---生成excel文件并导出 在开发过程中,需要将数据库中的数据以excel表格的方式导出. 首先说明.我这里用的是Apache的POI项目,它是目前比较成熟的HSSF接口, ...

随机推荐

  1. 使用kolla安装的openstack mariadb为集群所有节点无法启动

    当在做测试时,把所有的openstack节点都关机,再开启做测试时,发现mariadb galera集群启不来,相当于所有的mariadb集群都停止了(跟所有节点断电情况相似),这时候怎么办呢,重新建 ...

  2. python初始化list列表(1维、2维)

    1.初始化递增的list: list1 = range(10)#print list1#[0,1,2,...,9] 2.初始化每项为0的一维数组: list2 = [0] * 5#print list ...

  3. 爬取ofo共享单车信息

    前段时间看到很多微信公众号在转发一篇爬取mobike单车的信息,也不知道什么原因,在网上搜索了下很少有人在爬取ofo共享单车的数据,所以决定看看可以爬取ofo共享单车的那些数据. 抓取数据开始的时候, ...

  4. jmeter --响应断言详解

    jmeter --响应断言详解 响应断言 :对服务器的响应进行断言校验 (1)应用范围: main sample and sub sample, main sample only , sub-samp ...

  5. 自然语言处理之:搭建基于HanLP的开发环境(转)

    环境搭建比FNLP的简单,具体参考:https://github.com/hankcs/HanLP 各个版本的下载:https://github.com/hankcs/HanLP/releases 完 ...

  6. python获取代理IP并测试是否可用

    # coding: utf-8 import urllib2 import re import time def getDL(page): url = 'http://www.xicidaili.co ...

  7. ML: 聚类算法-概论

    聚类分析是一种重要的人类行为,早在孩提时代,一个人就通过不断改进下意识中的聚类模式来学会如何区分猫狗.动物植物.目前在许多领域都得到了广泛的研究和成功的应用,如用于模式识别.数据分析.图像处理.市场研 ...

  8. Hadoop2.2.0 eclipse插件编译及Ecliipse配置说明(图文版)

    一.引言: 最近在做一个城商行项目的POC测试it版本,涉及到编译Linux64bti的源码和开发插件使用,作为笔记分享给大家. 二.插件编译 Hadoop2x版本的Eclipse插件已经单独抽取成独 ...

  9. Linux Docker安装Jenkins

    安装环境 操作系统 centos7.3 安装Docker,安装及配置见 <Docker之Docker介绍及安装配置> 安装Jenkins 下载Jenkins 命令:sudo docker ...

  10. Bitmap BitmapData

    var sp:Sprite=new Sprite(); sp.graphics.beginFill(0xffccdd); sp.graphics.drawRect(0,0,100,100); sp.g ...