Browse Source

多sheet Excel导出添加动态字典值

develop
gaoguangchao 4 months ago
parent
commit
6b771d61c6
  1. 3
      ruoyi-common/src/main/java/com/ruoyi/common/utils/DictUtils.java
  2. 6
      ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelMultipleSheetsUtil.java
  3. 42
      ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
  4. 42
      ruoyi-ui/src/utils/dateUtils.js
  5. 93
      ruoyi-ui/src/views/deviceManage/deviceOnline/index.vue
  6. 4
      zc-business/src/main/java/com/zc/business/domain/OnlineSum.java

3
ruoyi-common/src/main/java/com/ruoyi/common/utils/DictUtils.java

@ -1,5 +1,6 @@
package com.ruoyi.common.utils; package com.ruoyi.common.utils;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import com.ruoyi.common.constant.Constants; import com.ruoyi.common.constant.Constants;
@ -43,7 +44,7 @@ public class DictUtils
{ {
return StringUtils.cast(cacheObj); return StringUtils.cast(cacheObj);
} }
return null; return new ArrayList<>();
} }
/** /**

6
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelMultipleSheetsUtil.java

@ -8,6 +8,7 @@ import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.Excel; import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.utils.DictUtils; import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.util.IOUtils; import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
@ -242,10 +243,10 @@ public class ExcelMultipleSheetsUtil {
* @param value * @param value
*/ */
private void handleAnnotationAndSetValue(Cell cell, Field field, Object value) { private void handleAnnotationAndSetValue(Cell cell, Field field, Object value) {
if (field.isAnnotationPresent(Excel.class) && field.getAnnotation(Excel.class).dictType().length() > 0) { if (StringUtils.isNotNull(value)&&field.isAnnotationPresent(Excel.class) && field.getAnnotation(Excel.class).dictType().length() > 0) {
value = DictUtils.getDictLabel(field.getAnnotation(Excel.class).dictType(), String.valueOf(value)); value = DictUtils.getDictLabel(field.getAnnotation(Excel.class).dictType(), String.valueOf(value));
} }
if (field.isAnnotationPresent(Excel.class) && StrUtil.isNotEmpty(field.getAnnotation(Excel.class).dateFormat())) { if (StringUtils.isNotNull(value)&&field.isAnnotationPresent(Excel.class) && StrUtil.isNotEmpty(field.getAnnotation(Excel.class).dateFormat())) {
value = DateUtil.format(Convert.convert(Date.class, value), field.getAnnotation(Excel.class).dateFormat()); value = DateUtil.format(Convert.convert(Date.class, value), field.getAnnotation(Excel.class).dateFormat());
} }
cell.setCellValue(ObjectUtil.isEmpty(value) ? null : value.toString()); cell.setCellValue(ObjectUtil.isEmpty(value) ? null : value.toString());
@ -333,5 +334,4 @@ public class ExcelMultipleSheetsUtil {
return styles; return styles;
} }
} }

42
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java

