• vue3实现导出Excel(2)


    第一步终端安装
    npm install js-table2excel# 或者
    yarn add js-table2excel
    第二步引入
     

    1. // 导出需要引入的
    2. import * as XLSX from 'xlsx';
    3. import table2excel from 'js-table2excel'

    第三步创建column数据表单

    1. const column = [
    2. //数据表单
    3. {
    4. title: "序号", //表头名称title
    5. key: "id", //数据
    6. type: "text", //类型
    7. },
    8. {
    9. title: "用户姓名",
    10. key: "name",
    11. type: "text",
    12. },
    13. {
    14. title: "用户昵称",
    15. key: "nickname",
    16. type: "text",
    17. },
    18. {
    19. title: "用户头像",
    20. key: "imgs",
    21. type: 'image',
    22. width: 80,
    23. height: 80,
    24. },
    25. {
    26. title: "手机号",
    27. key: "tel",
    28. type: "text",
    29. width: 130,
    30. height: 80,
    31. },
    32. {
    33. title: "商品名称",
    34. key: "ticketName",
    35. type: "text",
    36. width: 130,
    37. height: 80,
    38. },
    39. {
    40. title: "商品状态",
    41. key: 'sheng',
    42. type: "text",
    43. width: 130,
    44. height: 80,
    45. }
    46. ,
    47. {
    48. title: "支付状态",
    49. key: "shen",
    50. type: "text",
    51. width: 130,
    52. height: 80,
    53. },
    54. {
    55. title: "订单号",
    56. key: "orderNum",
    57. type: "text",
    58. width: 130,
    59. height: 80,
    60. },
    61. {
    62. title: "支付金额",
    63. key: "money",
    64. type: "text",
    65. width: 130,
    66. height: 80,
    67. },
    68. {
    69. title: "商品价格",
    70. key: "ticketPrice",
    71. type: "text",
    72. width: 130,
    73. height: 80,
    74. },
    75. {
    76. title: "支付日期",
    77. key: "pay_time",
    78. type: "text",
    79. width: 130,
    80. height: 80,
    81. },
    82. {
    83. title: "过期日期",
    84. key: "over_time",
    85. type: "text",
    86. width: 130,
    87. height: 80,
    88. },
    89. {
    90. title: "下单日期",
    91. key: "create_time",
    92. type: "text",
    93. width: 130,
    94. height: 80,
    95. },
    96. ];

    第四步进行导出

    1. //将数据转化为字符串(list_data数据是接口数据,把名称换成自己的数据即可)
    2. getCurrentDateTimeString()
    3. let tableDatas = JSON.parse(JSON.stringify(Value_Data.value));
    4. let datas = tableDatas;
    5. table2excel(column, datas, "订单导出" + g); //表单数据名称

     这里的+g是当天时间
    这里的getCurrentDataTimeString()是计算获取当天的时间
    获取当天时间

    1. let g = ''
    2. // 获取到当前时间
    3. function getCurrentDateTimeString() {
    4. const now = new Date();
    5. // 获取当前的年份,并只取后两位
    6. const year = now.getFullYear().toString().slice();
    7. // 获取当前的月份,并确保是两位数(从0开始)
    8. const month = (now.getMonth() + 1).toString().padStart(2, '0');
    9. // 获取当前的日期,并确保是两位数
    10. const day = now.getDate().toString().padStart(2, '0');
    11. // 获取当前的小时,并确保是两位数
    12. const hours = now.getHours().toString().padStart('0');
    13. // 获取当前的分钟,并确保是两位数
    14. const minutes = now.getMinutes().toString().padStart(2, '0');
    15. // 获取当前的秒,并确保是两位数
    16. const seconds = now.getSeconds().toString().padStart(2, '0');
    17. // 拼接成所需的字符串格式
    18. const dateTimeString = `${year}-${month}-${day} ${hours}-${minutes}-${seconds}`;
    19. g = `${year}-${month}-${day}`
    20. return dateTimeString;
    21. }

     完整代码

    1. let g = ''
    2. // 获取到当前时间
    3. function getCurrentDateTimeString() {
    4. const now = new Date();
    5. // 获取当前的年份,并只取后两位
    6. const year = now.getFullYear().toString().slice();
    7. // 获取当前的月份,并确保是两位数(从0开始)
    8. const month = (now.getMonth() + 1).toString().padStart(2, '0');
    9. // 获取当前的日期,并确保是两位数
    10. const day = now.getDate().toString().padStart(2, '0');
    11. // 获取当前的小时,并确保是两位数
    12. const hours = now.getHours().toString().padStart('0');
    13. // 获取当前的分钟,并确保是两位数
    14. const minutes = now.getMinutes().toString().padStart(2, '0');
    15. // 获取当前的秒,并确保是两位数
    16. const seconds = now.getSeconds().toString().padStart(2, '0');
    17. // 拼接成所需的字符串格式
    18. const dateTimeString = `${year}-${month}-${day} ${hours}-${minutes}-${seconds}`;
    19. g = `${year}-${month}-${day}`
    20. return dateTimeString;
    21. }
    22. //导出点击事件
    23. const derive = (i) => {
    24. console.log(i);
    25. const Value_Data = ref([])
    26. // if(Value_Data.value!=''){
    27. if (i == 0) {
    28. // if(Value_Data.value!=''){
    29. if (tableData.value != "") {
    30. Value_Data.value = Data.value
    31. console.log(Data.value);
    32. ElMessage({
    33. message: '导出成功',
    34. type: 'success',
    35. })
    36. data()
    37. // const worksheet = XLSX.utils.json_to_sheet(Value_Data.value);
    38. // const workbook = XLSX.utils.book_new();
    39. // XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1");
    40. // // 生成Excel文件并下载
    41. // XLSX.writeFile(workbook, `订单.xlsx`);
    42. const column = [
    43. //数据表单
    44. {
    45. title: "序号", //表头名称title
    46. key: "id", //数据
    47. type: "text", //类型
    48. },
    49. {
    50. title: "用户姓名",
    51. key: "name",
    52. type: "text",
    53. },
    54. {
    55. title: "用户昵称",
    56. key: "nickname",
    57. type: "text",
    58. },
    59. {
    60. title: "用户头像",
    61. key: "imgs",
    62. type: 'image',
    63. width: 80,
    64. height: 80,
    65. },
    66. {
    67. title: "手机号",
    68. key: "tel",
    69. type: "text",
    70. width: 130,
    71. height: 80,
    72. },
    73. {
    74. title: "商品名称",
    75. key: "ticketName",
    76. type: "text",
    77. width: 130,
    78. height: 80,
    79. },
    80. {
    81. title: "商品状态",
    82. key: 'sheng',
    83. type: "text",
    84. width: 130,
    85. height: 80,
    86. }
    87. ,
    88. {
    89. title: "支付状态",
    90. key: "shen",
    91. type: "text",
    92. width: 130,
    93. height: 80,
    94. },
    95. {
    96. title: "订单号",
    97. key: "orderNum",
    98. type: "text",
    99. width: 130,
    100. height: 80,
    101. },
    102. {
    103. title: "支付金额",
    104. key: "money",
    105. type: "text",
    106. width: 130,
    107. height: 80,
    108. },
    109. {
    110. title: "商品价格",
    111. key: "ticketPrice",
    112. type: "text",
    113. width: 130,
    114. height: 80,
    115. },
    116. {
    117. title: "支付日期",
    118. key: "pay_time",
    119. type: "text",
    120. width: 130,
    121. height: 80,
    122. },
    123. {
    124. title: "过期日期",
    125. key: "over_time",
    126. type: "text",
    127. width: 130,
    128. height: 80,
    129. },
    130. {
    131. title: "下单日期",
    132. key: "create_time",
    133. type: "text",
    134. width: 130,
    135. height: 80,
    136. },
    137. ];
    138. //将数据转化为字符串(list_data数据是接口数据,把名称换成自己的数据即可)
    139. getCurrentDateTimeString()
    140. let tableDatas = JSON.parse(JSON.stringify(Value_Data.value));
    141. let datas = tableDatas;
    142. table2excel(column, datas, "订单导出" + g); //表单数据名称
    143. } else {
    144. ElMessage({
    145. message: '暂无订单可以导出',
    146. type: 'warning',
    147. })
    148. }
    149. } else if (i == 1) {
    150. console.log(tableData.value);
    151. if (search_name.value != "" || time.value != "" || search_tel.value != "" || OrderAll.value != "" || status_z.value != "" || status_e.value != "") {
    152. Value_Data.value = tableData.value
    153. ElMessage({
    154. message: '导出成功',
    155. type: 'success',
    156. })
    157. data()
    158. const column = [
    159. //数据表单
    160. {
    161. title: "序号", //表头名称title
    162. key: "id", //数据
    163. type: "text", //类型
    164. },
    165. {
    166. title: "用户姓名",
    167. key: "name",
    168. type: "text",
    169. },
    170. {
    171. title: "用户昵称",
    172. key: "nickname",
    173. type: "text",
    174. },
    175. {
    176. title: "用户头像",
    177. key: "imgs",
    178. type: 'image',
    179. width: 80,
    180. height: 80,
    181. },
    182. {
    183. title: "手机号",
    184. key: "tel",
    185. type: "text",
    186. width: 130,
    187. height: 80,
    188. },
    189. {
    190. title: "商品名称",
    191. key: "ticketName",
    192. type: "text",
    193. width: 130,
    194. height: 80,
    195. },
    196. {
    197. title: "商品状态",
    198. key: 'sheng',
    199. type: "text",
    200. width: 130,
    201. height: 80,
    202. }
    203. ,
    204. {
    205. title: "支付状态",
    206. key: "shen",
    207. type: "text",
    208. width: 130,
    209. height: 80,
    210. },
    211. {
    212. title: "订单号",
    213. key: "orderNum",
    214. type: "text",
    215. width: 130,
    216. height: 80,
    217. },
    218. {
    219. title: "支付金额",
    220. key: "money",
    221. type: "text",
    222. width: 130,
    223. height: 80,
    224. },
    225. {
    226. title: "商品价格",
    227. key: "ticketPrice",
    228. type: "text",
    229. width: 130,
    230. height: 80,
    231. },
    232. {
    233. title: "支付日期",
    234. key: "pay_time",
    235. type: "text",
    236. width: 130,
    237. height: 80,
    238. },
    239. {
    240. title: "过期日期",
    241. key: "over_time",
    242. type: "text",
    243. width: 130,
    244. height: 80,
    245. },
    246. {
    247. title: "下单日期",
    248. key: "create_time",
    249. type: "text",
    250. width: 130,
    251. height: 80,
    252. },
    253. ];
    254. //将数据转化为字符串(list_data数据是接口数据,把名称换成自己的数据即可)
    255. getCurrentDateTimeString()
    256. let tableDatas = JSON.parse(JSON.stringify(Value_Data.value));
    257. let datas = tableDatas;
    258. table2excel(column, datas, "订单导出" + g); //表单数据名称
    259. } else {
    260. ElMessage({
    261. message: '您还未搜索订单无法导出数据',
    262. type: 'warning',
    263. })
    264. }
    265. } else {
    266. console.log(SelectArr.value);
    267. if (SelectArr.value.length > 0) {
    268. Value_Data.value = SelectArr.value
    269. ElMessage({
    270. message: '导出成功',
    271. type: 'success',
    272. })
    273. data()
    274. const column = [
    275. //数据表单
    276. {
    277. title: "序号", //表头名称title
    278. key: "id", //数据
    279. type: "text", //类型
    280. },
    281. {
    282. title: "用户姓名",
    283. key: "name",
    284. type: "text",
    285. },
    286. {
    287. title: "用户昵称",
    288. key: "nickname",
    289. type: "text",
    290. },
    291. {
    292. title: "用户头像",
    293. key: "imgs",
    294. type: 'image',
    295. width: 80,
    296. height: 80,
    297. },
    298. {
    299. title: "手机号",
    300. key: "tel",
    301. type: "text",
    302. width: 130,
    303. height: 80,
    304. },
    305. {
    306. title: "商品名称",
    307. key: "ticketName",
    308. type: "text",
    309. width: 130,
    310. height: 80,
    311. },
    312. {
    313. title: "商品状态",
    314. key: 'sheng',
    315. type: "text",
    316. width: 130,
    317. height: 80,
    318. }
    319. ,
    320. {
    321. title: "支付状态",
    322. key: "shen",
    323. type: "text",
    324. width: 130,
    325. height: 80,
    326. },
    327. {
    328. title: "订单号",
    329. key: "orderNum",
    330. type: "text",
    331. width: 130,
    332. height: 80,
    333. },
    334. {
    335. title: "支付金额",
    336. key: "money",
    337. type: "text",
    338. width: 130,
    339. height: 80,
    340. },
    341. {
    342. title: "商品价格",
    343. key: "ticketPrice",
    344. type: "text",
    345. width: 130,
    346. height: 80,
    347. },
    348. {
    349. title: "支付日期",
    350. key: "pay_time",
    351. type: "text",
    352. width: 130,
    353. height: 80,
    354. },
    355. {
    356. title: "过期日期",
    357. key: "over_time",
    358. type: "text",
    359. width: 130,
    360. height: 80,
    361. },
    362. {
    363. title: "下单日期",
    364. key: "create_time",
    365. type: "text",
    366. width: 130,
    367. height: 80,
    368. },
    369. ];
    370. //将数据转化为字符串(list_data数据是接口数据,把名称换成自己的数据即可)
    371. getCurrentDateTimeString()
    372. let tableDatas = JSON.parse(JSON.stringify(Value_Data.value));
    373. let datas = tableDatas;
    374. table2excel(column, datas, "订单导出" + g); //表单数据名称
    375. } else {
    376. ElMessage({
    377. message: '请选择你要导出的数据',
    378. type: 'warning',
    379. })
    380. return
    381. }
    382. }
    383. }
    384. // 当前时间
    385. let date = new Date();
    386. // 获取当前时间
    387. let date_time = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`
    388. console.log(Date.parse(new Date(date_time)));


     


     

  • 相关阅读:
    2023济南大学计算机考研信息汇总
    Linux(基于Centos7)(一)
    发布 AI 后,苹果股价创历史新高
    2023年腾讯云双十一活动攻略整理汇总
    【SpringBoot】70、SpringBoot实现MySQL数据库自动备份管理系统
    关于nacos的配置获取失败及服务发现问题的排坑记录
    复古风再现:探索70年代风格的室内设计在当今的复兴之路
    一种计算整数位1个数的新方法
    tar 和 zip 打包压缩命令
    升级openssh后出现问题(kex_exchange_identification: client sent。。。。。)
  • 原文地址:https://blog.csdn.net/weixin_62980497/article/details/141107035