TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Ms_ Dev
NA
236
91.8k
Excel file export & downloading issue in Angular6/Typescript
Mar 14 2019 10:33 PM
Hi All,
I have the following code in my .ts file which is creating excel export file but while opening the file in MS EXCEL, it is not opening and showing error as file has been currupted or bad file extension.
ExportExcel(objArray: any, filename: string) {
var
array =
typeof
objArray !=
'object'
? JSON.parse(objArray) : objArray;
var
str =
'<table>'
;
var
row =
"<tr>"
;
for
(
var
index
in
objArray[0]) {
row +=
'<th>'
+ index +
'</th>'
}
row = row.slice(0, -5);
str += row +
'</tr>'
;
for
(
var
i = 0; i < array.length; i++) {
var
line =
'<tr>'
;
for
(
var
index
in
array[i]) {
line +=
'<td>'
+ array[i][index] +
'</td>'
;
}
str += line +
'</tr>'
;
}
str +=
'</table>'
;
var
a = document.createElement(
"a"
);
a.setAttribute(
'style'
,
'display:none;'
);
document.body.appendChild(a);
var
blob =
new
Blob([str], { type:
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
});
var
url = window.URL.createObjectURL(blob);
a.href = url;
a.download = filename +
'.xlsx'
;
a.click();
str =
''
;
row =
''
;
}
Any help on this would be highly appriciated.
Reply
Answers (
1
)
Work Flow Tool in Angular 4
Autocomplete dropdown without using any 3rd party package