首页 > 代码库 > 在iphone程序中打开word、execl、pdf等文档
在iphone程序中打开word、execl、pdf等文档
方法一:
用UIWebView就可以了
-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView
{
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
// Calling -loadDocument:inView:
[self loadDocument:@"test.doc" inView:self.myWebview];
方法我也已经测试过了,希望对大家有帮助,
方法二:
下面方法是直接通过QLPreviewController打开文档
qlViewController = [[QLPreviewController alloc] init];
qlViewController.dataSource = self;
[self presentModalViewController:qlViewController animated:YES];
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
return 1;
}
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller
previewItemAtIndex:(NSInteger)index{
//-------------读文件
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if (!documentsDirectory) {
NSLog(@"Documents directory not found!");//return ;
}
NSString *fileName=[NSString stringWithFormat:@"%@.%@",nameQ,extQ];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
//-------------
NSURL *myQLDocument = [NSURL fileURLWithPath:appFile];
return myQLDocument;
}
用UIWebView就可以了
-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView
{
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
// Calling -loadDocument:inView:
[self loadDocument:@"test.doc" inView:self.myWebview];
方法我也已经测试过了,希望对大家有帮助,
方法二:
下面方法是直接通过QLPreviewController打开文档
qlViewController = [[QLPreviewController alloc] init];
qlViewController.dataSource = self;
[self presentModalViewController:qlViewController animated:YES];
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
return 1;
}
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller
previewItemAtIndex:(NSInteger)index{
//-------------读文件
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if (!documentsDirectory) {
NSLog(@"Documents directory not found!");//return ;
}
NSString *fileName=[NSString stringWithFormat:@"%@.%@",nameQ,extQ];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
//-------------
NSURL *myQLDocument = [NSURL fileURLWithPath:appFile];
return myQLDocument;
}
在iphone程序中打开word、execl、pdf等文档
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。