我使用这段代码生成了一个.xls文件,并将其保存在文档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;
.xls文件
当我试图在UIWebView中打开它时,它给出了一个错误“读取文档时出错”。但.xls文件已成功创建。我需要知道为什么webview无法打开文件。我尝试在webview中打开其他一些.xls文件,但它取得了成功。为什么不能打开我创建的.xls文件?任何建议。
答案 0 :(得分:0)
您可以尝试使用此代码加载生成的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];
请告诉我此代码是否可以帮助您。