渲染对象到excel

Raod 3 years ago
parent 99ba1e19f3
commit 37ae1e796a

@ -55,4 +55,7 @@ public class DataSetDto extends GaeaBaseDTO implements Serializable {
private Set<String> setParamList;
/**指定字段*/
private String fieldLabel;
}

@ -1,17 +1,24 @@
package com.anjiplus.template.gaea.business.modules.reportexcel.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.anji.plus.gaea.constant.BaseOperationEnum;
import com.anji.plus.gaea.curd.mapper.GaeaBaseMapper;
import com.anji.plus.gaea.exception.BusinessException;
import com.anji.plus.gaea.utils.GaeaAssert;
import com.anji.plus.gaea.utils.GaeaBeanUtils;
import com.anjiplus.template.gaea.business.code.ResponseCode;
import com.anjiplus.template.gaea.business.modules.dataset.controller.dto.DataSetDto;
import com.anjiplus.template.gaea.business.modules.dataset.controller.dto.OriginalDataDto;
import com.anjiplus.template.gaea.business.modules.dataset.service.DataSetService;
import com.anjiplus.template.gaea.business.modules.report.dao.ReportMapper;
import com.anjiplus.template.gaea.business.modules.report.dao.entity.Report;
import com.anjiplus.template.gaea.business.modules.reportexcel.controller.dto.ReportExcelDto;
import com.anjiplus.template.gaea.business.modules.reportexcel.dao.ReportExcelMapper;
import com.anjiplus.template.gaea.business.modules.reportexcel.dao.entity.ReportExcel;
import com.anjiplus.template.gaea.business.modules.reportexcel.service.ReportExcelService;
import com.anjiplus.template.gaea.business.modules.reportexcel.util.XlsSheetUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
@ -20,6 +27,8 @@ import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* TODO
*
@ -34,6 +43,9 @@ public class ReportExcelServiceImpl implements ReportExcelService {
@Autowired
private ReportExcelMapper reportExcelMapper;
@Autowired
private DataSetService dataSetService;
@Autowired
private ReportMapper reportMapper;
@ -93,9 +105,9 @@ public class ReportExcelServiceImpl implements ReportExcelService {
reportExcelDto.setSetParam(setParam);
}
reportExcelDto.setReportName(report.getReportName());
// TODO 数据集解析,待扩展
// JSONObject jsonObject = reportUtil.reportParse(reportExcelDto);
// reportExcelDto.setJsonStr(JSONObject.toJSONString(jsonObject));
// 数据集解析
String jsonStr = analysisReportData(reportExcelDto);
reportExcelDto.setJsonStr(jsonStr);
// reportExcelDto.setTotal(jsonObject.getJSONObject("rows").size());
return reportExcelDto;
}
@ -106,4 +118,106 @@ public class ReportExcelServiceImpl implements ReportExcelService {
return true;
}
/**
*
*/
private String analysisReportData(ReportExcelDto reportExcelDto) {
String jsonStr = reportExcelDto.getJsonStr();
List<JSONObject> dbObjectList = (List<JSONObject>) JSON.parse(jsonStr);
if (dbObjectList != null && dbObjectList.size() > 0) {
for (int x = 0; x < dbObjectList.size(); x++) {
analysisSheet(dbObjectList.get(x));
}
}
return JSONObject.toJSONString(dbObjectList);
}
/**
* sheet
*
* @param dbObject
*/
private void analysisSheet(JSONObject dbObject) {
//data是一个二维数组
if (dbObject.containsKey("data") && null != dbObject.get("data")) {
List<JSONArray> data = (List<JSONArray>) dbObject.get("data");
//行
for (int r = 0; r < data.size(); r++) {
JSONArray jsonArray = data.get(r);
//列
for (int c = 0; c < jsonArray.size(); c++) {
//单元格
JSONObject cell = jsonArray.getJSONObject(c);
if (null != cell && cell.containsKey("v") && StringUtils.isNotBlank(cell.getString("v"))) {
String v = cell.getString("v");
DataSetDto dataSet = getDataSet(v);
if (null != dataSet) {
OriginalDataDto originalDataDto = dataSetService.getData(dataSet);
if (null != originalDataDto.getData()) {
if (originalDataDto.getData().size() == 1) {
//对象
JSONObject jsonObject = originalDataDto.getData().get(0);
String fieldLabel = jsonObject.getString(dataSet.getFieldLabel());
String replace = v.replace("#{".concat(dataSet.getSetCode()).concat(".").concat(dataSet.getFieldLabel()).concat("}"), fieldLabel);
dbObject.getJSONArray("data").getJSONArray(r).getJSONObject(c).put("v", replace);
dbObject.getJSONArray("data").getJSONArray(r).getJSONObject(c).put("m", replace);
} else {
//集合
JSONObject jsonObject = originalDataDto.getData().get(0);
String fieldLabel = jsonObject.getString(dataSet.getFieldLabel());
String replace = v.replace("#{".concat(dataSet.getSetCode()).concat(".").concat(dataSet.getFieldLabel()).concat("}"), fieldLabel);
dbObject.getJSONArray("data").getJSONArray(r).getJSONObject(c).put("v", replace);
dbObject.getJSONArray("data").getJSONArray(r).getJSONObject(c).put("m", replace);
}
}
}
}
}
}
System.out.println("aaaa");
}
}
/**
* #{xxxx.xxxxx}
* @param v
* @return
*/
private DataSetDto getDataSet(String v) {
DataSetDto dto = new DataSetDto();
if (v.contains("#{") && v.contains("}")) {
int start = v.indexOf("#{") + 2;
int end = v.indexOf("}");
if (start < end) {
String substring = v.substring(start, end);
if (substring.contains(".")) {
String[] split = substring.split("\\.");
dto.setSetCode( split[0]);
dto.setFieldLabel(split[1]);
return dto;
}
}
}
return null;
}
}

