objective-c - UIWebView 不打开 ms word (doc) 和 ms excel (xls) 文件

标签 objective-c excel file-io uiwebview ms-word

我正在使用 XCode 3.2.5 iOS SDK 4.2 为 iPad 开发应用程序。此应用程序需要打开 PDF、DOC 和 XLS 文件并向用户预览它们。

我正在尝试使用 UIWebView 控件打开它们。只有 PDF 文件被正确打开。打开 DOC 或 XLS 时,我得到一个黑色的空 View 。 这发生在模拟器和设备中。

我做错了什么?

代码如下:

-(void)prepareToShow:(NSString*)filePath fileType:(NSString*)fileType request:(NSURLRequest*)request{

UIWebView *webView = (UIWebView*)self.view;
self.actualFilePath = filePath;

NSLog(@"File Path %@",filePath);

NSString *mimeType = @"";
if ([fileType caseInsensitiveCompare:@"PDF"]==NSOrderedSame){
    mimeType = @"application/pdf";
} else  if ([fileType caseInsensitiveCompare:@"DOC"]==NSOrderedSame){
    mimeType = @"application/msword";
} else  if ([fileType caseInsensitiveCompare:@"XLS"]==NSOrderedSame){
    mimeType = @"application/vnd.ms-excel";
}

[webView loadData:[NSData dataWithContentsOfFile:filePath] MIMEType:mimeType textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@"http://www.google.com"]];

}

最佳答案

要加载 word 或 excel 或 pdf 文档,您需要的是 文档文件物理路径。这意味着文件应该保存在文档或临时目录中或者它可能在应用程序包中。

//now use file path to load in webview
[yourWebView loadRequest:[NSURLRequest requestWithURL:[NSURL  fileURLWithPath:yourFilePath]]];

关于objective-c - UIWebView 不打开 ms word (doc) 和 ms excel (xls) 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5941562/

相关文章:

iOS ODRefreshControll 拉长

objective-c - UILabel 子类在 Objective-C 中显示为 UILabel

ios - 排序字符串包含数字,NSMutableArray 与字典

php - 如何使用 PhpSpreadSheet 折线图在每个标记中设置可见数据标签?

python - 我该如何将二维列表的内容复制到 Excel 格式的剪贴板?

c - 如何从文件中逐行读取?

python - 如何在 python/django 中使用 URL 保存图像

ios - 学习 Objective-C 的好资源/书籍

c# - 使用 C# 将 Excel 第一列读取到数组中

java - Java 和 StringTokenizer 中对象数组的输入/输出