Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to open file.xlsx in UIWebView IOS using objective-c

I have file.xlsx in my IOS app documents folder. I want to show open this excel file in UIWebview. but i am getting below error,

Error Domain=WebKitErrorDomain Code=102 "Frame load interrupted" 

but pdf and CSV files are opening, I am new to IOS and tried all possible things for it to work i guess from last 2 days. nothing worked out.. please help me

Update: Even if i rename it as file.xls its not opening below is my code,

    NSURL* nsUrl = [NSURL URLWithString:url];
        _urlReq = [NSURL URLWithString:url];
        [self performSelector:@selector(urlRequestForFile) withObject:nil afterDelay:0];
        _webView.delegate = self;
        NSURLRequest* request = [NSURLRequest requestWithURL: nsUrl];
        [_webView loadRequest: request];

-(void)urlRequestForFile{
    self.connection = nil;
    NSURLRequest *requestForFile = [NSURLRequest requestWithURL:_urlReq cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:300];
    _connection = [[NSURLConnection alloc]initWithRequest:requestForFile delegate:self startImmediately:YES];
    _ongingServiceFlag = YES;

}

need help in showing xlsx file inside my IOS app either using UIWebView or is there any other way to show xlsx file inside the app without using third party apps?

Update(Solution):
I am very surprised to see that there is no support for XLSX mentioned even in apple site for UIWebView but actually UIWebView completely supports XLSX format. one thing you need to make sure is to specify the correct 'textEncodingName' value. if your file is stored with base64 binary encoding u have to mention it as textEncodingName:@"base64" otherwise u have to mention as "utf-8"

Below line worked for me:

[webView loadData:urlData MIMEType:@"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" textEncodingName:@"base64" baseURL:nil];
like image 388
kumar Avatar asked Sep 10 '17 15:09

kumar


3 Answers

Use QLPreviewController for showing xlsx document in your app. Find the links for the tutorial below.

https://developer.apple.com/documentation/quicklook/qlpreviewcontroller http://iosdevelopertips.com/data-file-management/preview-documents-with-qlpreviewcontroller.html https://www.appcoda.com/quick-look-framework/

like image 186
Ganesh Bavaskar Avatar answered Sep 18 '22 09:09

Ganesh Bavaskar


The .XLSX filetype is based on openXML which is good news! This means it's easily readable, we just need to let the webview know the type, or rather mimeTYPE, of the file we are loading/displaying. According to microsoft the mimetype to use for XLSX (OpenXML) files is:

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

source: https://blogs.msdn.microsoft.com/dmahugh/2006/08/08/content-types-for-open-xml-documents/

To do this we load the data (dataWithContentsOfFile: or dataWithContentsOfURL: or your prefered method) by calling the webView method:

[_webView loadData:<#(nonnull NSData *)#> MIMEType:<#(nonnull NSString *)#> textEncodingName:<#(nonnull NSString *)#> baseURL:<#(nonnull NSURL *)#>]

Example of my working code:

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://mycoolserver.com/file.xlsx"]];
[_webView loadData:data MIMEType:@"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" textEncodingName:@"utf-8" baseURL:nil];

Side-by-side comparison of origional file and webview rendered file

like image 31
Chris J Avatar answered Sep 18 '22 09:09

Chris J


.xlsx File cannot be opened using UIWebView. Though you can load .xls file using UIWebView. Here is a list of files which you can load using UIWebView

https://developer.apple.com/library/content/qa/qa1630/_index.html

If you want to use .xlsx file, you have to use QuickLook FrameWork which contains QLPreviewController. Your code should be like this -

  - (void) initQlController{
  QLPreviewController *prev = [[QLPreviewController alloc]init];
  prev.delegate = self;
  prev.dataSource = self;
  [self presentModalViewController:prev animated:YES];
  [prev.navigationItem setRightBarButtonItem:nil]; }

Then you have to use the dataSource methods for the same : -

- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index

- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
like image 36
Aditya Srivastava Avatar answered Sep 18 '22 09:09

Aditya Srivastava