java 数据导入到exc ,并下载
package com.lizi.admin.controller.platform.excel;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExcelUtil {
//创建HSSFWorkbook工作薄对象
public static HSSFWorkbook export_text(List<Map<String,Object>> list,String data){
try {
//创建工作薄对象
HSSFWorkbook wb = new HSSFWorkbook();
//创建标题行样式
HSSFCellStyle headStyle = headStyle(wb);
//创建内容行样式
HSSFCellStyle contentStyle = contentStyle(wb);
//创建表
HSSFSheet sheet_1 = wb.createSheet(data+"对账信息");
//设置表的默认列宽
sheet_1.setDefaultColumnWidth(30);
//创建标题行
HSSFRow headRow = sheet_1.createRow(0);
HSSFCell head_cell_1 = headRow.createCell(0); //创建标题行第一列
head_cell_1.setCellValue("对账时间"); //第一列内容
head_cell_1.setCellStyle(headStyle); //将标题行样式添加
HSSFCell head_cell_2 = headRow.createCell(1);
head_cell_2.setCellValue("商家总流水");
head_cell_2.setCellStyle(headStyle);
HSSFCell head_cell_3 = headRow.createCell(2);
head_cell_3.setCellValue("商家总流水笔数");
head_cell_3.setCellStyle(headStyle);
HSSFCell head_cell_4 = headRow.createCell(3);
head_cell_4.setCellValue("实际收单总流水");
head_cell_4.setCellStyle(headStyle);
HSSFCell head_cell_5 = headRow.createCell(4);
head_cell_5.setCellValue("实际收单总流水笔数");
head_cell_5.setCellStyle(headStyle);
HSSFCell head_cell_6 = headRow.createCell(5);
head_cell_6.setCellValue("差异金额");
head_cell_6.setCellStyle(headStyle);
HSSFCell head_cell_7 = headRow.createCell(6);
head_cell_7.setCellValue("差异笔数");
head_cell_7.setCellStyle(headStyle);
HSSFCell head_cell_8 = headRow.createCell(7);
head_cell_8.setCellValue("调账金额");
head_cell_8.setCellStyle(headStyle);
HSSFCell head_cell_9 = headRow.createCell(8);
head_cell_9.setCellValue("调账笔数");
head_cell_9.setCellStyle(headStyle);
HSSFCell head_cell_10 = headRow.createCell(9);
head_cell_10.setCellValue("对账单状态");
head_cell_10.setCellStyle(headStyle);
//为内容行添加数据和样式
for (int i = 1; i <= list.size(); i++) {
HSSFRow contentRow = sheet_1.createRow(i);
Map<String, Object> map=list.get(i-1);
String accountsTime=map.get("accountsTime").toString();
String sysMoney=map.get("sysMoney").toString();
String sysNum=map.get("sysNum").toString();
String passagewayMoney=map.get("passagewayMoney").toString();
String passagewayNum=map.get("passagewayNum").toString();
String differenceMoney=map.get("differenceMoney").toString();
String differenceNumber=map.get("differenceNumber").toString();
String adjustmentMoney=map.get("adjustmentMoney").toString();
String adjustmentNumber=map.get("adjustmentNumber").toString();
String state="";
if(map.get("state")!=null){
if(map.get("state").toString().equals("0")){
state="正常";
}
if(map.get("state").toString().equals("1")){
state="未对账";
}
if(map.get("state").toString().equals("2")){
state="已对账";
}
}
HSSFCell content_cell_1 = contentRow.createCell(0);
content_cell_1.setCellValue(accountsTime);
content_cell_1.setCellStyle(contentStyle);
HSSFCell content_cell_2 = contentRow.createCell(1);
content_cell_2.setCellValue(sysMoney);
content_cell_2.setCellStyle(contentStyle);
HSSFCell content_cell_3 = contentRow.createCell(2);
content_cell_3.setCellValue(sysNum);
content_cell_3.setCellStyle(contentStyle);
HSSFCell content_cell_4 = contentRow.createCell(3);
content_cell_4.setCellValue(passagewayMoney);
content_cell_4.setCellStyle(contentStyle);
HSSFCell content_cell_5 = contentRow.createCell(4);
content_cell_5.setCellValue(passagewayNum);
content_cell_5.setCellStyle(contentStyle);
HSSFCell content_cell_6 = contentRow.createCell(5);
content_cell_6.setCellValue(differenceMoney);
content_cell_6.setCellStyle(contentStyle);
HSSFCell content_cell_7 = contentRow.createCell(6);
content_cell_7.setCellValue(differenceNumber);
content_cell_7.setCellStyle(contentStyle);
HSSFCell content_cell_8 = contentRow.createCell(7);
content_cell_8.setCellValue(adjustmentMoney);
content_cell_8.setCellStyle(contentStyle);
HSSFCell content_cell_9 = contentRow.createCell(8);
content_cell_9.setCellValue(adjustmentNumber);
content_cell_9.setCellStyle(contentStyle);
HSSFCell content_cell_10 = contentRow.createCell(9);
content_cell_10.setCellValue(state);
content_cell_10.setCellStyle(contentStyle);
}
return wb;
} catch (Exception e) {
e.getStackTrace();
}
return null;
}
//创建HSSFWorkbook工作薄对象
public static HSSFWorkbook export_day(List<Map<String,Object>> list,String data){
try {
//创建工作薄对象
HSSFWorkbook wb = new HSSFWorkbook();
//创建标题行样式
HSSFCellStyle headStyle = headStyle(wb);
//创建内容行样式
HSSFCellStyle contentStyle = contentStyle(wb);
//创建表
HSSFSheet sheet_1 = wb.createSheet(data+"对账信息");
//设置表的默认列宽
sheet_1.setDefaultColumnWidth(30);
//创建标题行
HSSFRow headRow = sheet_1.createRow(0);
HSSFCell head_cell_1 = headRow.createCell(0); //创建标题行第一列
head_cell_1.setCellValue("对账时间"); //第一列内容
head_cell_1.setCellStyle(headStyle); //将标题行样式添加
HSSFCell head_cell_2 = headRow.createCell(1);
head_cell_2.setCellValue("商户名称");
head_cell_2.setCellStyle(headStyle);
HSSFCell head_cell_3 = headRow.createCell(2);
head_cell_3.setCellValue("系统订单号");
head_cell_3.setCellStyle(headStyle);
HSSFCell head_cell_4 = headRow.createCell(3);
head_cell_4.setCellValue("系统订单金额");
head_cell_4.setCellStyle(headStyle);
HSSFCell head_cell_5 = headRow.createCell(4);
head_cell_5.setCellValue("通道订单号");
head_cell_5.setCellStyle(headStyle);
HSSFCell head_cell_6 = headRow.createCell(5);
head_cell_6.setCellValue("通道订单金额");
head_cell_6.setCellStyle(headStyle);
HSSFCell head_cell_7 = headRow.createCell(6);
head_cell_7.setCellValue("订单状态");
head_cell_7.setCellStyle(headStyle);
HSSFCell head_cell_8 = headRow.createCell(7);
head_cell_8.setCellValue("订单类型");
head_cell_8.setCellStyle(headStyle);
HSSFCell head_cell_9 = headRow.createCell(8);
head_cell_9.setCellValue("交易时间");
head_cell_9.setCellStyle(headStyle);
//为内容行添加数据和样式
for (int i = 1; i <= list.size(); i++) {
HSSFRow contentRow = sheet_1.createRow(i);
Map<String, Object> map=list.get(i-1);
String accountsTime=map.get("accountsTime").toString();
String shopName=map.get("shopName").toString();
String sysNo="";
if(map.get("sysNo")!=null){
sysNo=map.get("sysNo").toString();
}
String sysMoney="";
if(map.get("sysMoney")!=null){
sysMoney=map.get("sysMoney").toString();
}
String passagewayNo="";
if(map.get("passagewayNo")!=null){
passagewayNo=map.get("passagewayNo").toString();
}
String passagewayMoney="";
if(map.get("passagewayMoney")!=null){
passagewayMoney=map.get("passagewayMoney").toString();
}
String orderType="";
if(map.get("orderType")!=null){
int type=Integer.valueOf(map.get("orderType").toString()).intValue();
switch (type) {//0正常1订单缺失2通道缺失3金额不正确
case 0:
orderType="正常";
break;
case 1:
orderType="订单缺失";
break;
case 2:
orderType="通道缺失";
break;
default:
orderType="金额不正确";
break;
}
}
String orderState="";
if(map.get("orderState")!=null){
int state=Integer.valueOf(map.get("orderState").toString()).intValue();
switch (state) {//0正常1未对账2已对帐3已调账
case 0:
orderState="正常";
break;
case 1:
orderState="未对账";
break;
case 2:
orderState="已对帐";
break;
default:
orderState="已调账";
break;
}
}
String transactionTime=map.get("transactionTime").toString();
HSSFCell content_cell_1 = contentRow.createCell(0);
content_cell_1.setCellValue(accountsTime);
content_cell_1.setCellStyle(contentStyle);
HSSFCell content_cell_2 = contentRow.createCell(1);
content_cell_2.setCellValue(shopName);
content_cell_2.setCellStyle(contentStyle);
HSSFCell content_cell_3 = contentRow.createCell(2);
content_cell_3.setCellValue(sysNo);
content_cell_3.setCellStyle(contentStyle);
HSSFCell content_cell_4 = contentRow.createCell(3);
content_cell_4.setCellValue(sysMoney);
content_cell_4.setCellStyle(contentStyle);
HSSFCell content_cell_5 = contentRow.createCell(4);
content_cell_5.setCellValue(passagewayNo);
content_cell_5.setCellStyle(contentStyle);
HSSFCell content_cell_6 = contentRow.createCell(5);
content_cell_6.setCellValue(passagewayMoney);
content_cell_6.setCellStyle(contentStyle);
HSSFCell content_cell_7 = contentRow.createCell(6);
content_cell_7.setCellValue(orderType);
content_cell_7.setCellStyle(contentStyle);
HSSFCell content_cell_8 = contentRow.createCell(7);
content_cell_8.setCellValue(orderState);
content_cell_8.setCellStyle(contentStyle);
HSSFCell content_cell_9 = contentRow.createCell(8);
content_cell_9.setCellValue(transactionTime);
content_cell_9.setCellStyle(contentStyle);
}
return wb;
} catch (Exception e) {
e.getStackTrace();
}
return null;
}
/**
* 创建标题行样式
* @param wb
* @return
*/
public static HSSFCellStyle headStyle(HSSFWorkbook wb){
HSSFCellStyle headStyle = wb.createCellStyle(); //创建样式对象
HSSFFont headFont = wb.createFont(); //创建字体
headFont.setFontName("微软雅黑");
headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
headFont.setColor(HSSFFont.COLOR_RED);
headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
headStyle.setFont(headFont);
return headStyle;
}
/**
* 创建内容行样式
* @param wb
* @return
*/
public static HSSFCellStyle contentStyle(HSSFWorkbook wb){
HSSFCellStyle contentStyle = wb.createCellStyle();
HSSFFont contentFont = wb.createFont();
contentFont.setFontName("微软雅黑");
contentFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
contentFont.setColor(HSSFFont.COLOR_NORMAL);
contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
contentStyle.setFont(contentFont);
return contentStyle;
}
}
/**
* 导出年月的对账单
* @param model
* @param request
* @param response
* @param session
* @param payType
* @param date
* @param type
* @return
*/
@ResponseBody
@RequestMapping(value = "/FINANCIALCENTER/exportByYM")
public ModelAndView exportByYM(Model model, HttpServletRequest request,HttpServletResponse response, HttpSession session,
@RequestParam(value = "payType", required = true) String payType,
@RequestParam(value = "date", required = true) String date,
@RequestParam(value = "type", required = true) String type) {
try {
if(StringUtils.isNotBlank(payType)){
if(payType.equals("1")){
payType="7";
}
if(payType.equals("2")){
payType="6";
}
if(payType.equals("3")){
payType="4";
}
}
Criteria criteria = new Criteria();
criteria.getCondition().put("state", "");
criteria.getCondition().put("type", payType);
SessionUser user = getSessionUser(session);
TUSER tUser = this.tUSERService.queryById(user.getUserId());
Integer agentId=0;
if(tUser.getTYPE().equals(SystemConfig.USER_TYPE_AGENT_T)){
agentId=tUser.getAGENTID();
}
criteria.getCondition().put("agentId",agentId);
List<Map<String,Object>> maps=null;
if(type.equals("1")){//年
criteria.getCondition().put("year", date);
maps=this.adjustService.getOrderByYearAndType(criteria);
}
if(type.equals("2")){//月
criteria.getCondition().put("date", date);
maps=this.adjustService.getOrderByMonthAndType(criteria);
}
if(type.equals("3")){//日
criteria.getCondition().put("day", date);
maps=this.adjustService.getOrderByDayAndType(criteria);
}
if(maps!=null&&maps.size()>0){
//调用方法创建HSSFWorkbook工作簿对象
HSSFWorkbook wb = null;
if(type.equals("3")){//日
wb = ExcelUtil.export_day(maps,date);
}else{
wb = ExcelUtil.export_text(maps,date);
}
try {
//定义导出文件的名称,看不懂的同学可以先行了解一下文件下载
Random r = new Random();
int n2 = r.nextInt(1000);
String name=date+"_"+n2+".xls";
String fileName = new String(name.getBytes("UTF-8"),"ISO-8859-1");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","attachment; filename="+fileName);
OutputStream os = response.getOutputStream();
//将工作薄写入到输出流中
wb.write(os);
os.close();
} catch (Exception e) {
e.getStackTrace();
}
}else{
model.addAttribute(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
model.addAttribute(ErrorMsg.KEY_MESSAGE, date+"该日期没有交易数据");
return new ModelAndView(new MappingJacksonJsonView());
}
} catch (Exception e) {
e.printStackTrace();
model.addAttribute(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
model.addAttribute(ErrorMsg.KEY_MESSAGE, "查詢失败!");
}
return new ModelAndView(new MappingJacksonJsonView());
}
/**
* 导出excel
*/
exports.exportExcel = function(me){
var year = $cmt_year_details.param.year;
var payType = $cmt_year_details.payType;
var type = 1;
confirmDialog("导出提示", "确定导出Excel么?", {
okAction : function() {
$("#"+$cmt_year_details.page_id).loading('show');
location.href=$cmt_year_details.config.exportByYM+'&date='+year+'&payType='+payType+'&type='+type;
$("#"+$cmt_year_details.page_id).loading('hide');
}
});
}
java 数据导入到exc ,并下载的更多相关文章
- java 数据导入xls
@RequestMapping("admin/doorDesign/getexcel.do") public void getExcel(String name,String ph ...
- Java实现大批量数据导入导出(100W以上) -(二)导出
使用POI或JXLS导出大数据量(百万级)Excel报表常常面临两个问题: 1. 服务器内存溢出: 2. 一次从数据库查询出这么大数据,查询缓慢. 当然也可以分页查询出数据,分别生成多个Excel打包 ...
- java中 Excel表实现数据导入导出
需要引入依赖: <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> <dependency> < ...
- java中通过jacob调用dts进行数据导入导出
在一个项目中需要金蝶软件对接,但是业务服务器和财务服务器相隔很远(中间经过好几台服务器,有内网也有外网),从一个内网向另一个内网中传输时,需要外网辅助,因为不让原始数据受污染,使用了DTS数据同步到另 ...
- 效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中【附源代码下载】) 转
效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中[附源代码下载]) 本文目录: (一)背景 (二)数据库数据导入到Excel的方法比较 ...
- Java代码实现excel数据导入到Oracle
1.首先需要两个jar包jxl.jar,ojdbc.jar(注意版本,版本不合适会报版本错误)2.代码: Java代码 import java.io.File; import java.io.Fi ...
- java基于xml配置的通用excel单表数据导入组件(五、Action处理类)
package xxxxxx.manage.importexcel; import java.io.File; import java.util.HashMap; import java.util.M ...
- java基于xml配置的通用excel单表数据导入组件(四、DAO主处理类)
package XXXXX.manage.importexcel; import java.beans.IntrospectionException; import java.io.BufferedR ...
- Java实现大批量数据导入导出(100W以上) -(一)导入
最近业务方有一个需求,需要一次导入超过100万数据到系统数据库.可能大家首先会想,这么大的数据,干嘛通过程序去实现导入,为什么不直接通过SQL导入到数据库. 大数据量报表导出请参考:Java实现大批量 ...
随机推荐
- 使用myeclipse为java web项目添加SSH框架
添加SSH框架时,要严格按照先Struts,再Spring,最后Hibernate.添加方法见下方: 第一步:添加Struts框架 请按照图示一步步认真执行,配置好struts才可以进行下一步 第二步 ...
- webstorm 2016 激活(转)
2016.2.2 版本的破解方式: 安装以后,打开软件会弹出一个对话框:选择"license server" 输入:http://114.215.133.70:41017 2016 ...
- VBA 小知识
1. 循环 Dim i As Integer 'body Next 'body Wend 2. 键值数据结构 'create dictionary object Set dictMembers = C ...
- JavaScript中的事件
1.冒泡事件:事件按照特定的的事件目标到最不特定的事件目标顺序触发(它是按照DOM的层次节后依次做出的反应) 2.捕获事件:事件从不确定的对象document 开始触发然后到最精确(也可以在窗口级别捕 ...
- (转)maven配置之pom.xml配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 用"时:分:秒"的方式显示运行时间
import datetime,time start = datetime.datetime.now()...dosomething() end = datetime.datetime.now()pr ...
- 基于 Ubuntu 编译 windows 版 adb
. . . . . adb 的源码在 Android 源码树中,所以只能在 Linux 下编译,而在 Linux 下编译 windows 版本的应用就需要使用交叉编译器 MinGW 了. 环境: Ub ...
- SQL 分页
sql = "SELECT TOP 10000 * " + " FROM(SELECT ROW_NUMBER() OVER(ORDER BY DataArticleID) ...
- ASP.NET ZERO 学习 HangFire的使用二
之前在ABP配置好了HangFire环境之后,那么如何才能添加Job到HangFire中让其执行呢 这就需要参考ABP的Background Jobs and Workers, 参考路径:http:/ ...
- Python SQLAlchemy --1
本文為 Python SQLAlchemy ORM 一系列教學文: SQLAlchemy 大概是目前 Python 最完整的資料庫操作的套件了,不過最令人垢病的是它的文件真的很難閱讀,如果不搭配個實例 ...