[英]Export table into excel Angular 5
I am trying to export my table into excel sheet here I am generating my html table using json data dynamically. 我正在尝试将表导出到excel表中,我正在使用json数据动态生成html表。
I am using this code to generate my table in angular: 我正在使用此代码生成我的角度表:
var table = document.createElement("TABLE") as HTMLTableElement;
var row,header,cell1, cell2;
var data = chart.options.data;
// table.style.border = "1px solid #000";
header = table.createTHead();
row = header.insertRow(0);
table.setAttribute("id", "myId");
cell1 = row.insertCell(0);
cell2 = row.insertCell(1);
// cell1.style.border = "1px solid #000";
// cell2.style.border = "1px solid #000";
cell1.innerHTML = "<strong>Districts</strong>";
cell2.innerHTML = "<b>"+rain_fall_type+"_Value</b>";
for(var i = 0; i < data.length; i++){
for(var j = 0; j< data[i].dataPoints.length; j++){
// console.log(data[i].dataPoints[j]);
// console.log(data[i].dataPoints[j].label);
row = table.insertRow(1);
cell1 = row.insertCell(0);
cell2 = row.insertCell(1);
// cell1.style.border = "1px solid #000";
// cell2.style.border = "1px solid #000";
cell1.innerHTML = "<strong>"+data[i].dataPoints[j].label+"</strong>";
cell2.innerHTML = data[i].dataPoints[j].y;
}
}
// document.getElementById("chartContainer").innerHTML = "<h1>"+rain_fall_type+" "+year+"</h2>";
document.getElementById("chartContainer").appendChild(table);
This is generating a table and a values I am trying to use : https://www.npmjs.com/package/tableexport this library to export my table. 这将生成一个表和一个我要使用的值: https : //www.npmjs.com/package/tableexport此库可导出我的表。 When I am adding html table and run this code: 当我添加html表并运行以下代码时:
var n = new TableExport(document.getElementsByTagName("table"));
inside ngoninit I can see three buttons and all of them work fine but. 在ngoninit里面,我可以看到三个按钮,但它们都可以正常工作。 When I am generating a dynamic table using above code I don't see any button to export. 当我使用上述代码生成动态表时,我看不到要导出的任何按钮。
I have added a button and on that click I am using this: 我添加了一个按钮,然后单击该按钮,我正在使用此按钮:
myEvent(event) {
var n = new TableExport(document.getElementsByTagName("table"));
}
When I click on this button I can see all three buttons to export but none of them is clickable. 当我单击此按钮时,我可以看到所有三个要导出的按钮,但是没有一个是可单击的。 Is there any way I can make it work with dynamic html table. 有什么办法可以使它与动态html表一起使用。
you are missing the (click)="myEvent()" in your buttons, for example 例如,您缺少按钮中的(click)=“ myEvent()”
<button (click)="myEvent($event)" type="button" tableexport-id="5bf287e5-xlsx" class="button-default xlsx">Export to xlsx</button>
But also, in myEvent you define 而且,在myEvent中,您定义
var table = new TableExport(document.getElementsByTagName("table"));
Shouldn't it be, just to be sure you grab the correct table 不应该,只是确保您抓住正确的桌子
var table = new TableExport(document.getElementById("myId"));
In Angular you should be using Template reference variables ( #myTable ) and getting a reference to them in the controller with @ViewChild('myTable') 在Angular中,您应该使用模板引用变量(#myTable),并使用@ViewChild('myTable')在控制器中获取对它们的引用
There is a utility that provides excel, csv, json and txt export support to material table components I've recently shared on npm. 有一个实用程序可以为我最近在npm上共享的材料表组件提供excel,csv,json和txt导出支持。 It is used by multiple applications in prod so far. 到目前为止,它已在产品中被多个应用程序使用。
What you should do is just using matTableExporter directive and related properties of it. 您应该做的只是使用matTableExporter指令及其相关属性。
<mat-table matTableExporter [dataSource]="dataSource" #exporter="matTableExporter" hiddenColumns="[0]">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.