Accomplishing export excel by date.

This commit is contained in:
little-cat-sweet 2021-11-18 12:04:16 +08:00
parent 05eb6e1d92
commit 167bffcd58
8 changed files with 120 additions and 155 deletions

View File

@ -1,53 +0,0 @@
package com.xhpc.general.controller;
import cn.hutool.core.date.DateUtil;
import com.xhpc.general.service.IXhpcStatisticsExcelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
/**
* @Author HongYun on 2021/11/15
*/
@RestController
@RequestMapping(value = "/statistics")
public class XhpcStatisticsExcelController {
@Autowired
private IXhpcStatisticsExcelService iXhpcStatisticsExcelService;
@GetMapping(value = "/downByDay")
public Object downByDay(HttpServletResponse response) {
String[] titles = {"时段", " 电量(度)", "充电时长(小时)", "充电次数(次)", "电费(元)", "服务费(元)"
, "订单总金额(元)", "抵扣的总金额(元)", "用户支付的金额(元)", "运营商电费(元)", "运营商服务费(元)"
, "流量方总金额抽成(元)", "流量方服务费抽成(元)", "平台总金额抽成(元)", "平台服务费抽成(元)", "运维总金额抽成(元)", "运维服务费抽成(元)"};
String[] keys = {"time", "chargingDegree", "chargingTime", "chargingNumber", "powerPrice", "servicePrice", "totalPrice"
, "promotionDiscount", "actPrice", "actPowerPrice", "actServicePrice", "internetCommission", "internetSvcCommission"
, "platformCommission", "platformSvcCommission", "operationCommission", "operationSvcCommission"};
response.setContentType("application/x-xls");
try {
ServletOutputStream out = response.getOutputStream();
try {
String name = DateUtil.format(new Date(), "yyyyMMddHHmmss");
response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(name + ".xls", "UTF-8"));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
iXhpcStatisticsExcelService.exportByDay(out, titles, keys);
return "success";
} catch (Exception e) {
e.printStackTrace();
return "导出信息失败";
}
}
}

View File

@ -1,17 +0,0 @@
package com.xhpc.general.service;
import javax.servlet.ServletOutputStream;
import java.util.List;
import java.util.Map;
/**
* @Author HongYun on 2021/11/15
*/
public interface IXhpcStatisticsExcelService {
void export(ServletOutputStream out, String[] titles, String[] keys, List<Map<String, Object>> data) throws Exception;
void exportByDay(ServletOutputStream out, String[] titles, String[] keys) throws Exception;
}

View File

