我正在从事Angle 8项目。我有一个组件来处理一些数据,并使用XLSX.WorkSheet方法将此数据导出到excel。当我在浏览器中运行我的应用程序时,它将成功下载EXCEL文件(.xlsx)。
现在我正在将我的angular 8应用程序转换为Cordova android,所有功能都与我的angular应用程序的浏览器视图相同,但是当我尝试导出Export EXCEL时,则无法下载文件。
我在下面显示一些导出Excel数据的角度项目代码。
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);
}
使用以上代码,当我在浏览器的Angular 8 Application中单击Excel按钮时,我能够下载excel文件。下图所示
我想要在Android .APK(Cordova)中具有相同的行为
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');
})
只需将以上代码添加到您的服务中,xls将下载到应用程序中。它适用于android应用。