简体   繁体   English

Angular导出Excel客户端

[英]Angular export Excel client side

I'm using Angular v4, i guess how can I build an Excel spreadsheet starting from an object in a component. 我正在使用Angular v4,我想如何从组件中的对象开始构建Excel电子表格。 I need to download the Excel file on the click of a button and I have to do this client side. 我需要点击一个按钮下载Excel文件,我必须做这个客户端。 I have a json file composed of arrays and I need to transfer this on an excel file, possibly customizable in style. 我有一个由数组组成的json文件,我需要在excel文件上传输它,可能是风格可定制的。 Is it possible? 可能吗? If yes, how? 如果有,怎么样?

Edit: No js libraries please, need to do this with Typescript and Angular 编辑:请不要使用js库,需要使用Typescript和Angular来完成

yourdata= jsonData yourdata = jsonData

ConvertToCSV(objArray) {
            var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
              var str = '';
            var row = "";

            for (var index in objArray[0]) {
                //Now convert each value to string and comma-separated
                row += index + ',';
            }
            row = row.slice(0, -1);
            //append Label row with line break
            str += row + '\r\n';

            for (var i = 0; i < array.length; i++) {
                var line = '';
                for (var index in array[i]) {
                    if (line != '') line += ','

                    line += array[i][index];
                }
                str += line + '\r\n';
            }
            return str;
        }

in your html: 在你的HTML中:

<button (click)="download()">export to excel</button>

in component: 在组件中:

download(){    
 var csvData = this.ConvertToCSV(yourdata);
                        var a = document.createElement("a");
                        a.setAttribute('style', 'display:none;');
                        document.body.appendChild(a);
                        var blob = new Blob([csvData], { type: 'text/csv' });
                        var url= window.URL.createObjectURL(blob);
                        a.href = url;
                        a.download = 'User_Results.csv';/* your file name*/
                        a.click();
                        return 'success';
}

Hope you it will help you 希望你能帮助你

You may not like this answer based on your requirements; 根据您的要求,您可能不喜欢这个答案; however, without a JS library you are re-inventing the wheel. 但是,如果没有JS库,你就会重新发明轮子。 I've personally found Excel XML formats to be mind boggling, with the library below, (callable from within Angular), you can benefit from the work already done on that front. 我个人发现Excel XML格式令人难以置信,使用下面的库(可从Angular中调用),您可以从已经完成的工作中受益。

There are two parts to your design, the download and the opening/editing of the content, neither are Angular related as Angular is just the container to do the work. 您的设计有两个部分,即内容的下载和打开/编辑,Angular也不是相关的,因为Angular只是完成工作的容器。

  1. Search for how to download files within Angular 搜索如何在Angular中下载文件
  2. Once the download has happened, and the file is now "local" open it up using something like this 下载完成后,文件现在是“本地”,使用类似的东西打开它

I think you will not get that done without js libraries in the background. 我想你不会在没有js库的情况下完成这项工作。 What you need are the typings for utilizing it in your Angular project with typescript. 你需要的是在你的Angular项目中使用打字稿的类型。

For creating an excel file you could use something like exceljs . 要创建excel文件,您可以使用exceljs之类的东西 To use it in your project also install the typings you can find here . 要在项目中使用它,还要安装此处可以找到的类型。 I am not sure if this library fits... haven't used it before. 我不确定这个库是否合适......以前没用过它。

For downloading you should use FileSaver.js (I have already used it). 要下载,您应该使用FileSaver.js (我已经使用过它)。

npm install file-saver --save

... and the typings: ......和打字:

npm install @types/file-saver --save-dev

For using FileSaver.js put the following import to your component: 对于使用FileSaver.js,将以下导入放入组件:

import * as FileSaver from 'file-saver';

To trigger the download use that: 要触发下载,请使用:

FileSaver.saveAs(fileData, "filename.xlsx")

'fileData' has to be a Blob. 'fileData'必须是Blob。

Hope this helps a little. 希望这有所帮助。

Vishwanath answer was working for me when i replaced "," with ";". 当我用“;”代替“,”时,Vishwanath的回答对我有用。 In Typescript the implementation could look like this: 在Typescript中,实现可能如下所示:

ConvertToCSV(objArray: any) {
    const array = typeof objArray !== 'object' ? JSON.parse(objArray) : objArray;
    let str = '';
    let row = '';

    for (const index of Object.keys(objArray[0])) {
      row += `${index};`;
    }
    row = row.slice(0, -1);
    str += `${row}\r\n`;

    for (let i = 0; i < array.length; i++) {
      let line = '';
      for (const index of Object.keys(array[i])) {
        if (line !== '') {
          line += ';';
        }
        line += array[i][index];
      }
      str += `${line}\r\n`;
    }
    return str;
  }

I hope this helps someone. 我希望这可以帮助别人。

it not is possible. 这是不可能的。

XLS is a binary format. XLS是二进制格式。

The project Apache POI( https://en.wikipedia.org/wiki/Apache_POI ) name class as HSSF (Horrible SpreadSheet Format). 项目Apache POI( https://en.wikipedia.org/wiki/Apache_POI )名称类为HSSF(可怕的SpreadSheet格式)。

my recommendation, make it in server side. 我的建议,在服务器端进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM