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
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.