vue-office

repository·master·Indexed 26 days ago

https://github.com/501351981/vue-office

A Vue-based component library for previewing office documents, including docx, excel (.xlsx, .xls), pdf, and pptx. It supports Vue 2 and Vue 3, and provides pure JavaScript preview capabilities for use with non-Vue frameworks. The library includes dedicated components such as VueOfficeDocx, VueOfficeExcel, VueOfficePdf, and VueOfficePptx, as well as core JS preview instances (JsDocxPreview and JsExcelPreview) for manual lifecycle management.

Tokens
6.7K
Snippets
6
Records
55
Agent score
90%

What's inside vue-office

  1. Preview files using vue3-demo

    master

    The vue3-demo package provides two methods for file previewing:

    1. Vue Component Preview: Use built-in Vue components to render document previews directly within your Vue application.
    2. Pure JS Preview: Use pure JavaScript methods to handle file previews without relying on Vue component lifecycle management.
  2. Use the vue2.7 preview demo

    master

    The vue2-demo package provides a demonstration of how to preview documents using Vue 2.7. It supports two primary preview methods:

    1. Vue Component Preview: Using dedicated Vue components to render document content within a Vue application.
    2. Pure JS Preview: Using JavaScript logic to handle document previews without relying on Vue component lifecycle methods.
  3. Install vue-office components

    master
    Install the specific component for the file type you wish to preview. All components require vue-demi@0.14.6 as a peer dependency. If you are using Vue 2.6 or below, you must also install @vue/composition-api.
  4. Preview docx files with @vue-office/docx

    master

    Use the VueOfficeDocx component to preview Word documents. The src prop accepts a URL string (network address or relative path), an ArrayBuffer, or a Blob. You can listen to the @rendered event when rendering is complete.

    <template>
        <vue-office-docx
            :src="docx"
            style="height: 100vh;"
            @rendered="rendered"
        />
    </template>
    
    <script>
    import VueOfficeDocx from '@vue-office/docx'
    import '@vue-office/docx/lib/index.css'
    
    export default {
        components:{
            VueOfficeDocx
        },
        data(){
            return {
                docx: 'http://static.shanhuxueyuan.com/test6.docx'
            }
        },
        methods:{
            rendered(){
                console.log("渲染完成")
            }
        }
    }
    </script>
  5. Preview excel files with @vue-office/excel

    master

    Use the VueOfficeExcel component to preview Excel documents (.xlsx, .xls). The src prop accepts a URL string or binary data (ArrayBuffer/Blob). Supported events include @rendered and @error.

    <template>
        <vue-office-excel
            :src="excel"
            style="height: 100vh;"
            @rendered="renderedHandler"
            @error="errorHandler"
        />
    </template>
    
    <script>
    import VueOfficeExcel from '@vue-office/excel'
    import '@vue-office/excel/lib/index.css'
    
    export default {
        components: {
            VueOfficeExcel
        },
        data() {
            return {
                excel: 'http://static.shanhuxueyuan.com/demo/excel.xlsx'
            }
        },
        methods: {
            renderedHandler() {
                console.log("渲染完成")
            },
            errorHandler() {
                console.log("渲染失败")
            }
        }
    }
    </script>
  6. Preview pdf files with @vue-office/pdf

    master

    Use the VueOfficePdf component to preview PDF documents. The src prop accepts a URL string or binary data (ArrayBuffer/Blob). Supported events include @rendered and @error.

    <template>
        <vue-office-pdf
            :src="pdf"
            style="height: 100vh"
            @rendered="renderedHandler"
            @error="errorHandler"
        />
    </template>
    
    <script>
    import VueOfficePdf from '@vue-office/pdf'
    
    export default {
        components: {
            VueOfficePdf
        },
        data() {
            return {
                pdf: 'http://static.shanhuxueyuan.com/test.pdf'
            }
        },
        methods: {
            renderedHandler() {
                console.log("渲染完成")
            },
            errorHandler() {
                console.log("渲染失败")
            }
        }
    }
    </script>
  7. Preview pptx files with @vue-office/pptx

    master

    Use the VueOfficePptx component to preview PowerPoint documents (.pptx). The src prop accepts a URL string or binary data (ArrayBuffer/Blob). Supported events include @rendered and @error.

    <template>
        <vue-office-pptx
            :src="pdf"
            style="height: 100vh"
            @rendered="renderedHandler"
            @error="errorHandler"
        />
    </template>
    
    <script>
    import VueOfficePptx from '@vue-office/pptx'
    
    export default {
        components: {
            VueOfficePptx
        },
        data() {
            return {
                pdf: 'http://****/test.pptx'
            }
        },
        methods: {
            renderedHandler() {
                console.log("渲染完成")
            },
            errorHandler() {
                console.log("渲染失败")
            }
        }
    }
    </script>
  8. Configure JsExcelPreview options

    master

    The options object passed to init or via setOptions() controls the behavior and appearance of the Excel previewer.

    Available Options:

    • xls: Boolean. Set to true if previewing legacy .xls files. Defaults to false.
    • minColLength: Number. Minimum column length. Defaults to 20.
    • showContextmenu: Boolean. Whether to show the context menu. Defaults to false.
    • cellSelected: Function. Callback triggered when a single cell is selected. Receives an object: { cell, rowIndex, columnIndex }.
    • cellsSelected: Function. Callback triggered when a range of cells is selected. Receives an object: { cell, startRowIndex, startColumnIndex, endRowIndex, endColumnIndex }.
    • beforeTransformData: Function. Hook to modify the workbook data before it is processed. Receives the workbook object.
    • transformData: Function. Hook to modify the spreadsheet-compatible data after transformation. Receives workbookData.