@ -8,6 +8,7 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;
@ -36,4 +37,18 @@ public class XlsSheetUtilTest {
System.out.println("start");
}
@Test
public void test2(){
List<String> list = new LinkedList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
list.add(2, "f");
//CopyOnWriteArrayList
System.out.println(list);
}
}

@ -67,8 +67,6 @@
</el-button>
</el-tooltip>
</div>
<!-- <div id="x-spreadsheet-demo"
class="excel-designer" /> -->
<div id="luckysheet"
style="margin:0px;padding:0px;position:absolute;width:100%;height:95vh;left: 0px;top: 30px;bottom:0px;" />
<div id="qrCode"
@ -493,7 +491,7 @@ export default {
}
},
async preview () {
var routeUrl = this.$router.resolve({ path: '/report/excelreport/viewer', query: { reportCode: this.reportCode } })
var routeUrl = this.$router.resolve({ path: '/excelreport/viewer', query: { reportCode: this.reportCode } })
window.open(routeUrl.href, '_blank')
},
async queryAllDataSet () {

@ -44,8 +44,8 @@
</div>
</div>
<div class="layout-middle">
<div id="x-spreadsheet-demo"
class="excel-designer" />
<div id="luckysheet"
style="margin:0px;padding:0px;position:absolute;width:100%;height:95vh;left: 0px;top: 30px;bottom:0px;" />
</div>
</div>
</template>
@ -78,43 +78,6 @@ export default {
}
},
mounted () {
this.options = {
mode: 'read', // edit | read
showToolbar: false,
showGrid: true,
showContextmenu: false,
view: {
height: () => document.documentElement.clientHeight,
width: () => document.getElementsByClassName('layout-middle')[0].clientWidth,
},
row: {
len: 100,
height: 25,
},
col: {
len: 52,
width: 100,
indexWidth: 60,
minWidth: 60,
},
style: {
bgcolor: '#ffffff',
align: 'left',
valign: 'middle',
textwrap: false,
strike: false,
underline: false,
color: '#0a0a0a',
font: {
name: 'Helvetica',
size: 10,
bold: false,
italic: false,
},
},
}
// this.sheet = new Spreadsheet('#x-spreadsheet-demo', this.options).loadData({})
this.load()
this.preview()
},
created () {
@ -126,8 +89,6 @@ export default {
this.params.setParam = JSON.stringify(arr)
const { code, data } = await preview(this.params)
if (code !== '200') return
this.excelData = JSON.parse(data.jsonStr)
this.sheet.loadData(this.excelData)
},
async preview () {
this.excelData = {}
@ -149,7 +110,8 @@ export default {
this.tableData2 = extendArry
this.excelData = data.jsonStr
this.sheet.loadData(JSON.parse(this.excelData))
this.sheetData = (data == null ? [{}] : JSON.parse(data.jsonStr))
this.createSheet();
},
download (val) {
const result = {}
@ -176,55 +138,67 @@ export default {
}
return objSecond
},
load () {
const rows10 = { len: 1000 }
for (let i = 0; i < 1000; i += 1) {
rows10[i] = {
cells: {},
}
}
const rows = {}
this.sheet = x_spreadsheet('#x-spreadsheet-demo', this.options)
.loadData([
//
createSheet(){
const options = {
container: 'luckysheet', // DOMid
title: 'Luckysheet Demo', //
lang: 'zh', //
plugins:['chart'],
data:[
{
freeze: 'B3',
styles: [],
merges: [],
cols: {
len: 20,
// 2: { width: 200 },
"name": "report", //
"color": "", //
"index": 0, //
"status": 1, //
"order": 0, //
"hide": 0,//
"row": 36, //
"column": 18, //
"defaultRowHeight": 19, //
"defaultColWidth": 73, //
"celldata": [], //使
"config": {
"merge":{}, //
"rowlen":{}, //
"columnlen":{}, //
"rowhidden":{}, //
"colhidden":{}, //
"borderInfo":{}, //
"authority":{}, //
},
rows,
},
{ name: 'sheet-test', rows: rows10 },
])
.change((cdata) => {
const dataRect = this.sheet.data.getDataRect()
})
this.sheet
.on('cell-selected', (cell, ri, ci) => {
// console.log('cell:', cell, ', ri:', ri, ', ci:', ci);
})
.on('cell-edited', (text, ri, ci) => {
// console.log('text:', text, ', ri: ', ri, ', ci:', ci);
})
this.sheet.on('printSettings', () => {
// .
})
this.sheet.on('generateQrcode', () => {
// .
console.log('insertImg', this.dialogVisible)
})
this.sheet.on('insertImg', () => {
//
})
"scrollLeft": 0, //
"scrollTop": 315, //
"luckysheet_select_save": [], //
"calcChain": [],//
"isPivotTable":false,//
"pivotTable":{},//
"filter_select": {},//
"filter": null,//
"luckysheet_alternateformat_save": [], //
"luckysheet_alternateformat_save_modelCustom": [], //
"luckysheet_conditionformat_save": {},//
"frozen": {}, //
"chart": [], //
"zoomRatio":1, //
"image":[], //
"showGridLines": 1, //线
"dataVerification":{} //
}
]
};
options.data = this.sheetData;
$(function () {
luckysheet.create(options);
});
},
},
}
</script>
<style scoped lang="scss">
@import '../../../../components/x-spreadsheet/dist/xspreadsheet.css';
.download {
width: 100%;
float: left;

Loading…
Cancel
Save