@ -1,80 +0,0 @@
package com.xhpc.general.service;
import com.xhpc.general.mapper.XhpcStatisticsExcelMapper;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.ServletOutputStream;
import java.util.List;
import java.util.Map;
/**
* @Author HongYun on 2021/11/15
*/
@Service
public class XhpcStatisticsExcelServiceImpl implements IXhpcStatisticsExcelService {
@Autowired
private XhpcStatisticsExcelMapper xhpcStatisticsExcelMapper;
@Override
public void exportByDay(ServletOutputStream out, String[] titles, String[] keys) throws Exception {
List<Map<String, Object>> data = xhpcStatisticsExcelMapper.selectByDay();
export(out, titles, keys, data);
}
public void export(ServletOutputStream out, String[] titles, String[] keys, List<Map<String, Object>> data) throws Exception {
try {
//Making the head.
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet hssfSheet = workbook.createSheet("sheet1");
HSSFRow row = hssfSheet.createRow(0);
HSSFCellStyle hssfCellStyle = workbook.createCellStyle();
hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
hssfCellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
hssfCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFCell hssfCell;
for (int i = 0; i < titles.length; i++) {
hssfCell = row.createCell(i);
hssfCell.setCellValue(titles[i]);
hssfCell.setCellStyle(hssfCellStyle);
hssfSheet.setColumnWidth(i, hssfCell.getStringCellValue().getBytes().length * 256 + 200);
}
//Loading data.
HSSFRow dataRow;
HSSFCell dataCell;
int valuesLength = keys.length;
for (int i = 1; i <= data.size(); i++) {
Map<String, Object> map = data.get(i - 1);
if (null != map) {
dataRow = hssfSheet.createRow(i);
for (int j = 0; j < valuesLength; j++) {
if (null != map.get(keys[j])) {
dataCell = dataRow.createCell(j);
String s = String.valueOf(map.get(keys[j]));
dataCell.setCellValue(s);
}
}
}
}
// Exporting to the client.
try {
workbook.write(out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
throw new Exception("导出信息失败!");
}
}
}

View File

@ -101,6 +101,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
</dependencies>
<build>

View File

@ -1,8 +1,8 @@
package com.xhpc.order.controller;
import cn.hutool.core.date.DateUtil;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.domain.BaseEntity;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.order.service.IXhpcStatisticsService;
import io.swagger.annotations.Api;
@ -12,6 +12,11 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;
@ -64,7 +69,6 @@ public class XhpcStatisticsController extends BaseController {
List<Map<String, Object>> list = xhpcStatisticsService.getDateIntervalPage(chargingStationIds,internetUserId,operatorId,startTime,endTime,userId,type);
return getDataTable(list);
}
/**
* 电站统计
* @param chargingStationIds 电站集合
@ -197,11 +201,43 @@ public class XhpcStatisticsController extends BaseController {
* @return
*/
@GetMapping("/getTerminalList")
public AjaxResult getTerminalList(Long chargingStationId, @RequestParam("userId") Long userId, @RequestParam("type")Integer type)
{
if (type == null || type==3) {
public AjaxResult getTerminalList(Long chargingStationId, @RequestParam("userId") Long userId, @RequestParam("type") Integer type) {
if (type == null || type == 3) {
return AjaxResult.success();
}
return xhpcStatisticsService.getTerminalList(chargingStationId, userId, type);
}
/**
* export excel by date.
*
* @param response
* @return
*/
@GetMapping(value = "/exportExcelByDate")
public Object exportExcelByDate(HttpServletResponse response, String chargingStationIds, Long internetUserId, Long operatorId, String startTime, String endTime, @RequestParam("userId") Long userId, @RequestParam("type") Integer type) {
String[] titles = {"时段", " 电量(度)", "充电时长(小时)", "充电次数(次)", "电费(元)", "服务费(元)"
, "订单总金额(元)", "抵扣的总金额(元)", "用户支付的金额(元)", "运营商电费(元)", "运营商服务费(元)"
, "流量方总金额抽成(元)", "流量方服务费抽成(元)", "平台总金额抽成(元)", "平台服务费抽成(元)", "运维总金额抽成(元)", "运维服务费抽成(元)"};
String[] keys = {"createTime", "chargingDegree", "chargingTime", "chargingNumber", "powerPrice", "servicePrice", "totalPrice"
, "promotionDiscount", "actPrice", "actPowerPrice", "actServicePrice", "internetCommission", "internetSvcCommission"
, "platformCommission", "platformSvcCommisssion", "operationCommission", "operationSvcCommission"};
response.setContentType("application/x-xls");
try {
ServletOutputStream out = response.getOutputStream();
try {
String name = DateUtil.format(new Date(), "yyyyMMddHHmmss");
response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(name + ".xls", "UTF-8"));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
List<Map<String, Object>> data = xhpcStatisticsService.getDateIntervalPage(chargingStationIds, internetUserId, operatorId, startTime, endTime, userId, type);
xhpcStatisticsService.exportByDay(out, titles, keys, data);
return "success";
} catch (Exception e) {
e.printStackTrace();
return "导出信息失败";
}
}
}

View File

@ -2,6 +2,7 @@ package com.xhpc.order.mapper;
import com.xhpc.order.domain.XhpcStatisticsStation;
import com.xhpc.order.domain.XhpcStatisticsTimeInterval;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@ -12,6 +13,7 @@ import java.util.Map;
* @date 2021/9/1 10:00
* @Version 1.0
*/
@Mapper
public interface XhpcStatisticsServiceMapper {
/**

View File

@ -4,6 +4,7 @@ import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.order.domain.XhpcStatisticsStation;
import com.xhpc.order.domain.XhpcStatisticsTimeInterval;
import javax.servlet.ServletOutputStream;
import java.util.List;
import java.util.Map;
@ -140,8 +141,15 @@ public interface IXhpcStatisticsService {
/**
* 添加订单统计
*
* @param xhpcStatisticsStation
*/
void addStatisticsStation(XhpcStatisticsStation xhpcStatisticsStation);
void export(ServletOutputStream out, String[] titles, String[] keys, List<Map<String, Object>> data) throws Exception;
void exportByDay(ServletOutputStream out, String[] titles, String[] keys, List<Map<String, Object>> data) throws Exception;
void exportByDate(ServletOutputStream out, String[] titles, String[] keys, List<Map<String, Object>> data) throws Exception;
}

View File

@ -7,9 +7,12 @@ import com.xhpc.order.domain.XhpcStatisticsStation;
import com.xhpc.order.domain.XhpcStatisticsTimeInterval;
import com.xhpc.order.mapper.XhpcStatisticsServiceMapper;
import com.xhpc.order.service.IXhpcStatisticsService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.ServletOutputStream;
import java.util.*;
/**
@ -303,5 +306,66 @@ public class XhpcStatisticsServiceImpl implements IXhpcStatisticsService {
// System.out.println(">>>>>>>>>"+beginOfDay.toString());
// }
@Override
public void exportByDay(ServletOutputStream out, String[] titles, String[] keys, List<Map<String, Object>> data) throws Exception {
export(out, titles, keys, data);
}
@Override
public void exportByDate(ServletOutputStream out, String[] titles, String[] keys, List<Map<String, Object>> data) throws Exception {
export(out, titles, keys, data);
}
public void export(ServletOutputStream out, String[] titles, String[] keys, List<Map<String, Object>> data) throws Exception {
try {
//Making the head.
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet hssfSheet = workbook.createSheet("sheet1");
HSSFRow row = hssfSheet.createRow(0);
HSSFCellStyle hssfCellStyle = workbook.createCellStyle();
hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
hssfCellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
hssfCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFCell hssfCell;
for (int i = 0; i < titles.length; i++) {
hssfCell = row.createCell(i);
hssfCell.setCellValue(titles[i]);
hssfCell.setCellStyle(hssfCellStyle);
hssfSheet.setColumnWidth(i, hssfCell.getStringCellValue().getBytes().length * 256 + 200);
}
//Loading data.
HSSFRow dataRow;
HSSFCell dataCell;
int valuesLength = keys.length;
for (int i = 1; i <= data.size(); i++) {
Map<String, Object> map = data.get(i - 1);
if (null != map) {
dataRow = hssfSheet.createRow(i);
for (int j = 0; j < valuesLength; j++) {
if (null != map.get(keys[j])) {
dataCell = dataRow.createCell(j);
String s = String.valueOf(map.get(keys[j]));
dataCell.setCellValue(s);
}
}
}
}
// Exporting to the client.
try {
workbook.write(out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
throw new Exception("导出信息失败!");
}
}
}