dip s

dip s

  • NA
  • 256
  • 77.1k

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.
  1. public byte[] GetExcelFile(string FilePath, string ImagePathUserName, string ImagePathPassword)    
  2.        {    
  3.            byte[] result = null;    
  4.     
  5.            try    
  6.            {    
  7.                // username and password for permission to access shared folder    
  8.                var impersonationContext = new WrappedImpersonationContext(FilePath, ImagePathUserName, ImagePathPassword);    
  9.                impersonationContext.Enter();    
  10.     
  11.                if (!File.Exists(FilePath))    
  12.                {    
  13.                   // result = [];    
  14.                }    
  15.                else    
  16.                {    
  17.                    // Load file meta data with FileInfo    
  18.                    FileInfo fileInfo = new FileInfo(FilePath);    
  19.     
  20.                    // The byte[] to save the data in    
  21.                    byte[] data = new byte[fileInfo.Length];    
  22.     
  23.                    // Load a filestream and put its content into the byte[]    
  24.                    using (FileStream fs = fileInfo.OpenRead())    
  25.                    {    
  26.                        fs.Read(data, 0, data.Length);    
  27.                    }    
  28.                    result = (data);    
  29.                          
  30.                }    
  31.                impersonationContext.Leave();    
  32.            }    
  33.            catch (Exception ex)    
  34.            {    
  35.                objcommonrepository.LogError(ex.Message);    
  36.            }    
  37.            return result;    
  38.        }    
  39.    
 my service.ts code is like
 
  1. GetExcelFile(FilePath, ImagePathUserName, ImagePathPassword) {  
  2.     const headers = new Headers();  
  3.     headers.append('Content-Type''application/json');  
  4.     headers.append('Access-Control-Allow-Origin''*');  
  5.     headers.append('Access-Control-Allow-Methods''GET, POST, PUT');  
  6.     return this.http.get(environment.apiUrl + 'ImageUpload/GetPdfFile?FilePath=' + FilePath + '&ImagePathUserName=' + ImagePathUserName  
  7.     + '&ImagePathPassword=' + ImagePathPassword,  
  8.     { headers: headers }).pipe(map(data => data.json()),  
  9.         catchError((error: any) => {  
  10.           throw error;  
  11.         }));  
  12.   }  
my component.ts code is like
  1.  this.imageuploadservice.GetExcelFile(FilePathToRead, ImagePathUserName, ImagePathPassword).subscribe( data => {     
  2.     if (data != null && data.length > 0) {    
  3. onst blob = new Blob([data],{ type: 'application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet' });    
  4.        const url= window.URL.createObjectURL(blob);    
  5.        window.open(url);    
  6. }    
  7. ;
How to open excel file? any help would be appreciated

Answers (2)