ExportExcel.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <template>
  2. <div class="ExportExcel">
  3. <el-row style="margin-bottom: 15px;padding-bottom: 15px;border-bottom: 2px solid #EEE8D5;">
  4. <el-col :span="12">
  5. <button class="btn" :class="templateId == 1?'green':''" @click="typeClick(1)">单机数据模板</button>
  6. <button class="btn" :class="templateId == 2?'green':''" @click="typeClick(2)">上网电量及功率、测风塔数据模板</button>
  7. </el-col>
  8. <el-col :span="12">
  9. <el-row justify="end">
  10. <button class="btn green" @click="allExport()">一键导出</button>
  11. </el-row>
  12. </el-col>
  13. </el-row>
  14. <table class="com-table">
  15. <thead>
  16. <tr>
  17. <th>
  18. 风电场
  19. </th>
  20. <th>
  21. <input class='t2' type='checkbox' @click="rqCheck"/>
  22. 日期间隔
  23. </th>
  24. <th>
  25. <input class='t2' type='checkbox' @click="sjCheck"/>
  26. 时间间隔
  27. </th>
  28. <th width="120px">
  29. 操作
  30. </th>
  31. </tr>
  32. </thead>
  33. <el-scrollbar>
  34. <tbody>
  35. <tr v-for="(row, index) of tableData" :key="index">
  36. <td>
  37. {{row.fdc}}
  38. </td>
  39. <td class="excelPickerTd">
  40. <el-date-picker v-model="value1[index]" type="daterange" @change='value1Change(index)' range-separator="至"
  41. start-placeholder="开始日期" end-placeholder="结束日期" class="mr15">
  42. </el-date-picker>
  43. </td>
  44. <td>
  45. <el-input placeholder="请输入时间间隔" type='number' v-model="inputVal[index]" @change='inputValChange(index)'></el-input>
  46. </td>
  47. <td width="120px">
  48. <el-button type='text' style='cursor: pointer;' @click="thisExport(index)">导出
  49. </el-button>
  50. </td>
  51. </tr>
  52. </tbody>
  53. </el-scrollbar>
  54. </table>
  55. </div>
  56. </template>
  57. <script>
  58. import JsZip from 'jszip';
  59. import {
  60. export_blob,
  61. export_more_blob
  62. } from "@tools/excel/Export2Excel.js";
  63. import BASE from "@tools/basicTool.js";
  64. export default {
  65. components: {
  66. JsZip
  67. },
  68. data() {
  69. const that = this;
  70. return {
  71. single:false,
  72. rqChecked:false,
  73. sjChecked:false,
  74. checkList: [],
  75. tableIdArr: [], //放checkbox的id数组
  76. tableData: [],
  77. value1: [],
  78. inputVal: [],
  79. inputInterval: 1800,
  80. options: [],
  81. value2: [],
  82. startTs: [],
  83. endTs: [],
  84. muBan: {
  85. type1: ['数据时间', 'scada风速(m/s)', 'scada功率(kW)', '是否故障(故障=1,不故障=0)', '是否限电(限电=1,不限电=0)'],
  86. type2: [
  87. ['数据时间', '日报上网电量(mWh)'],
  88. ['数据时间', '上网功率(kW)'],
  89. ['数据时间', '轮毂高度平均风速(m/s)', '轮毂高度平均风向(°)', '温度(℃)', '压强(hPa)', '湿度(%)']
  90. ],
  91. },
  92. templateId: 1,
  93. stations: [],
  94. paths: [],
  95. pathName: [],
  96. zipNames: [],
  97. tempArrayS: [],
  98. }
  99. },
  100. created() {
  101. this.tableVal();
  102. },
  103. methods: {
  104. value1Change(index){
  105. if(this.rqChecked){
  106. var date = this.value1[index];
  107. this.value1 = [];
  108. this.tableData.forEach((ele, index) => {
  109. this.value1.push(date);
  110. this.time(index, date);
  111. })
  112. }
  113. },
  114. inputValChange(index){
  115. if(this.sjChecked){
  116. var sj = this.inputVal[index];
  117. this.inputVal = [];
  118. this.tableData.forEach((ele, index) => {
  119. this.inputVal.push(sj);
  120. })
  121. }
  122. },
  123. rqCheck(){
  124. this.rqChecked=event.target.checked
  125. },
  126. sjCheck(){
  127. this.sjChecked=event.target.checked
  128. },
  129. typeClick(a){
  130. this.templateId = a;
  131. },
  132. thisExport(index) {
  133. let that = this;
  134. that.single = true;
  135. that.time(index, that.value1[index]);
  136. that.stations[index] = that.tableData[index].value;
  137. that.allExportF(0, index);
  138. },
  139. tableVal() {
  140. let that = this;
  141. that.API.requestData({
  142. method: "GET",
  143. subUrl: "export/databases",
  144. success(res) {
  145. if (res.code === 200) {
  146. const arr = [];
  147. const arr2 = [];
  148. const arr3 = [];
  149. const arr4 = [];
  150. var data = []; //项目列表
  151. res.data.forEach((item,index) => {
  152. const value = Object.keys(item)[0];
  153. arr.push({
  154. value: value,
  155. label: item[value]
  156. })
  157. arr2.push(value + '/' + item[value]);
  158. arr3.push(value);
  159. arr4.push(item[value]);
  160. data.push({
  161. fdc: item[value],
  162. value: value,
  163. });
  164. that.inputVal.push(1800);
  165. that.checkList.push(index);
  166. })
  167. that.options = arr;
  168. that.value2 = arr2;
  169. that.stations = arr3;
  170. that.zipNames = arr4;
  171. that.tableData = data;
  172. that.defaultValue1();
  173. }
  174. },
  175. });
  176. },
  177. defaultValue1() { //默认时间区间
  178. const end = new Date();
  179. const start = new Date();
  180. start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
  181. /////////////
  182. var nowDate = new Date();
  183. var year = nowDate.getFullYear();
  184. var month = nowDate.getMonth();
  185. var day = nowDate.getDate();
  186. this.tableData.forEach((ele, index) => {
  187. this.value1.push([start, end]);
  188. this.time(index, [start, end]);
  189. })
  190. },
  191. handleCommand(command) {
  192. this.templateId = command;
  193. },
  194. formatJson(filterVal, jsonData) {
  195. return jsonData.map(v => filterVal.map(j => v[j]));
  196. },
  197. allExport() {
  198. this.allExportF(0);
  199. },
  200. allExportF(_index, thisIndex) {
  201. BASE.showLoading({text:'正在导出,请耐心等待...'});
  202. const that = this;
  203. if (that.stations[_index]) {
  204. that.API.requestData({
  205. method: "GET",
  206. subUrl: "export/history/all",
  207. data: {
  208. templateId: that.templateId,
  209. startTs: thisIndex!=undefined ? that.startTs[thisIndex] : that.startTs[_index],
  210. endTs: thisIndex!=undefined ? that.endTs[thisIndex] : that.endTs[_index],
  211. interval: thisIndex!=undefined ? that.inputVal[thisIndex] : that.inputVal[_index],
  212. station: thisIndex!=undefined ? that.stations[thisIndex] : that.stations[_index]
  213. },
  214. success(res) {
  215. if (res.code == 200) {
  216. if (thisIndex!=undefined) {
  217. if (that.templateId == 1) {
  218. that.export(res.data, 0, that.zipNames[thisIndex]);
  219. } else if (that.templateId == 2) {
  220. that.exportMuban2(res.data, 0, that.zipNames[thisIndex]);
  221. }
  222. } else {
  223. if (that.templateId == 1) {
  224. that.export(res.data, 0, that.zipNames[_index], ++_index);
  225. } else if (that.templateId == 2) {
  226. that.exportMuban2(res.data, 0, that.zipNames[_index], ++_index);
  227. }
  228. }
  229. }
  230. }
  231. });
  232. }
  233. },
  234. export (data, _index, zipName, deepIndex) {
  235. const that = this;
  236. let tHeader = that.muBan['type' + that.templateId];
  237. var dataKey = Object.keys(data);
  238. var key = dataKey[_index];
  239. if (key) {
  240. let tempObject = {};
  241. let tempArray = [];
  242. const dataLength = data[key].length;
  243. let filterVal = [];
  244. data[key][0].forEach((pEle, pIndex) => {
  245. for (let i = 0; i < dataLength; i++) {
  246. tempObject[String(i)] = data[key][i][pIndex];
  247. }
  248. tempArray.push(tempObject)
  249. tempObject = {};
  250. })
  251. // 上面的index、nickName、name是tableData里对象的属性key值
  252. data[key][0].forEach((ele, index) => {
  253. filterVal.push(index);
  254. });
  255. that.paths.push(export_blob(tHeader, that.formatJson(filterVal, tempArray))); //传blob
  256. var str = key.split('_');
  257. that.pathName.push(str[0].substring(0,2)+str[1]); //excel的名字
  258. _index++;
  259. if (deepIndex!=undefined) {
  260. setTimeout(() => {
  261. that.export(data, _index, zipName, deepIndex);
  262. }, 500);
  263. if (dataKey.length == _index) { //当长度一致时,向zip塞值
  264. var thisPath = that.paths,
  265. thisPathName = that.pathName;
  266. that.paths = [];
  267. that.pathName = [];
  268. that.zipOut(thisPath, thisPathName, zipName, deepIndex)
  269. }
  270. } else {
  271. setTimeout(() => {
  272. that.export(data, _index, zipName);
  273. }, 500);
  274. if (dataKey.length == _index) { //当长度一致时,向zip塞值
  275. var thisPath = that.paths,
  276. thisPathName = that.pathName;
  277. that.paths = [];
  278. that.pathName = [];
  279. that.zipOut(thisPath, thisPathName, zipName)
  280. }
  281. }
  282. }
  283. },
  284. exportMuban2(data, _index, excelName, deepIndex) {
  285. const that = this;
  286. var dataKey = Object.keys(data);
  287. let tHeader = that.muBan['type' + that.templateId];
  288. var key = dataKey[_index];
  289. if (key) {
  290. let tempObject = {};
  291. let tempArray = [];
  292. const dataLength = data[key].length;
  293. data[key][0].forEach((pEle, pIndex) => {
  294. for (let i = 0; i < dataLength; i++) {
  295. tempObject[String(i)] = data[key][i][pIndex];
  296. }
  297. tempArray.push(tempObject)
  298. tempObject = {};
  299. })
  300. // 上面的index、nickName、name是tableData里对象的属性key值
  301. that.tempArrayS.push(tempArray);
  302. _index++;
  303. if (dataKey.length == that.tempArrayS.length) {
  304. let obj = [];
  305. that.tempArrayS.forEach((ele, index) => {
  306. obj[index] = [];
  307. ele.forEach((ele2, index2) => {
  308. var key = Object.keys(ele2)
  309. var arr = [];
  310. key.forEach((ele3, index3) => {
  311. arr.push(ele2[ele3])
  312. })
  313. obj[index][index2] = arr;
  314. arr = [];
  315. })
  316. })
  317. if (deepIndex!=undefined) {
  318. that.paths.push(export_more_blob(tHeader, obj)); //传blob
  319. that.pathName.push(excelName); //excel的名字
  320. that.tempArrayS = [];
  321. that.allExportF(deepIndex);
  322. } else {
  323. that.paths.push(export_more_blob(tHeader, obj, excelName)); //只渲染一个excel
  324. BASE.closeLoading();
  325. }
  326. }
  327. if (deepIndex!=undefined) {
  328. that.exportMuban2(data, _index, excelName, deepIndex);
  329. if (that.stations.length == deepIndex && _index == 1) { //当长度一致时,向zip塞值,_index只让执行第一次
  330. var thisPath = that.paths,
  331. thisPathName = that.pathName;
  332. that.tempArrayS = [];
  333. that.paths = [];
  334. that.pathName = [];
  335. that.zipOut(thisPath, thisPathName, '风电场_上网电量&功率&测风塔数据')
  336. BASE.closeLoading();
  337. }
  338. }else{
  339. that.exportMuban2(data, _index, excelName);
  340. if (that.stations.length == deepIndex && _index == 1) { //当长度一致时,向zip塞值,_index只让执行第一次
  341. var thisPath = that.paths,
  342. thisPathName = that.pathName;
  343. that.tempArrayS = [];
  344. that.paths = [];
  345. that.pathName = [];
  346. BASE.closeLoading();
  347. }
  348. }
  349. }
  350. },
  351. zipOut(paths, pathName, zipName, deepIndex) {
  352. // paths 所有blob
  353. // pathName 所有excel文件名
  354. // zipName zip的包名
  355. // deepIndex 执行ajax下标,当zipOut方法执行完后,再执行allExportF
  356. const that = this;
  357. var zip = new JsZip();
  358. paths.forEach((item, index) => {
  359. const arr_name = item // 下载文件, 并存成ArrayBuffer对象
  360. const file_name = pathName[index] + '.xlsx' // 获取文件名
  361. zip.file(file_name, arr_name, {
  362. binary: true
  363. }) // 逐个添加文件
  364. })
  365. zip.generateAsync({
  366. type: 'blob',
  367. })
  368. .then(function(content) {
  369. // 下载的文件名
  370. var filename = zipName + '.zip'
  371. // 创建隐藏的可下载链接
  372. var eleLink = document.createElement('a')
  373. eleLink.download = filename
  374. eleLink.style.display = 'none'
  375. // 下载内容转变成blob地址
  376. eleLink.href = URL.createObjectURL(content)
  377. // 触发点击
  378. document.body.appendChild(eleLink)
  379. eleLink.click()
  380. // 然后移除
  381. document.body.removeChild(eleLink)
  382. if (deepIndex) {
  383. that.allExportF(deepIndex); //递归,去执行ajax
  384. if(that.stations.length == deepIndex){
  385. BASE.closeLoading();
  386. }
  387. }else if(that.single){
  388. BASE.closeLoading();
  389. that.single = false;
  390. }
  391. })
  392. },
  393. time(index, val) {
  394. this.startTs[index] = Date.parse(new Date(val[0]));
  395. this.endTs[index] = Date.parse(new Date(val[1]) + 86400000);
  396. },
  397. changeSelect(val) {
  398. this.stations = [];
  399. this.zipNames = [];
  400. val.forEach(ele => {
  401. var key = ele.split('/');
  402. this.stations.push(key[0]);
  403. this.zipNames.push(key[1]);
  404. })
  405. },
  406. },
  407. }
  408. </script>
  409. <style lang="less">
  410. .excelPickerTd input {
  411. flex: 1;
  412. }
  413. .el-date-editor--daterange.mr15{
  414. border: 1px solid rgba(96, 103, 105, 0.2);
  415. height: 33px;
  416. padding: 0 8px;
  417. width: 369px;
  418. input{
  419. background-color: transparent;
  420. color: white;
  421. }
  422. .el-range-separator{
  423. color: white;
  424. }
  425. }
  426. .com-table td{
  427. color: white !important;
  428. }
  429. .t2{top: 2px;position: relative !important;}
  430. </style>