Installations
npm install vue-json-excel
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
10.19.0
NPM Version
6.14.8
Score
87.6
Supply Chain
99.1
Quality
76.1
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
Vue (94.44%)
JavaScript (5.56%)
Developer
jecovier
Download Statistics
Total Downloads
3,748,423
Last Day
768
Last Week
5,629
Last Month
43,363
Last Year
734,069
GitHub Statistics
691 Stars
186 Commits
168 Forks
13 Watching
1 Branches
17 Contributors
Package Meta Information
Latest Version
0.3.0
Package Id
vue-json-excel@0.3.0
Unpacked Size
94.52 kB
Size
29.07 kB
File Count
9
NPM Version
6.14.8
Node Version
10.19.0
Total Downloads
Cumulative downloads
Total Downloads
3,748,423
Last day
-36.3%
768
Compared to previous day
Last week
-42.2%
5,629
Compared to previous week
Last month
-11.5%
43,363
Compared to previous month
Last year
-15.6%
734,069
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
JSON to Excel for VUE 2
Download your JSON data as an Excel file directly from the browser. This component is based on the solution proposed on this thread
Important! Extra prompt in Microsoft Excel
The method implemented in this component uses HTML tables to draw the .xls files, Microsoft Excel no longer recognize HTML as native content so a warning message will be displayed before opening the file. The content will be rendered perfectly but the message can't be avoided.
Getting started
Get the package:
1npm install vue-json-excel
Register JsonExcel in your vue app entry point:
1import Vue from "vue"; 2import JsonExcel from "vue-json-excel"; 3 4Vue.component("downloadExcel", JsonExcel);
In your template
1<download-excel :data="json_data"> 2 Download Data 3 <img src="download_icon.png" /> 4</download-excel>
Props List
Name | Type | Description | Default |
---|---|---|---|
data | Array | Data to be exported. | |
fields | Object | Fields inside the JSON Object that you want to export. If none provided, all properties in the JSON will be exported. | |
export-fields (exportFields) | Object | Used to fix the problem with other components that use the variable fields, like vee-validate. exportFields works exactly like fields | |
type | string | Mime type [xls, csv] | xls |
name | string | File name to export. | data.xls |
header | string/Array | Title(s) for the data. Can be a string (one title) or an array of strings (multiple titles). | |
title(deprecated) | string/Array | same as header, title is maintained for retro-compatibility purposes but its use is not recommended due to the conflict with the HTML5 title attribute. | |
footer | string/Array | Footer(s) for the data. Can be a string (one footer) or an array of strings (multiple footers). | |
default-value (defaultValue) | string | Use as fallback when the row has no field values. | '' |
worksheet | string | Name of the worksheet tab. | 'Sheet1' |
fetch | Function | Callback to fetch data before download, if it's set it runs immediately after mouse pressed and before download process. IMPORTANT: only works if no data prop is defined. | |
before-generate | Function | Callback to call a method right before the generate / fetch data, eg:show loading progress | |
before-finish | Function | Callback to call a method right before the download box pops out, eg:hide loading progress | |
stringifyLongNum | Boolean | stringify long number and decimal(solve the problem of loss of digital accuracy), default: false | |
escapeCsv | Boolean | This escapes CSV values in order to fix some excel problems with number fields. But this will wrap every csv data with =" and ", to avoid that you have to set this prop to false. default: True |
Example
1import Vue from "vue"; 2import JsonExcel from "vue-json-excel"; 3 4Vue.component("downloadExcel", JsonExcel); 5 6const app = new Vue({ 7 el: "#app", 8 data: { 9 json_fields: { 10 "Complete name": "name", 11 City: "city", 12 Telephone: "phone.mobile", 13 "Telephone 2": { 14 field: "phone.landline", 15 callback: (value) => { 16 return `Landline Phone - ${value}`; 17 }, 18 }, 19 }, 20 json_data: [ 21 { 22 name: "Tony Peña", 23 city: "New York", 24 country: "United States", 25 birthdate: "1978-03-15", 26 phone: { 27 mobile: "1-541-754-3010", 28 landline: "(541) 754-3010", 29 }, 30 }, 31 { 32 name: "Thessaloniki", 33 city: "Athens", 34 country: "Greece", 35 birthdate: "1987-11-23", 36 phone: { 37 mobile: "+1 855 275 5071", 38 landline: "(2741) 2621-244", 39 }, 40 }, 41 ], 42 json_meta: [ 43 [ 44 { 45 key: "charset", 46 value: "utf-8", 47 }, 48 ], 49 ], 50 }, 51});
In your HTML call it like
1<download-excel 2 class="btn btn-default" 3 :data="json_data" 4 :fields="json_fields" 5 worksheet="My Worksheet" 6 name="filename.xls" 7> 8 Download Excel (you can customize this with html code!) 9</download-excel>
REQUIRED
- json_data: Contains the data you want to export.
- json_fields: You can select what fields to export. Specify nested data and assign labels to the fields. The key is the label, the value is the JSON field. This will export the field data 'as is'. If you need to customize the the exported data you can define a callback function. Thanks to @gucastiliao.
1let json_fields = { 2 // regular field (exported data 'as is') 3 fieldLabel: attributeName, // nested attribute supported 4 // callback function for data formatting 5 anotherFieldLabel: { 6 field: anotherAttributeName, // nested attribute supported 7 callback: (value) => { 8 return `formatted value ${value}`; 9 }, 10 }, 11};
json_fields is a object that represents which columns will be exported. If no object is provided, the component will be use the first object in your data array to extract the keys as columns names. Json field example:
:export-fields="{
'Human friendly name': '_name_field_from_json',
'user's last name': '_last_name_text'
}"
Export CSV
To export JSON as a CSV file, just add the prop type
with a value of "csv":
1<download-excel 2 class="btn btn-default" 3 :data="json_data" 4 :fields="json_fields" 5 type="csv" 6 name="filename.xls" 7> 8 Download CSV (you can customize this with html code!) 9</download-excel>
Multi-line values will appear in a single cell
A single text value in the data that contains newline characters will appear as a single cell in Excel. This avoids the undesired behavior of multi-line values getting split into multiple cells that must be merged before using data filters and pivot tables.
For example:
1<template> 2 <div> 3 <json-excel :data="dataForExcel" /> 4 </div> 5</template> 6<script> 7 import JsonExcel from "@/components/JsonExcel"; 8 9 export default { 10 components: { 11 JsonExcel, 12 }, 13 data: () => { 14 return { 15 dataForExcel: [ 16 { colA: "Hello", colB: "World" }, 17 { 18 colA: "Multi-line", 19 /* Multi-line value: */ 20 colB: 21 "This is a long paragraph\nwith multiple lines\nthat should show in a single cell.", 22 }, 23 { colA: "Another", colB: "Regular cell" }, 24 ], 25 }; 26 }, 27 }; 28</script>
Fetch Data on Demand
In case you need to fetch data from the server, you could use the fetch prop that allows you to define a callback function that is executed when your user click the download button. This function has to return a JSON value containing the data to export. A basic use case is:
1<template> 2 <div id="app"> 3 4 <hr> 5 <h2>Fetch Example</h2> 6 <downloadexcel 7 class = "btn" 8 :fetch = "fetchData" 9 :fields = "json_fields" 10 :before-generate = "startDownload" 11 :before-finish = "finishDownload"> 12 Download Excel 13 </downloadexcel> 14 </div> 15</template> 16 17<script> 18import downloadexcel from "vue-json-excel"; 19import axios from 'axios'; 20 21export default { 22 name: "App", 23 components: { 24 downloadexcel, 25 }, 26 data(){ 27 return { 28 json_fields: { 29 'Complete name': 'name', 30 'Date': 'date', 31 }, 32 } 33 }, //data 34 methods:{ 35 async fetchData(){ 36 const response = await axios.get('https://holidayapi.com/v1/holidays?key=a4b2083b-1577-4acd-9408-6e529996b129&country=US&year=2017&month=09'); 37 console.log(response); 38 return response.data.holidays; 39 }, 40 startDownload(){ 41 alert('show loading'); 42 }, 43 finishDownload(){ 44 alert('hide loading'); 45 } 46 } 47}; 48</script> 49
Using callbacks
when using callback functions in the fields description, you have three option to retrieve data:
- field: 'path.to.nested.property' you can retrieve a specific value using the nested property notation.
1 json_fields: { 2 'Complete name': 'name', 3 'City': 'city', 4 'Telephone': 'phone.mobile', 5 'Telephone 2' : { 6 field: 'phone.landline', 7 callback: (value) => { 8 return `Landline Phone - ${value}`; 9 } 10 }, 11 },
- field: 'define.nested.object' you can retrieve a nested object too.
1 json_fields: {s 2 'Complete name': 'name', 3 'City': 'city', 4 'Telephone': 'phone.mobile', 5 'Telephone 2' : { 6 field: 'phone', 7 callback: (value) => { 8 return `Landline Phone - ${value.landline}`; 9 } 10 }, 11 },
- Or get the whole row if field is undefined.
1 json_fields: { 2 'Complete name': 'name', 3 'City': 'city', 4 'Telephone': 'phone.mobile', 5 'Telephone 2' : { 6 callback: (value) => { 7 return `Landline Phone - ${value.phone.landline}`; 8 } 9 }, 10 },
License
MIT
Status
This project is in an early stage of development. Any contribution is welcome :D
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 existing vulnerabilities detected
Reason
Found 10/17 approved changesets -- score normalized to 5
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 23 are checked with a SAST tool
Score
3.7
/10
Last Scanned on 2024-12-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More