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实现大批量 ...
随机推荐
- Ext.get Ext.getDom Ext.getCmp 的区别
Html DOM Ext Element Component Component 最高层 Html DOM 最基础 Ext.getCmp 是 Ext.ComponentMgr.get ...
- jquery api
1. clone()可以复制一个节点 2. .prop()方法为元素赋属性值非常方便. $("input").prop("disabled", false); ...
- VisualSVN Server导入Repository
SVN服务器换机器了,原来SVN服务器A的Repository需要原样复制到新的SVN服务器B. 原样复制 Repository ,是为了保持SVN内容不会因换了服务器而导致不同. 那么在新SVN服务 ...
- MySQL 安装和启动服务,“本地计算机 上的 MySQL 服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止。”
MySQL 安装和启动服务,以及遇到的问题 MySQL版本: mysql-5.7.13-winx64.zip (免安装,解压放到程序文件夹即可,比如 C:\Program Files\mysql-5. ...
- linux学习笔记--NFS
NFS分为客户端,服务端.客户端要访问服务端,要写从RPC服务获得端口. 整个流程分为: 1,服务端rpc服务先启动2,nfs服务把端口上报给rpc服务3,rpc客户端通过服务端的rpcbind拿到访 ...
- ruby Matrix 输出 格式化
require 'matrix' class Matrix def to_pretty_s s = "" i = 0 while i < self.column_size s ...
- sql server 分页存储过程
----------------------分页存储过程------------------------------------------------------------------------ ...
- python autopep8
安装 使用pip install autopep8或easy_install 都可以. 使用 autopep8 -i -a 要检查的py文件路径 更多参数使用可以参考:https://github.c ...
- C# 对Access数据库操作的通用类
(转载自博主Jerry很简单) //Access数据库-C# 操作类 代码using System;using System.Collections.Generic;using System.Linq ...
- WKwebView与JS交互(h5主动)
先:WKUIDelegate,WKNavigationDelegate,WKScriptMessageHandler // 创建一个webiview的配置项 WKWebViewConfiguratio ...