0

I've generated an .xls file using this piece of code and saved it inside documents directry.

NSMutableString *excel = [[NSMutableString alloc] init];

    //Excel sheet header
    [excel appendString:@"<?xml version=\"1.0\"?>\n<?mso-application progid=\"Excel.Sheet\"?>\n<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" "];
    [excel appendString:@"xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\">\n<DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">"];
    [excel appendString:@"<LastAuthor>Kuzora</LastAuthor>"];
    [excel appendString:[NSString stringWithFormat:@"<Created>%@</Created>",[NSDate date]]];
    [excel appendString:@"<Version>11.5606</Version>\n</DocumentProperties>\n<ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">\n<WindowHeight>6690</WindowHeight>\n<WindowWidth>14355</WindowWidth>"];
    [excel appendString:@"<WindowTopX>360</WindowTopX>\n<WindowTopY>75</WindowTopY>\n<ProtectStructure>False</ProtectStructure>\n<ProtectWindows>False</ProtectWindows>\n</ExcelWorkbook>\n<Styles>"];
    [excel appendString:@"<Style ss:ID=\"Default\" ss:Name=\"Normal\">\n<Alignment ss:Vertical=\"Bottom\"/>\n<Borders/>\n<Font/>\n<Interior/>\n<NumberFormat/>\n<Protection/>\n</Style>"];
    [excel appendString:@"<Style ss:ID=\"s21\">\n<NumberFormat ss:Format=\"Medium Date\"/>\n</Style><Style ss:ID=\"s22\">\n<NumberFormat ss:Format=\"Short Date\"/>\n</Style></Styles>"];

    //Excel sheet content
    [excel appendString:@"<Worksheet ss:Name=\"User\">"];
    [excel appendString:@"<Table ss:ExpandedColumnCount=\"4\" ss:ExpandedRowCount=\"1\" x:FullColumns=\"1\" x:FullRows=\"1\">"];
    [excel appendString:@"<Row>"];
    [excel appendString:@"<Cell><Data ss:Type=\"String\">TraineeDetailID</Data></Cell>"];
    [excel appendString:@"<Cell><Data ss:Type=\"String\">Name</Data></Cell>"];
    [excel appendString:@"<Cell><Data ss:Type=\"String\">DOB</Data></Cell>"];
    [excel appendString:@"<Cell><Data ss:Type=\"String\">HeightFeet</Data></Cell>"];
    [excel appendString:@"</Row>"];
    [excel appendString:@"</Table>"];
    [excel appendString:@"</Worksheet>"];
    [excel appendString:@"</Workbook>"];

    filePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"somename.xls"];
    data = [excel dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:FALSE];
    if(![data writeToFile:filePath atomically:YES])
        return;

The .xls file enter image description here

When i tried to open it in an UIWebView it is giving me an error "An error occurred while reading the document.". But .xls file has been created successfully. I need to know why the webview cant able to open the file. I tried opening some other .xls file in webview but it was a success. Why cant it open the .xls file that i've created? any suggestions.

skyler
  • 742
  • 1
  • 10
  • 24
  • Maybe this can help you: http://stackoverflow.com/questions/4090386/uiwebview-unable-to-read-document-error – doruvil Jun 23 '14 at 09:11

1 Answers1

0

You can try this code to load generated excel:

 NSString *resourceDBFolderPath;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error1;
    NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"somename.xls"]];
    BOOL success = [fileManager fileExistsAtPath:documentDBFolderPath];

    NSURL *url = [NSURL fileURLWithPath: documentDBFolderPath];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
if(success)
    [webView loadRequest:request];

Please let me know whether this code helps you.

Shankar Aware
  • 128
  • 1
  • 11
  • i tried the same code to load the excel, but i didn't succeeded. – skyler Jun 23 '14 at 09:43
  • i have created new project and use your code and run the app ....actually you would need to add the following code to load the file [_webView loadData:data MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:nil]; – Shankar Aware Jun 23 '14 at 10:36
  • :Thanks for your help, did you succeeded by doing that way. When in tried i didn't succeeded. – skyler Jun 23 '14 at 11:44
  • have you debugged and printed whether therre is some data for data object,,, – Shankar Aware Jun 23 '14 at 11:48
  • if you are doing everything in the viewDidload then it will not going to work ...since it take time to generate the xml file and takes time to load the data on webview so check by adding the button on same screen and try to load it by clicking the button – Shankar Aware Jun 23 '14 at 11:51
  • Thanks, i double checked the xml file twice after it got created. Its good, but when i tried to load it fails. I even tried this (https://github.com/andreac/RSSheet). Which also generates .xls file, but also fails while loaded in a UIWebview. Is there any other way to overcome this. – skyler Jun 23 '14 at 12:35