sheet.vue 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <!--
  2. * @Author : yuanrunwei
  3. * @Date : 2021-12-04 14:23:58
  4. * @LastEditors : yuanrunwei
  5. * @LastEditTime : 2021-12-04 15:38:33
  6. * @FilePath : \spfm-market-front\src\pages\main\performance\components\sheet.vue
  7. -->
  8. <template>
  9. <div class="sheet-container">
  10. <el-upload action :on-change="handleChange" :show-file-list="false">
  11. <el-button size="mini" type="success">上传</el-button>
  12. </el-upload>
  13. <div id="luckysheet" class="sheet-container-block"></div>
  14. </div>
  15. </template>
  16. <script>
  17. import XLSX from "xlsx";
  18. import luckyexcel from "luckyexcel";
  19. export default {
  20. data() {
  21. return {};
  22. },
  23. methods: {
  24. handleInit() {},
  25. handleCreate(file) {
  26. luckyexcel.transformExcelToLucky(file, (export_json) => {
  27. var options = {
  28. container: "luckysheet",
  29. data: export_json.sheets,
  30. title: export_json.info.name,
  31. userInfo: export_json.info.name.creator,
  32. };
  33. window.luckysheet.create(options);
  34. });
  35. },
  36. async handleChange(response) {
  37. this.handleCreate(response.raw);
  38. let binary = await this.handleFile(response.raw);
  39. let workbook = XLSX.read(binary, {
  40. type: "binary",
  41. cellDates: true,
  42. });
  43. let sheet = workbook.Sheets[workbook.SheetNames[0]];
  44. const data = XLSX.utils.sheet_to_json(sheet);
  45. this.handleCreate(data);
  46. },
  47. handleFile(file) {
  48. return new Promise((resolve) => {
  49. let reader = new FileReader();
  50. reader.readAsBinaryString(file);
  51. reader.onload = (event) => {
  52. resolve(event.target.result);
  53. };
  54. });
  55. },
  56. },
  57. mounted() {
  58. this.handleInit();
  59. },
  60. };
  61. </script>
  62. <style lang="scss" scope>
  63. .sheet-container {
  64. position: relative;
  65. width: 1200px;
  66. height: 500px;
  67. &-block {
  68. position: absolute;
  69. width: calc(100% - 40px);
  70. height: 100%;
  71. }
  72. }
  73. </style>