Search code examples
angularcordovacordova-plugins

probelm in downloading .xlsx file from Cordova android app


I am working in angular 8 project. I have one component which process some data & export this data to excel using XLSX.WorkSheet method. When i run my application in browser then it successfully download EXCEL file(.xlsx).

Now i am converting my angular 8 application to Cordova android, all functionality working same as brower view of my angular app but when i tring to Export EXCEL then file not downloading.

Here i show some code of angular project for Export Excel data as below.

import { ExportAsService, ExportAsConfig, SupportedExtensions } from 'ngx-export-as';
import * as XLSX from 'xlsx';
constructor(private exportAsService: ExportAsService) {}

public exportExcel(jsonData: any[], fileName: string): void {
    const ws: XLSX.WorkSheet = XLSX.utils.json_to_sheet(jsonData);
    const wb: XLSX.WorkBook = { Sheets: { 'data': ws }, SheetNames: ['data'] };
    const excelBuffer: any = XLSX.write(wb, { bookType: 'xlsx', type: 'array' });
    var out = XLSX.write(wb, { type: 'base64' });
    var xlsContent = 'data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,' + out;
    this.exportAsService.downloadFromDataURL(fileName, xlsContent);    
}

Using above code i am able to download excel file when click on Excel button in Angular 8 Application in browser. that show in below image download file when click on Excel button

I want same behavior in Android .APK(Cordova)


Solution

  • this.file.writeFile(this.file.dataDirectory, 'filename.xls', data, { replace: true }).then(fileEntry => {
          // Open the xls with the correct OS tools
          this.fileOpener.open(this.file.dataDirectory + 'filename.xls', 'application/vnd.ms-excel');
        })
    

    just add the above code to your service and the xls will be downloaded in app. It works for android app.