iOS- 读取Excel 等文件

1.使用UIWebView

- (void) setWebView
{
    NSString *ExcelName = [NSString stringWithFormat:@"通讯录.xlsx"];
    _ExcelWebView = [[UIWebView alloc]initWithFrame:self.view.frame];
    _ExcelWebView.delegate = self;
    _ExcelWebView.scalesPageToFit = YES;//自动对页面进行缩放以适应屏幕
    [_ExcelWebView setClipsToBounds:YES];//设置为界限
    [_ExcelWebView setScalesPageToFit:YES];//页面设置为合适
    [self showExcelData:ExcelName];
    [self.view addSubview:_ExcelWebView];
}
//    _ExcelWebView.detectsPhoneNumbers = YES;//自动检测网页上的电话号码,单击可以拨打
- (void) showExcelData:(NSString *)ExcelName
{
    NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:ExcelName];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_ExcelWebView loadRequest:request];
    /**
     *  或者以下方法
     NSString *path = [[NSBundle mainBundle] pathForResource:@"通讯录" ofType:@"xlsx"];
     NSURL *url = [NSURL fileURLWithPath:path];
     [_ExcelWebView loadRequest:[NSURLRequest requestWithURL:url]];
     */
}
//打印Excel数据
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *strings = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerText"];
    NSLog(@"%@",strings);
}

2.使用QuickLook.freamwork

- (void) setQuickLook
{
    NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"通讯录.xlsx"];
    UIDocumentInteractionController *document = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:path]];
    document.delegate = self;
    NSLog(@"%d",[document presentPreviewAnimated:YES]);//显示数据
}
//预览:文档交互控制器为预览视图控制器
- (UIViewController*)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController*)controller
{
    return self;
}
//视图预览文档交互控制器
- (UIView*)documentInteractionControllerViewForPreview:(UIDocumentInteractionController*)controller
{
    return self.view;
}
//预览文档交互控制器矩形
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller
{
    return self.view.frame;
}
//点击预览窗口的“Done”(完成)按钮时调用
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController*)_controller
{
    
}

3.使用QLPreviewController

- (void) setQLPreviewController
{
    QLPreviewController *previewoCntroller = [[QLPreviewController alloc] init];
    previewoCntroller.dataSource = self;
    previewoCntroller.delegate = self;
    [self presentViewController:previewoCntroller animated:YES completion:nil];
}
- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller
{
    return 1;
}
- (id )previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
    NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"通讯录.xlsx"];
    return [NSURL fileURLWithPath:path];
}

4.通过DHxls框架
https://github.com/dhoerl/DHlibxls
但是这个框架下载下来需要添加文件,注意Excel表格导入的时候要如下操作
具体操作http://www.jianshu.com/p/7cb76399bf7c

iOS- 读取Excel 等文件_第1张图片
helloworld.png

你可能感兴趣的:(iOS- 读取Excel 等文件)