1.0.2 • Published 2 years ago

ksc-component-export-files v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

文件导出、导出工具

  • 支持 Excel 、CSV 文件导入
  • 支持 Excel 、CSV、Word 导出

安装

npm i ksc-component-export-files
# 或者 
npm install git+http://newgit.op.ksyun.com/ksc-cns-fe/ksc-component-export-files.git --save 

使用

文件导出

var output = [
    ['标题1','标题2','标题3','标题4'],
    ['数据1','数据2','数据3','数据4'],
    ....
    ['数据1N','数据2N','数据3N','数据4N'],
]

// 推荐:方式一 
import { exportFile } from 'ksc-component-export-files'
exportFile(output ,'文件名-' + Date.now()  , "csv" )

// 方式二 
import kscComponentExportFiles from 'ksc-component-export-files';
kscComponentExportFiles(output ,'文件名-' + Date.now()  , "csv" )

文件导入 异步方式

input( type = 'file' 
accept = ".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
 @click = "upload" id = 'uploadFile' ) 
// Html Input
import { importFile } from 'ksc-component-export-files'

const files = document.getElementById('uploadFile') as any 
let reader = new FileReader();
reader.readAsArrayBuffer(files.files[0]) as any 
reader.onload = function( event ){
  // 这里的this 指向 global.this : FileReader
  let result = this.result 
  // let result =  event.target.value  ; // 也可以获取result 
  importFile( result ).then( (data : any  ) =>{
        console.log( data )
    })
}
// Element-ui  
 el-upload(
    action = '#'
    ref = 'upload'
    :on-change="handleChange"
    :on-success = "handleSuccess"
    :on-exceed="handleExceed"
    :limit = '1'
    :file-list="fileList"
    accept = '.csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel'
)   
    el-button 导入列表   

// js 
  handleChange(file , fileList ){
    this.fileList = fileList ;
    const reader = new FileReader();
    reader.readAsBinaryString( file.raw )
    reader.onload = function(  ev ){
        let result = ev.target.result ; 
        importFile( result , {
            type: 'binary',
            cellDates: true ,
        }).then( (data) =>{
            console.log( JSON.stringify(data) )
        })
    }

  },

importFile 方法参数

paramtypedefault
dataany''
optsObject : ParsingOptions{type: 'binary', cellDates: true}}
  // ParsingOptions Params
export interface ParsingOptions{
    /** Input data encoding */
    type?: 'base64' | 'binary' | 'buffer' | 'file' | 'array' | 'string';

    /** Default codepage */
    codepage?: number;

    /**
     * Save formulae to the .f field
     * @default true
     */
    cellFormula?: boolean;

    /**
     * Parse rich text and save HTML to the .h field
     * @default true
     */
    cellHTML?: boolean;

    /**
     * Save number format string to the .z field
     * @default false
     */
    cellNF?: boolean;

    /**
     * Generate formatted text to the .w field
     * @default true
     */
    cellText?: boolean;

    /** Override default date format (code 14) */
    dateNF?: string;

    /**
     * If >0, read the first sheetRows rows
     * @default 0
     */
    sheetRows?: number;

    /**
     * If true, parse calculation chains
     * @default false
     */
    bookDeps?: boolean;

    /**
     * If true, add raw files to book object
     * @default false
     */
    bookFiles?: boolean;

    /**
     * If true, only parse enough to get book metadata
     * @default false
     */
    bookProps?: boolean;

    /**
     * If true, only parse enough to get the sheet names
     * @default false
     */
    bookSheets?: boolean;

    /** If specified, only parse the specified sheets or sheet names */
    sheets?: number | string | Array<number | string>;

    /** If true, plaintext parsing will not parse values */
    raw?: boolean;

    dense?: boolean;
}