├── .babelrc ├── .github └── workflows │ └── release.yml ├── .gitignore ├── .releaserc.js ├── CHANGELOG.md ├── LICENSE ├── README.md ├── package-lock.json ├── package.json ├── src └── index.js └── webpack.config.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "@babel/preset-env" 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | 8 | jobs: 9 | publish: 10 | if: "! contains(github.event.head_commit.message, '[skip ci]')" 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v1 14 | - uses: bahmutov/npm-install@v1 15 | - name: Build 16 | run: npm run build 17 | - name: Semantic Release 18 | uses: cycjimmy/semantic-release-action@v2 19 | with: 20 | extra_plugins: | 21 | @semantic-release/changelog@3.0.6 22 | @semantic-release/git@7.0.18 23 | env: 24 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 25 | NPM_TOKEN: ${{ secrets.NPM_TOKEN }} 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules/ 3 | dist/ 4 | npm-debug.log 5 | yarn-error.log 6 | 7 | # Editor directories and files 8 | .idea 9 | *.suo 10 | *.ntvs* 11 | *.njsproj 12 | *.sln 13 | -------------------------------------------------------------------------------- /.releaserc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | branch: 'master', 3 | plugins: [ 4 | [ '@semantic-release/commit-analyzer' ], 5 | [ '@semantic-release/release-notes-generator' ], 6 | [ '@semantic-release/changelog', { changelogFile: 'CHANGELOG.md' } ], 7 | [ '@semantic-release/npm', { npmPublish: true } ], 8 | [ '@semantic-release/github' ], 9 | [ '@semantic-release/git', { assets: ['CHANGELOG.md', 'package.json'] } ] 10 | ] 11 | } 12 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## [1.0.1](https://github.com/d2-projects/vue-table-import/compare/v1.0.0...v1.0.1) (2020-11-25) 2 | 3 | 4 | ### Bug Fixes 5 | 6 | * :package: 移除多余的 module 配置 ([47532e8](https://github.com/d2-projects/vue-table-import/commit/47532e83a68f87629d899f33cde17df8cab65ed1)) 7 | 8 | 9 | ### Reverts 10 | 11 | * Revert "fix: :package: 移除多余的 module 配置" ([acbfa83](https://github.com/d2-projects/vue-table-import/commit/acbfa83ca6b3365f8d355c6315e4a739da2aaa73)) 12 | 13 | # 1.0.0 (2020-11-25) 14 | 15 | 16 | ### Bug Fixes 17 | 18 | * :package: 移除多余的 module 配置 ([1983494](https://github.com/d2-projects/vue-table-import/commit/19834948b8b299d8ae40a0cdf48f08f035c86113)) 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 FairyEver 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vue-table-export 2 | 3 | ## externals 4 | 5 | VueTableExport 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@d2-projects/vue-table-import", 3 | "description": "vue plugin to import csv or xlsx table file", 4 | "version": "1.0.1", 5 | "author": "liyang <1711467488@qq.com>", 6 | "private": false, 7 | "license": "MIT", 8 | "main": "dist/vue-table-import.js", 9 | "files": [ 10 | "dist/", 11 | "src/" 12 | ], 13 | "repository": { 14 | "type": "git", 15 | "url": "git+https://github.com/d2-projects/vue-table-import.git" 16 | }, 17 | "scripts": { 18 | "build": "webpack" 19 | }, 20 | "dependencies": { 21 | "papaparse": "^4.6.2", 22 | "vue": "^2.5.11", 23 | "xlsx": "^0.14.1" 24 | }, 25 | "bugs": { 26 | "url": "https://github.com/d2-projects/vue-table-import/issues" 27 | }, 28 | "homepage": "https://github.com/d2-projects/vue-table-import#readme", 29 | "devDependencies": { 30 | "@babel/cli": "^7.10.4", 31 | "@babel/core": "^7.10.4", 32 | "@babel/preset-env": "^7.10.4", 33 | "babel-loader": "^8.1.0", 34 | "clean-webpack-plugin": "^3.0.0", 35 | "terser-webpack-plugin": "^3.0.6", 36 | "webpack": "^4.43.0", 37 | "webpack-cli": "^3.3.12" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable */ 2 | import papa from 'papaparse' 3 | import xlsx from 'xlsx' 4 | 5 | const vueTableImport = { 6 | install (Vue, options) { 7 | Vue.prototype.$import = { 8 | // 导入 csv 9 | csv (file) { 10 | return new Promise((resolve, reject) => { 11 | papa.parse(file, { 12 | header: true, 13 | skipEmptyLines: true, 14 | complete: (results, file) => { 15 | resolve(results) 16 | } 17 | }) 18 | }) 19 | }, 20 | // 导入 xlsx 21 | xlsx (file) { 22 | return new Promise((resolve, reject) => { 23 | const reader = new FileReader() 24 | const fixdata = data => { 25 | let o = '' 26 | let l = 0 27 | const w = 10240 28 | for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w))) 29 | o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w))) 30 | return o 31 | } 32 | const getHeaderRow = sheet => { 33 | const headers = [] 34 | const range = xlsx.utils.decode_range(sheet['!ref']) 35 | let C 36 | const R = range.s.r 37 | for (C = range.s.c; C <= range.e.c; ++C) { 38 | var cell = sheet[xlsx.utils.encode_cell({ c: C, r: R })] 39 | var hdr = 'UNKNOWN ' + C 40 | if (cell && cell.t) hdr = xlsx.utils.format_cell(cell) 41 | headers.push(hdr) 42 | } 43 | return headers 44 | } 45 | reader.onload = e => { 46 | const data = e.target.result 47 | const fixedData = fixdata(data) 48 | const workbook = xlsx.read(btoa(fixedData), { type: 'base64' }) 49 | const firstSheetName = workbook.SheetNames[0] 50 | const worksheet = workbook.Sheets[firstSheetName] 51 | const header = getHeaderRow(worksheet) 52 | const results = xlsx.utils.sheet_to_json(worksheet) 53 | resolve({header, results}) 54 | } 55 | reader.readAsArrayBuffer(file) 56 | }) 57 | } 58 | } 59 | } 60 | } 61 | 62 | if (typeof window !== 'undefined' && window.Vue) { 63 | window.Vue.use(vueTableImport) 64 | } 65 | 66 | export default vueTableImport 67 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path') 2 | const TerserPlugin = require('terser-webpack-plugin') 3 | const { CleanWebpackPlugin } = require('clean-webpack-plugin') 4 | 5 | module.exports = { 6 | mode: 'production', 7 | entry: './src/index.js', 8 | output: { 9 | filename: 'vue-table-import.js', 10 | path: path.resolve(__dirname, 'dist'), 11 | // library指定的就是你使用require时的模块名,这里便是require("VueTableImport") 12 | library: 'VueTableImport', 13 | // libraryTarget会生成不同umd的代码,可以只是commonjs标准的,也可以是指amd标准的,也可以只是通过script标签引入的 14 | libraryTarget: 'umd', 15 | // 会对 UMD 的构建过程中的 AMD 模块进行命名。否则就使用匿名的 define 16 | umdNamedDefine: true 17 | }, 18 | module:{ 19 | rules: [ 20 | { 21 | test: /\.js$/, 22 | exclude: /node_modules/, 23 | use: { 24 | loader: 'babel-loader' 25 | } 26 | } 27 | ] 28 | }, 29 | plugins: [ 30 | new CleanWebpackPlugin() 31 | ], 32 | optimization: { 33 | minimize: true, 34 | minimizer: [ 35 | new TerserPlugin({ 36 | sourceMap: true 37 | }) 38 | ] 39 | } 40 | } 41 | --------------------------------------------------------------------------------