vue word预览,excel预览,pdf预览

淩亂°似流年 2022-11-15 11:13 817阅读 0赞

一、word预览–mammoth.js

安装:npm install --save mammoth

tip:只能预览.docx文件

word.vue:

  1. <template>
  2. <div>
  3. <div id="wordView" v-html="vHtml" />
  4. </div>
  5. </template>
  6. <script>
  7. import mammoth from "mammoth";
  8. export default {
  9. name: "word",
  10. data() {
  11. return {
  12. vHtml: "",
  13. wordURL:''//文件地址
  14. };
  15. },
  16. created() {
  17. let vm=this;
  18. vm.wordURL = this.$route.query.wordURL;
  19. const xhr = new XMLHttpRequest();
  20. xhr.open("get", this.wordURL, true);
  21. xhr.responseType = "arraybuffer";
  22. xhr.onload = function () {
  23. if (xhr.status == 200) {
  24. mammoth
  25. .convertToHtml({ arrayBuffer: new Uint8Array(xhr.response) })
  26. .then(function (resultObject) {
  27. vm.$nextTick(() => {
  28. // document.querySelector("#wordView").innerHTML =
  29. // resultObject.value;
  30. vm.vHtml=resultObject.value;
  31. });
  32. });
  33. }
  34. };
  35. xhr.send();
  36. },
  37. methods: {
  38. },
  39. };
  40. </script>

跳转带的参数:

  1. if (row.fileType == 3) {
  2. //word
  3. let routeData = this.$router.resolve({
  4. path: "/word",
  5. query: {
  6. wordURL: response.data,
  7. },
  8. });
  9. window.open(routeData.href, "_blank");
  10. } else if (row.fileType == 5) {
  11. //pdf
  12. window.open(response.data);
  13. } else if (row.fileType == 4) {
  14. //excel
  15. let routeData = this.$router.resolve({
  16. path: "/excel",
  17. query: {
  18. excelURL: response.data,
  19. },
  20. });
  21. window.open(routeData.href, "_blank");
  22. }

二、excel预览–sheetjs

安装:npm install --save xlsx

excel.vue:

  1. <template>
  2. <div id="table">
  3. <el-table :data="tableData" style="width: 100%">
  4. <el-table-column
  5. v-for="(value,key,index) in tableData[2]"
  6. :key="index"
  7. :prop="key"
  8. :label="key"
  9. ></el-table-column>
  10. </el-table>
  11. </div>
  12. </template>
  13. <script>
  14. import XLSX from "xlsx";
  15. export default {
  16. data() {
  17. return {
  18. tableData: [],
  19. workbook: {},
  20. excelURL: "",
  21. };
  22. },
  23. mounted() {
  24. this.excelURL = this.$route.query.excelURL;
  25. this.readWorkbookFromRemoteFile(this.excelURL);
  26. },
  27. methods: {
  28. readWorkbookFromRemoteFile: function (url) {
  29. var xhr = new XMLHttpRequest();
  30. xhr.open("get", url, true);
  31. xhr.responseType = "arraybuffer";
  32. let _this = this;
  33. xhr.onload = function (e) {
  34. if (xhr.status === 200) {
  35. var data = new Uint8Array(xhr.response);
  36. var workbook = XLSX.read(data, { type: "array" });
  37. console.log(workbook);
  38. var sheetNames = workbook.SheetNames; // 工作表名称集合
  39. _this.workbook = workbook;
  40. _this.getTable(sheetNames[0]);
  41. }
  42. };
  43. xhr.send();
  44. },
  45. getTable(sheetName) {
  46. console.log(sheetName);
  47. var worksheet = this.workbook.Sheets[sheetName];
  48. this.tableData = XLSX.utils.sheet_to_json(worksheet);
  49. console.log(this.tableData);
  50. },
  51. },
  52. };
  53. </script>
  54. <style lang="stylus" scoped>
  55. #table {
  56. font-family: 'Avenir', Helvetica, Arial, sans-serif;
  57. -webkit-font-smoothing: antialiased;
  58. -moz-osx-font-smoothing: grayscale;
  59. color: #2c3e50;
  60. margin-top: 60px;
  61. border: 1px solid #ebebeb;
  62. padding: 20px;
  63. width: 80%;
  64. margin: 20px auto;
  65. border-shadow: 0 0 8px 0 rgba(232, 237, 250, 0.6), 0 2px 4px 0 rgba(232, 237, 250, 0.5);
  66. border-radius: 10px;
  67. overflow: scroll;
  68. height: 100%;
  69. .tab {
  70. margin: 0 0 20px 0;
  71. display: flex;
  72. flex-direction: row;
  73. }
  74. }
  75. </style>
  76. <style scoped>
  77. .is-active {
  78. background-color: #409eff;
  79. }
  80. span {
  81. background-color: red;
  82. }
  83. </style>

三、pdf预览——vue-pdf

安装:npm install --save vue-pdf

  1. <template>
  2. <div>
  3. <pdf ref="pdf" :src="pdfUrl" style="width: 100%;" />
  4. </div>
  5. </template>
  6. <script>
  7. import pdf from 'vue-pdf'
  8. export default {
  9. data(){
  10. return: {
  11. pdfUrl: '',
  12. }
  13. }
  14. created() {
  15. const path = 'test.pdf'// 你获取到的pdf路径
  16. // pdf.createLoadingTask解决文件件跨域问题
  17. this.pdfUrl = pdf.createLoadingTask(path)
  18. },
  19. }
  20. </script>

发表评论

表情:
评论列表 (有 0 条评论,817人围观)

还没有评论,来说两句吧...

相关阅读

    相关 pdf

    > 首先下载pdf.js插件,放到项目里面 > > 官网下载地址:[http://mozilla.github.io/pdf.js/getting\_started/\dow