@ -623,41 +623,41 @@ public class ExcelUtil<T>
// 写入各条记录,每条记录对应excel表中的一行 // 写入各条记录,每条记录对应excel表中的一行
Map<String, CellStyle> styles = new HashMap<String, CellStyle>(); Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
CellStyle style = wb.createCellStyle(); CellStyle style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER); style.setAlignment(HorizontalAlignment.CENTER);//单元格水平对齐方式
style.setVerticalAlignment(VerticalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.CENTER);//单元格垂直对齐方式
Font titleFont = wb.createFont(); Font titleFont = wb.createFont();
titleFont.setFontName("Arial"); titleFont.setFontName("Arial");//字体
titleFont.setFontHeightInPoints((short) 16); titleFont.setFontHeightInPoints((short) 16);//字号
titleFont.setBold(true); titleFont.setBold(true);//加粗
style.setFont(titleFont); style.setFont(titleFont);//设置字体
styles.put("title", style); styles.put("title", style);
style = wb.createCellStyle(); style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER); style.setAlignment(HorizontalAlignment.CENTER);//单元格水平对齐方式
style.setVerticalAlignment(VerticalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.CENTER);//单元格垂直对齐方式
style.setBorderRight(BorderStyle.THIN); style.setBorderRight(BorderStyle.THIN);//右边框细线
style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());//右边框颜色
style.setBorderLeft(BorderStyle.THIN); style.setBorderLeft(BorderStyle.THIN);//左边框细线
style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());//左边框颜色
style.setBorderTop(BorderStyle.THIN); style.setBorderTop(BorderStyle.THIN);//上边框细线
style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());//上边框颜色
style.setBorderBottom(BorderStyle.THIN); style.setBorderBottom(BorderStyle.THIN); //下边框细线
style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());//下边框颜色
Font dataFont = wb.createFont(); Font dataFont = wb.createFont();
dataFont.setFontName("Arial"); dataFont.setFontName("Arial");//字体
dataFont.setFontHeightInPoints((short) 10); dataFont.setFontHeightInPoints((short) 10);//字号
style.setFont(dataFont); style.setFont(dataFont);//设置字体
styles.put("data", style); styles.put("data", style);
style = wb.createCellStyle(); style = wb.createCellStyle();
style.cloneStyleFrom(styles.get("data")); style.cloneStyleFrom(styles.get("data"));
style.setAlignment(HorizontalAlignment.CENTER); style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());//填充前景色为25%灰色
style.setFillPattern(FillPatternType.SOLID_FOREGROUND); style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Font headerFont = wb.createFont(); Font headerFont = wb.createFont();
headerFont.setFontName("Arial"); headerFont.setFontName("Arial");
headerFont.setFontHeightInPoints((short) 10); headerFont.setFontHeightInPoints((short) 12);
headerFont.setBold(true); headerFont.setBold(true);
headerFont.setColor(IndexedColors.WHITE.getIndex()); headerFont.setColor(IndexedColors.WHITE.getIndex());
style.setFont(headerFont); style.setFont(headerFont);

42
ruoyi-ui/src/utils/dateUtils.js

@ -165,3 +165,45 @@ export function countDays(date1, date2) {
return 0 return 0
} }
} }
/**
* 日期加一天或减一天
* @param dateStr 日期字符串格式为yyyy-MM-dd
* @param numDays 增加或减少的天数负数减正数加
* @returns {string}
*/
export function addOrSubtractDays(date, numDays) {
if(typeof date === 'string'){
const parts = date.split('-');
const year = parseInt(parts[0], 10);
const month = parseInt(parts[1], 10) - 1; // 月份需要减1,因为Date对象月份是从0开始的
const day = parseInt(parts[2], 10);
const date1 = new Date(year, month, day);
date1.setDate(date1.getDate() + numDays);
const yearFormatted = date1.getFullYear();
const monthFormatted = (date1.getMonth() + 1).toString().padStart(2, '0'); // 月份加1,并格式化为两位数
const dayFormatted = date1.getDate().toString().padStart(2, '0'); // 格式化为两位数
return `${yearFormatted}-${monthFormatted}-${dayFormatted}`;
}else if(date instanceof Date){
date.setDate(date.getDate() + numDays);
const yearFormatted = date.getFullYear();
const monthFormatted = (date.getMonth() + 1).toString().padStart(2, '0'); // 月份加1,并格式化为两位数
const dayFormatted = date.getDate().toString().padStart(2, '0'); // 格式化为两位数
return `${yearFormatted}-${monthFormatted}-${dayFormatted}`;
}else {
console.error('参数格式无效!')
return ''
}
}
/**
*
* @param rangeNubmer 范围天数负数表示向前正数表示向后
* @returns {{start: string, end: String}}
*/
export function getDaysRange(rangeNubmer){
if(!(rangeNubmer instanceof Number)) rangeNubmer=Number(rangeNubmer)
const startDate= new Date();
startDate.setDate(startDate.getDate()+rangeNubmer);
return {start:formatDate(startDate),end:formatDate(new Date())}
}

93
ruoyi-ui/src/views/deviceManage/deviceOnline/index.vue

