I am new to ionic and when I try to export excel nothing is happening. I tried different things but they all lead back to the same result.
here is my code.
component.ts
export class HomePage implements OnInit {
scannedResult: any[] = [];
contentvisibility = 'show';
displayedColumns = ['firstname', 'lastname', 'company', 'email'];
scannedRsult1: any[] = [];
base64result: string;
constructor(private barcodeScanner: BarcodeScanner, private plt: Platform, private fileOpener: FileOpener,
private file: File, private socialSharing: SocialSharing) {
this.storage.create();
this.storage.get('item').then((res) => {
if (res) {
this.scannedResult = res;
alert(this.scannedResult);
}
});
}
async ngOnInit() {
await this.storage.create();
}
startScan() {
this.barcodeScanner.scan().then(barcodeData => {
console.log('Barcode data', barcodeData);
this.scannedResult.push(JSON.parse(barcodeData.text));
// this.setObject(barcodeData);
this.storage.set('item', this.scannedResult)
.then(
() => {
alert('Item Stored Success');
},
error => console.error('Error storing item', error)
);
return barcodeData;
}).catch(err => {
console.log('Error', err);
});
}
createExcel() {
const ws = XLSX.utils.json_to_sheet(this.scannedResult);
// eslint-disable-next-line
const wb = { Sheets: { 'data': ws }, SheetNames: ['data'] };
const buffer = XLSX.write(wb, {
bookType: 'xlsx',
type: 'array'
});
const fileType = 'application/vnd.openxmlformats-officedocument.spreadsheetxml.sheet';
const blob: Blob = new Blob([buffer], { type: fileType });
this.convertFile(blob).subscribe(res=>{
console.log(res);
this.base64result = res;
console.log(this.base64result);
this.saveToPhone(this.base64result);
}, err => {
console.log(err);
});
}
saveToPhone(buffer: any) {
const fileType = 'application/vnd.openxmlformats-officedocument.spreadsheetxml.sheet';
const fileName = 'Visitor_Data';
const fileExtension = '.xlsx';
const currentDate = new Date().toLocaleString().replace(/[,:\s\/]/g, '-');
const name = `myFile-${currentDate}.xlsx`;
console.log(name);
//method one
this.file.writeFile(this.file.dataDirectory, fileName + fileExtension, buffer, {replace: true}).then(res => {
this.fileOpener.open(res.nativeURL, fileType);
console.log(res);
alert('excel file saved in phone');
});
// method two
Filesystem.writeFile({
path: name,
data: buffer,
directory: Directory.Documents,
encoding: Encoding.UTF8
});
}
//conver one
convertFile(file : Blob) : Observable<string> {
const result = new ReplaySubject<string>(1);
const reader = new FileReader();
reader.readAsBinaryString(file);
reader.onload = (event) => result.next(btoa(event.target.result.toString()));
return result;
}
// convert two
blobToBase64(blob: Blob): Observable<string> {
return new Observable<string>(observer => {
const reader = new FileReader();
reader.onerror = observer.error;
reader.onabort = observer.error;
reader.onload = () => observer.next(reader.result as string);
// reader.onloadend = observer.complete.bind(observer);
reader.readAsDataURL(blob);
});
}
}
I tried it with the two methods I mentioned above but they failed to save the excel file to the device.