How to export JavaScript array info to csv (on client side)?

前端 未结 29 1682
没有蜡笔的小新
没有蜡笔的小新 2020-11-21 21:55

I know there are lot of questions of this nature but I need to do this using JavaScript. I am using Dojo 1.8 and have all the attribute info in array, which loo

29条回答
  •  孤独总比滥情好
    2020-11-21 22:57

    Here's an Angular friendly version:

      constructor(private location: Location, private renderer: Renderer2) {}
    
      download(content, fileName, mimeType) {
    
        const a = this.renderer.createElement('a');
    
        mimeType = mimeType || 'application/octet-stream';
    
        if (navigator.msSaveBlob) {
    
          navigator.msSaveBlob(new Blob([content], {
            type: mimeType
          }), fileName);
        }
        else if (URL && 'download' in a) {
    
          const id = GetUniqueID();
    
          this.renderer.setAttribute(a, 'id', id);
          this.renderer.setAttribute(a, 'href', URL.createObjectURL(new Blob([content], {
            type: mimeType
          })));
    
          this.renderer.setAttribute(a, 'download', fileName);
    
          this.renderer.appendChild(document.body, a);
    
          const anchor = this.renderer.selectRootElement(`#${id}`);
    
          anchor.click();
    
          this.renderer.removeChild(document.body, a);
        }
        else {
          this.location.go(`data:application/octet-stream,${encodeURIComponent(content)}`);
        }
      };
    

提交回复
热议问题