index.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <div v-loading="loading" class="home">
  3. <div v-if="fileType == 'xlsx'" style="overflow-y: scroll;" class="home_content">
  4. <div class="home_content_xlsx" v-html="tableau"></div>
  5. </div>
  6. <div v-else-if="fileType == 'pdf'" style="overflow-y: scroll;" class="home_content">
  7. <pdf v-for="i in numPages" :key="i" :src="pdfSrc" :page="i"></pdf>
  8. </div>
  9. <div v-else-if="fileType == 'docx' || fileType == 'doc'" style="overflow-y: scroll;" class="home_content">
  10. <div ref="file"></div>
  11. </div>
  12. </div>
  13. </template>
  14. <script>
  15. import axios from 'axios'
  16. import * as XLSX from 'xlsx'
  17. import pdf from 'vue-pdf'
  18. const docx = require('docx-preview')
  19. export default {
  20. components: { pdf },
  21. data () {
  22. return {
  23. loading: false,
  24. tableau: null,
  25. fileType: '',
  26. pdfSrc: '',
  27. docxSrc: '',
  28. numPages: 1
  29. }
  30. },
  31. created () {
  32. const { query } = this.$route
  33. if (!query) return
  34. this.loading = true
  35. const { fileName } = query
  36. this.getFileType(fileName)
  37. },
  38. methods: {
  39. getXlxsData (fileName) {
  40. axios.get('./' + fileName, {
  41. responseType: "arraybuffer", // 设置响应体类型为arraybuffer
  42. }).then(({ data }) => {
  43. const workbook = XLSX.read(new Uint8Array(data), { type: "array" }); // 解析数据
  44. const sheet = workbook.Sheets[workbook.SheetNames[0]]; // workbook.SheetNames 下存的是该文件每个工作表名字,这里取出第一个工作表
  45. const csv = XLSX.utils.sheet_to_html(sheet)
  46. this.tableau = this.apendTable(this.csv2table(csv, fileName))
  47. this.loading = false
  48. }).catch(err => {
  49. this.loading = false
  50. console.error(err)
  51. })
  52. },
  53. getDocxData (fileName) {
  54. axios.get('./' + fileName, {
  55. responseType: 'blob'
  56. }).then(({ data }) => {
  57. docx.renderAsync(data, this.$refs.file) // 渲染到页面
  58. this.loading = false
  59. }).catch(err => {
  60. this.loading = false
  61. console.error(err)
  62. })
  63. },
  64. getFileType (dotName) {
  65. if (dotName.includes('pdf')) {
  66. this.fileType = 'pdf'
  67. // this.loadPDF('./' + dotName)
  68. this.getNumPages('./' + dotName)
  69. } else if (dotName.includes('docx') || dotName.includes('doc')) {
  70. this.fileType = 'docx' || 'doc'
  71. this.getDocxData('./' + dotName)
  72. } else if (dotName.includes('xlsx')) {
  73. this.fileType = 'xlsx'
  74. this.getXlxsData('./' + dotName)
  75. }
  76. },
  77. getNumPages (url) {
  78. const loadingTask = pdf.createLoadingTask(url)
  79. loadingTask.promise.then(pdf => {
  80. this.pdfSrc = loadingTask
  81. this.numPages = pdf.numPages
  82. this.loading = false
  83. }).catch((err) => {
  84. this.loading = false
  85. console.error(err)
  86. })
  87. },
  88. csv2table (csv, fileName) {
  89. var html = '';
  90. let str = csv;
  91. let startNo = str.indexOf(`<table>`);
  92. let endNo = str.indexOf(`</table>`);
  93. str = str.substring(startNo, endNo + `</table>`.length);
  94. str = str.replace(/(\b(?:id|t|v)=".*?")/g, '');
  95. if (fileName == './航站三字码对应表.xlsx') {
  96. str = str.replace('<table>', `<table border="1" style="border-collapse:collapse; width: 100%; border:1px solid #666666;line-height: 2;">`);
  97. } else {
  98. str = str.replace('<table>', `<table class="table" border="1" style="border-collapse:collapse; width: 100%; border:1px solid #666666;line-height: 2;">`);
  99. }
  100. html += str;
  101. return html;
  102. },
  103. apendTable (str) {
  104. let html = '';
  105. let startNo = str.indexOf(`<tr>`);
  106. let endNo = str.indexOf(`</tr>`);
  107. let thead = str.substring(startNo, endNo);
  108. let theadh = thead.replaceAll('td', 'th')
  109. str = str.replace(`${thead}</tr>`, `<thead>${theadh}</tr></thead>`)
  110. html += str;
  111. return html;
  112. }
  113. },
  114. }
  115. </script>
  116. <style lang="scss" scoped>
  117. .home {
  118. height: calc(100vh - 80px);
  119. padding: 24px;
  120. &_content {
  121. height: 100%;
  122. width: 100%;
  123. overflow: hidden;
  124. background-color: #fff;
  125. ::v-deep &_xlsx {
  126. .table {
  127. tr {
  128. th {
  129. &:last-child {
  130. display: none;
  131. }
  132. }
  133. td {
  134. &:last-child {
  135. display: none;
  136. }
  137. }
  138. }
  139. }
  140. table {
  141. table-layout: fixed;
  142. thead {
  143. text-align: center;
  144. tr {
  145. // height: 50px;
  146. th {
  147. position: sticky;
  148. top: 0;
  149. background: #fff;
  150. }
  151. }
  152. }
  153. tbody {
  154. text-align: center;
  155. }
  156. }
  157. }
  158. }
  159. }
  160. </style>