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
dip s
NA
256
81.2k
How to open/show an excel file on button click in Angular 8 and C#
Feb 3 2021 10:20 AM
Hello,
In my web app, there are some excel files stored on one of the computer in network. When user clicks on specific file name then that excel file should be opened and user will be able to see that excel file. We have used angular 8 for front end and asp.net mvc 5 for back end.
The code which i have done is like.
File path = "IP address of computer\folder name\ excel file"
Imagepathusername and Imagepathpassword to access computer in network o get excel sheet . This is the user name and password of computer on which excel file is stored.
public
byte
[] GetExcelFile(
string
FilePath,
string
ImagePathUserName,
string
ImagePathPassword)
{
byte
[] result =
null
;
try
{
// username and password for permission to access shared folder
var impersonationContext =
new
WrappedImpersonationContext(FilePath, ImagePathUserName, ImagePathPassword);
impersonationContext.Enter();
if
(!File.Exists(FilePath))
{
// result = [];
}
else
{
// Load file meta data with FileInfo
FileInfo fileInfo =
new
FileInfo(FilePath);
// The byte[] to save the data in
byte
[] data =
new
byte
[fileInfo.Length];
// Load a filestream and put its content into the byte[]
using
(FileStream fs = fileInfo.OpenRead())
{
fs.Read(data, 0, data.Length);
}
result = (data);
}
impersonationContext.Leave();
}
catch
(Exception ex)
{
objcommonrepository.LogError(ex.Message);
}
return
result;
}
my service.ts code is like
GetExcelFile(FilePath, ImagePathUserName, ImagePathPassword) {
const
headers =
new
Headers();
headers.append(
'Content-Type'
,
'application/json'
);
headers.append(
'Access-Control-Allow-Origin'
,
'*'
);
headers.append(
'Access-Control-Allow-Methods'
,
'GET, POST, PUT'
);
return
this
.http.get(environment.apiUrl +
'ImageUpload/GetPdfFile?FilePath='
+ FilePath +
'&ImagePathUserName='
+ ImagePathUserName
+
'&ImagePathPassword='
+ ImagePathPassword,
{ headers: headers }).pipe(map(data => data.json()),
catchError((error: any) => {
throw
error;
}));
}
my component.ts code is like
this
.imageuploadservice.GetExcelFile(FilePathToRead, ImagePathUserName, ImagePathPassword).subscribe( data => {
if
(data !=
null
&& data.length > 0) {
onst blob =
new
Blob([data],{ type:
'application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet'
});
const
url= window.URL.createObjectURL(blob);
window.open(url);
}
;
How to open excel file? any help would be appreciated
Reply
Answers (
2
)
how to add custom button in summernote editor
Sign-in into application with linked-in Account