https://www.npmjs.com/package/ngx-papaparse
1. instalar con npm
2. agregar al modulo donde se va usar
3. importar e inyectar en componente
import { Papa } from 'ngx-papaparse';
constructor(
public _posts: QuestionaryService,
private router: Router,
private papa: Papa,
) {
el array de datos es tal cual lo recibe con una consulta de api-rest
//31-03-2020
exportCVS(){
var data = JSON.parse(JSON.stringify(this.dataList));
var data = data.map(item=>{
item.category_theme = item.category_theme.description;
item.files = item.files.map(item2=>{
return {
'Index': 'Index: '+ item2.index_content + '\n',
'Content': 'Content body: ' + item2.description,
}
});
item.files = this.papa.unparse( item.files, { header: false });
return {
'Theme': item.title,
'Category_Theme' : item.category_theme,
'Description': item.description,
'Content': item.files,
'Status' : item.status ? 'Active' : 'Inactive',
};
});
this.export( data, "Question"+ "_" + Date.now() );
};
//31-03-2020
async export( dataList:any, nameFile: string ){
try {
let csv = this.papa.unparse( dataList );
var blog = new Blob([csv]);
var a = window.document.createElement("a" );
a.href = window.URL.createObjectURL(blog);
a.download = nameFile + '.csv';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
} catch (e) { console.error('error', e); }
};
No hay comentarios.:
Publicar un comentario