怎么用vue展示.docx文件、excel文件和csv文件内容

工具使用   发布日期:2025年02月19日   浏览次数:288

这篇文章主要介绍了怎么用vue展示.docx文件、excel文件和csv文件内容的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇怎么用vue展示.docx文件、excel文件和csv文件内容文章都会有所收获,下面我们一起来看看吧。

    一、展示word文件内容

    1、安装并引入依赖mammoth

    1. npm install --save mammoth
    1. import mammoth from "mammoth"

    2、页面中使用

    1. <div v-html="content"/>
    1. //根据文件url,以arraybuffer的形式获取docx文件内容,传给插件转成html格式,展示在页面上
    2. var xhr = new XMLHttpRequest()
    3. xhr.open('GET', fileurl, true)
    4. xhr.responseType = 'arraybuffer'
    5. const rhis = this
    6. xhr.onload = function(){
    7. if(xhr.status === 200){
    8. mammoth.convertToHtml({arrayBuffer: new Uint8Array(xhr.response)}).then(function(res){
    9. rhis.$nextTick(()=>{
    10. rhis.content = res.value
    11. })
    12. })
    13. }
    14. }
    15. xhr.send()

    二、展示excel/csv文件内容

    1、安装并引入依赖handsontable、papaparse,excel文件需要安装xlxs

    1. npm install handsontable @handsontable/vue
    2. npm install papaparse
    3. npm install xlsx
    1. import Papa from 'papaparse'
    2. import xlsx from 'xlsx'

    2、公共组件sheet.vue

    1. <template>
    2. <div class="overf">
    3. <div id="table" class="sheet">
    4. <hot-table ref="hot" :data="data" :settings="hotSettings" />
    5. </div>
    6. </div>
    7. </template>
    8. <script>
    9. import { HotTable } from '@handsontable/vue'
    10. // import Handsontable from 'handsontable'
    11. import 'handsontable/dist/handsontable.full.css'
    12. export default {
    13. components: { HotTable },
    14. props: {
    15. data: {
    16. type: Array,
    17. default() {
    18. return []
    19. }
    20. }
    21. },
    22. data() {
    23. return {
    24. hot: null,
    25. hotSettings: {
    26. readOnly: true
    27. // manualColumnResize: true,
    28. // manualRowResize: true,
    29. // minSpareRows: 0
    30. }
    31. }
    32. },
    33. watch: {
    34. data(newValue) {
    35. this.$refs.hot.hotInstance.loadData(newValue)
    36. }
    37. },
    38. created() {
    39. },
    40. methods: {}
    41. }
    42. </script>
    43. <style lang="scss" scoped>
    44. .overf{
    45. height: 300px;
    46. overflow: hidden;
    47. }
    48. .sheet{
    49. height: 100%;overflow: auto;
    50. &>>>#hot-display-license-info{
    51. display:none;
    52. }
    53. }
    54. </style>

    3、页面内引入组件

    1. import sheet from './sheet'
    1. <sheet v-if="isCsv" :data="sheetData" />
    1. data() {
    2. return {
    3. sheetData: [], // sheet
    4. }
    5. },
    1. // csv文件
    2. this.sheetData = []
    3. const rhis = this
    4. Papa.parse(fileurl, {
    5. download: true,
    6. complete: res => {
    7. const arrs = res.data
    8. const lastItem = arrs[arrs.length - 1].every(val => val === '')
    9. lastItem && arrs.pop()
    10. rhis.sheetData = arrs
    11. rhis.isCsv = true
    12. }
    13. })
    1. // excel文件
    2. var xhr2 = new XMLHttpRequest()
    3. xhr2.open('GET', fileurl, true)
    4. xhr2.responseType = 'blob'
    5. const rhis = this
    6. xhr2.onload = function() {
    7. var blob = this.response
    8. var reader = new FileReader()
    9. reader.onload = function(e) {
    10. const wb = xlsx.read(e.target.result, {
    11. type: 'binary'
    12. })
    13. rhis.outputWorkbook(wb) // 处理数据
    14. }
    15. reader.readAsBinaryString(blob)
    16. }
    17. xhr2.send()
    1. // 读取 excel 文件
    2. outputWorkbook(workbook) {
    3. this.sheetData = []
    4. var sheetNames = workbook.SheetNames // 工作表名称集合
    5. sheetNames.forEach(name => {
    6. var worksheet = workbook.Sheets[name] // 只能通过工作表名称来获取指定工作表
    7. var data = xlsx.utils.sheet_to_csv(worksheet)
    8. Papa.parse(data, { // 使用papaparse解析csv数据,并展示在表格中
    9. complete: res => {
    10. const arrs = res.data
    11. // 去除最后的空行
    12. const lastItem = arrs[arrs.length - 1].every(val => val === '')
    13. lastItem && arrs.pop()
    14. this.sheetData = arrs
    15. this.isCsv = true
    16. }
    17. })
    18. })
    19. },

    以上就是怎么用vue展示.docx文件、excel文件和csv文件内容的详细内容,更多关于怎么用vue展示.docx文件、excel文件和csv文件内容的资料请关注九品源码其它相关文章!