@ -5,9 +5,22 @@
<type-tree @defaultCheck="defaultCheck" @nodeCheck="nodeCheck" :filter="false" :show_checkbox="true" :default_check_first="true"></type-tree> <type-tree @defaultCheck="defaultCheck" @nodeCheck="nodeCheck" :filter="false" :show_checkbox="true" :default_check_first="true"></type-tree>
</el-col> </el-col>
<el-col :span="20"> <el-col :span="20">
<el-button @click="handleExport">导出</el-button> <el-row>
<el-date-picker
style="float: right"
v-model="dateRange"
size="mini"
@change="handleRangePickerChange"
value-format="yyyy-MM-dd HH:mm:ss"
type="daterange"
range-separator="至"
start-placeholder="开始日期"
end-placeholder="结束日期">
</el-date-picker>
<el-button type="primary" style="float: right;margin-right: 10px" size="mini" @click="handleExport">导出</el-button>
</el-row>
<line-chart height="30vh" :show-legend="true" :chart-data="chartData"></line-chart> <line-chart height="30vh" :show-legend="true" :chart-data="chartData"></line-chart>
<el-table :data="tableData" height="45vh" style="width: 100%" @sort-change="handleSortChange"> <el-table :data="tableData" height="42vh" style="width: 100%" @sort-change="handleSortChange">
<el-table-column prop="deviceName" label="设备名称" width="200"></el-table-column> <el-table-column prop="deviceName" label="设备名称" width="200"></el-table-column>
<el-table-column prop="stakeMark" label="桩号"></el-table-column> <el-table-column prop="stakeMark" label="桩号"></el-table-column>
<el-table-column prop="direction" label="方向"> <el-table-column prop="direction" label="方向">
@ -159,6 +172,7 @@
</template> </template>
<script> <script>
import {formatDate,addOrSubtractDays,getDaysRange} from "@/utils/dateUtils";
import TypeTree from "@/views/deviceManage/typeTree"; import TypeTree from "@/views/deviceManage/typeTree";
import LineChart from "@/views/deviceManage/lineChart"; import LineChart from "@/views/deviceManage/lineChart";
import indexLine from "@/views/deviceManage/deviceOnline/indexLine"; import indexLine from "@/views/deviceManage/deviceOnline/indexLine";
@ -184,6 +198,7 @@ export default {
deviceStateOptions: deviceState, deviceStateOptions: deviceState,
logVisible:false, logVisible:false,
deviceType:typeTree, deviceType:typeTree,
dateRange:[],
// //
queryParams: { queryParams: {
pageNum: 1, pageNum: 1,
@ -208,7 +223,9 @@ export default {
} }
}, },
created() { created() {
let rangeDate = getDaysRange(-30);
this.dateRange[0] = rangeDate.start;
this.dateRange[1] = rangeDate.end;
}, },
methods: { methods: {
defaultCheck(types){ defaultCheck(types){
@ -228,30 +245,36 @@ export default {
}) })
}, },
deviceOnlineChart(){ deviceOnlineChart(){
deviceOnlineChart(this.queryParams).then(response => { if(this.dateRange.length > 0){
let data = response.data; this.queryParams.startTime = this.dateRange[0];
let xData ,yData = []; this.queryParams.time = this.dateRange[1];
if(Object.keys(data).length !== 0){ deviceOnlineChart(this.queryParams).then(response => {
xData =Object.keys(data[Object.keys(data)[0]]); let data = response.data;
for(const type in data){ let xData ,yData = [];
let yItem = {} if(Object.keys(data).length !== 0){
yItem.name = this.findType(type).label; xData =Object.keys(data[Object.keys(data)[0]]);
yItem.unit = '%'; for(const type in data){
yItem.data = []; let yItem = {}
let lineObj = data[type]; yItem.name = this.findType(type).label;
for(const time in lineObj){ yItem.unit = '%';
yItem.data.push(lineObj[time].replace('%','')); yItem.data = [];
let lineObj = data[type];
for(const time in lineObj){
yItem.data.push(lineObj[time].replace('%',''));
}
yData.push(yItem);
} }
yData.push(yItem);
} }
} this.chartData.xData = xData;
this.chartData.xData = xData; this.chartData.yData = yData;
this.chartData.yData = yData; })
}) }
}, },
seeLog(deviceId){ seeLog(deviceId){
this.logVisible = true; this.logVisible = true;
this.initQueryDate(); this.initQueryDate();
this.logQueryParams.queryDate=formatDate(new Date(),'yyyy-MM-dd');
this.logQueryParams.deviceId = deviceId; this.logQueryParams.deviceId = deviceId;
this.logQuery(); this.logQuery();
}, },
@ -262,7 +285,6 @@ export default {
}, },
networkLogTable(){ networkLogTable(){
networkLogTable(this.logQueryParams).then(response => { networkLogTable(this.logQueryParams).then(response => {
console.log(response)
this.logTableData = response.rows; this.logTableData = response.rows;
this.logTotal = response.total; this.logTotal = response.total;
}) })
@ -284,30 +306,14 @@ export default {
this.networkLogEcharts() this.networkLogEcharts()
this.networkLogTable() this.networkLogTable()
}, },
initQueryDate(){
const date = new Date();
this.logQueryParams.queryDate = `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`;
},
upDay(){ upDay(){
this.logQueryParams.queryDate = this.addOrSubtractDays(this.logQueryParams.queryDate, -1); this.logQueryParams.queryDate = addOrSubtractDays(this.logQueryParams.queryDate, -1);
this.logQuery(); this.logQuery();
}, },
downDay(){ downDay(){
this.logQueryParams.queryDate = this.addOrSubtractDays(this.logQueryParams.queryDate, 1); this.logQueryParams.queryDate = addOrSubtractDays(this.logQueryParams.queryDate, 1);
this.logQuery(); this.logQuery();
}, },
addOrSubtractDays(dateStr, numDays) {
const parts = dateStr.split('-');
const year = parseInt(parts[0], 10);
const month = parseInt(parts[1], 10) - 1; // 1Date0
const day = parseInt(parts[2], 10);
const date = new Date(year, month, day);
date.setDate(date.getDate() + numDays);
const yearFormatted = date.getFullYear();
const monthFormatted = (date.getMonth() + 1).toString().padStart(2, '0'); // 1
const dayFormatted = date.getDate().toString().padStart(2, '0'); //
return `${yearFormatted}-${monthFormatted}-${dayFormatted}`;
},
findType(typeCode) { findType(typeCode) {
return this.deviceType.find(item => item.value == typeCode) return this.deviceType.find(item => item.value == typeCode)
}, },
@ -316,6 +322,13 @@ export default {
...this.queryParams ...this.queryParams
}, `设备在线率${new Date().getTime()}.xlsx`) }, `设备在线率${new Date().getTime()}.xlsx`)
}, },
handleRangePickerChange(){
if(this.dateRange.length > 0){
this.queryParams.startTime = this.dateRange[0];
this.queryParams.time = this.dateRange[1];
this.deviceOnlineChart();
}
}
} }
} }
</script> </script>

4
zc-business/src/main/java/com/zc/business/domain/OnlineSum.java

@ -21,9 +21,9 @@ public class OnlineSum implements java.io.Serializable {
private String stakeMark;//设备桩号 private String stakeMark;//设备桩号
private String direction;//方向 private String direction;//方向
private String deviceType;//设备类型 private String deviceType;//设备类型
@Excel(name = "设备状态", readConverterExp = "0=异常,1=正常") @Excel(name = "设备状态", dictType = "dc_device_state")
private String deviceStatus;//当前设备状态? private String deviceStatus;//当前设备状态?
@Excel(name = "使用状态", readConverterExp = "0=未使用,1=使用中") @Excel(name = "使用状态", dictType = "dc_device_use")
private String useState; private String useState;
private int totalCount;//当天总次数 private int totalCount;//当天总次数
@Excel(name = "在线率(%)") @Excel(name = "在线率(%)")

Loading…
Cancel
Save