数据库数据以Excel的方式导出
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的方式导出的更多相关文章
- 将mysql数据库数据以Excel文件的形式导出
最近在工作中,领导让从数据库中导出一些数据并存放到Excel表格中,网上有许多教程,下面是我总结的其中俩种方法. 从数据库管理工具中导出(navicat) 在navicat导出数据导Excel中还是比 ...
- 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 ...
- PHP中导出Excel,将数据以Excel形式导出
现在,很多地方都需要导出数据,这里说一种简单的方法将数据以Excel的形式导出,方法如下: <?php date_default_timezone_set('PRC');//设置时区 /*设置h ...
- 数据以Excel形式导出导服务器,再将文件读取到客户端另存 以HSSFWorkbook方式实现
public void exportExcel(List<P2pInfo> repayXist,HttpServletRequest request,HttpServletResponse ...
- SAP ABAP: 把内表数据以excel或csv格式,通过前台或者后台的方式上传至FTP服务器
今天接到一个FTP的需求,就是每天晚上把当天某个报表的数据自动保存excel上传到FTP服务器. SAP已经有现成的FTP函数使用,可以通过函数的方式来实现,实现前先准备一些数据: User:登录FT ...
- ASP.NET中将导出的数据以UTF-8编码方式进行存储
Response.Charset = "UTF-8"; Response.ContentEncoding = Encoding.UTF8; Response.AppendHea ...
- POI导出数据以Excel的方式录入,下载
简单描述:把数据导出到excel文件中.过程:获取要导出的数据列表(list),创建excel文件,数据放入. 代码: //html代码 <div class="btn-group&q ...
- Java使用POI插件将数据以excel形式备份
将数据以表格形式进行备份 (1)导入poi的jar包 放入lib下: WebRoot\WEB-INF\lib\poi-3.2-FINAL-20081019.jar 下载链接:https://gith ...
- springMVC(4)---生成excel文件并导出
springMVC(4)---生成excel文件并导出 在开发过程中,需要将数据库中的数据以excel表格的方式导出. 首先说明.我这里用的是Apache的POI项目,它是目前比较成熟的HSSF接口, ...
随机推荐
- Maven install报MojoExecutionException
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.2-beta-5:assembly (m ...
- Eclipse安装插件的“最好方法”:dropins文件夹的妙用
在Eclipse3.4以前安装插件非常繁琐. 在Eclipse3.5以后插件安装的功能做了改进.而且非常方便易用. 我们只需要把需要的插件复制(拖放)到eclipse\dropins,然后插件就安装成 ...
- py2exe 打包的两种方式
cmd模式 #!/usr/bin/python #-*- coding: UTF-8 -*- from distutils.core import setup import py2exe setup( ...
- SoapUI 5.2.1 调试工具
SoapUI 5.2.1 调试工具 1.打开soapUI. 2.新建一个项目,实例如下: 点击ok后在soapUI界面左侧会显示出此项目,如图: 2.创建测试用例: a.新建用例组,选择此项目右键,新 ...
- mac打开文件提示文件已经坏了的修改
10.12下面,mac做了安全的限制,不能打开任意的文件,需要解除限制 sudo spctl --master-disable
- Apache Solr 介绍
Apache Solr 介绍 Solr 是什么? Solr 是一个开源的企业级搜索服务器,底层使用易于扩展和修改的Java 来实现.服务器通信使用标准的HTTP 和XML,所以如果使用Solr 了解J ...
- [CMAKE] 详解CMakeLists.txt文件
[快速查询]https://cmake.org/cmake/help/v2.8.8/cmake.html#section_Commands 1 CMake简介 CMake是跨平台编译工具,比make更 ...
- Git忽略规则和.gitignore规则不生效的解决办法
Git忽略规则和.gitignore规则不生效的解决办法 Git忽略规则: 在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改根目录中 .gitignore 文件的方法(如果 ...
- windows下vscode修复c++找不到头文件
因为原博客太长将部分内容分开 vscode找不到头文件的问题是由于windows下vscode默认的编译器是微软的MSVC(vs使用的编译器)的头文件路径 如果你没有安装vs肯定会因为找不到头文件而报 ...
- pytest.2.运行多个文件
From: http://www.testclass.net/pytest/multiple_tests/ 背景 在现实的测试活动中,我们经常会定义多个用例文件,用来实现业务逻辑或其他逻辑上用例的物理 ...