Accomplishing the export of statistics excel.

This commit is contained in:
little-cat-sweet 2021-11-16 16:10:47 +08:00
parent cf6cdc79fa
commit 02e35e9073
6 changed files with 188 additions and 2 deletions

View File

@ -0,0 +1,60 @@
package com.xhpc.general.controller;
import cn.hutool.core.date.DateUtil;
import com.xhpc.general.mapper.XhpcStatisticsExcelMapper;
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;
import java.util.List;
import java.util.Map;
/**
* @Author HongYun on 2021/11/15
*/
@RestController
@RequestMapping(value = "/statistics")
public class XhpcStatisticsExcelController {
@Autowired
private IXhpcStatisticsExcelService iXhpcStatisticsExcelService;
@Autowired
private XhpcStatisticsExcelMapper xhpcStatisticsExcelMapper;
@GetMapping(value = "/downByDay")
public Object test(HttpServletResponse response) {
String[] titles = {"时段(某天)", " 电量(度)", "充电时长(小时)", "充电次数(次)", "电费(元)", "服务费(元)"
, "订单总金额(元)", "电站折扣(元)", "运营商实收电费(元)", "运营商实收服务费(元)", "运营商实收合计(元)", "用户实际支出(元)"
, "流量方总金额抽成(元)", "流量方服务费抽成(元)", "平台总金额抽成(元)", "平台服务费抽成(元)", "运维总金额抽成(元)", "运维服务费抽成(元)"};
String[] keys = {"time", "chargingDegree", "chargingTime", "chargingNumber", "powerPrice", "servicePrice", "totalPrice"
, "promotionDiscount", "actPowerPrice", "actServicePrice", "actTotalPrice", "actPrice", "internetCommission", "internetSvcCommission"
, "platformCommission", "platformSvcCommission", "operationCommission", "operationSvcCommission"};
List<Map<String, Object>> data = xhpcStatisticsExcelMapper.selectByDay();
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, data);
return "success";
} catch (Exception e) {
e.printStackTrace();
return "导出信息失败";
}
}
}

View File

@ -0,0 +1,15 @@
package com.xhpc.general.mapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
import java.util.Map;
/**
* @Author HongYun on 2021/11/15
*/
@Mapper
public interface XhpcStatisticsExcelMapper {
List<Map<String, Object>> selectByDay();
}

View File

@ -1,9 +1,7 @@
package com.xhpc.general.service;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.general.domain.XhpcAgreement;
import org.apache.commons.math3.genetics.Fitness;
import java.util.List;

View File

@ -0,0 +1,16 @@
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 exportByDay(ServletOutputStream out, String[] titles, String[] keys, List<Map<String, Object>> data) throws Exception;
void export(ServletOutputStream out, String[] titles, String[] keys, List<Map<String, Object>> data) throws Exception;
}

View File

@ -0,0 +1,74 @@
package com.xhpc.general.service;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
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 {
public void exportByDay(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("导出信息失败!");
}
}
}

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xhpc.general.mapper.XhpcStatisticsExcelMapper">
<select id="selectByDay" resultType="map">
select date_format(create_time, '%Y-%m-%d') as time, sum(charging_degree) as chargingDegree,
sum(charging_time) as chargingTime, sum(charging_number) as chargingNumber,
sum(power_price) as powerPrice, sum(service_price) as servicePrice,
sum(total_price) as totalPrice, sum(promotion_discount) as promotionDiscount,
sum(act_power_price) as actPowerPrice, sum(act_service_price) as actServicePrice,
sum(act_power_price + act_service_price) as actTotalPrice,
sum(act_price) as actPrice, sum(internet_commission) as internetCommission,
sum(internet_svc_commission) as internetSvcCommission, sum(platform_commission) as platformCommission,
sum(platform_svc_commisssion) as platformSvcCommission, sum(operation_commission) as operationCommission,
sum(operation_svc_commission) as operationSvcCommission
from xhpc_statistics_station
GROUP BY time;
</select>
</mapper>