iOS- 讀取Excel 等文件

1.使用UIWebView

- (void) setWebView
{
    NSString *ExcelName = [NSString stringWithFormat:@"通訊錄.xlsx"];
    _ExcelWebView = [[UIWebView alloc]initWithFrame:self.view.frame];
    _ExcelWebView.delegate = self;
    _ExcelWebView.scalesPageToFit = YES;//自動(dòng)對頁面進(jìn)行縮放以適應(yīng)屏幕
    [_ExcelWebView setClipsToBounds:YES];//設(shè)置為界限
    [_ExcelWebView setScalesPageToFit:YES];//頁面設(shè)置為合適
    [self showExcelData:ExcelName];
    [self.view addSubview:_ExcelWebView];
}
//    _ExcelWebView.detectsPhoneNumbers = YES;//自動(dòng)檢測網(wǎng)頁上的電話號碼,單擊可以撥打
- (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數(shù)據(jù)
-(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]);//顯示數(shù)據(jù)
}
//預(yù)覽:文檔交互控制器為預(yù)覽視圖控制器
- (UIViewController*)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController*)controller
{
    return self;
}
//視圖預(yù)覽文檔交互控制器
- (UIView*)documentInteractionControllerViewForPreview:(UIDocumentInteractionController*)controller
{
    return self.view;
}
//預(yù)覽文檔交互控制器矩形
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller
{
    return self.view.frame;
}
//點(diǎn)擊預(yù)覽窗口的“Done”(完成)按鈕時(shí)調(diào)用
- (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 <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
    NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"通訊錄.xlsx"];
    return [NSURL fileURLWithPath:path];
}

4.通過DHxls框架
https://github.com/dhoerl/DHlibxls
但是這個(gè)框架下載下來需要添加文件,注意Excel表格導(dǎo)入的時(shí)候要如下操作
具體操作http://m.itdecent.cn/p/7cb76399bf7c

helloworld.png

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容