IOS 通過Objective-C讀取、解析Excel


顯示Excel我就不介紹了,大多人都知道使用UIWebView控件即可,所以直接上代碼

//
//  ViewController.m
//  PRJ_excelDemo
//
//  Created by wangzhipeng on 13-4-12.
//  Copyright (c) 2013年 com.comsoft. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (strong, nonatomic) IBOutlet UIWebView *pWV_main;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [_pWV_main setClipsToBounds:YES];
    [_pWV_main setScalesPageToFit:YES];
    //Document路徑下
//  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//  NSString *documentsDirectory = [paths objectAtIndex:0];
    //項目路徑下
    NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"wangzhipeng.xlsx"];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_pWV_main loadRequest:request];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

問題是我目前開發的項目需求是獲取.xlsx中的內容

於是我想起來了,UIWebView能加載說明它獲取到了.xlsx內容,我就想通過JS來獲取UIWebView的內容

NSString *pStr_js = @"document.documentElement.innerHTML";
NSString *pStr_html = [_pWV_main stringByEvaluatingJavaScriptFromString:pStr_js];
    
NSLog(@"%@", pStr_html);

結果竟然是:

2013-04-12 16:59:01.418 PRJ_excelDemo[4195:11303] <head></head><body></body>

好吧,我傻了...

我在谷歌上搜查了半天,但我似乎無法找到通過Objective-C來讀取Excel文件的方法。

我覺得唯一的可行的解決方案是Excel先轉換為CSV,然后讀取一個文本文件,但我不希望出現這種情況,太麻煩。

 

解決方案:

最后在 StackOverFlow的問題 從這個問題中找尋的相關解決方法,用到的 libxls 庫和 DHlibxls 框架。

主要是 DHlibxls ,個人認為是最底層是libxls庫,而DHlibxls對libxls 進行了一次封裝,使得在IOS中更好用了。

以下是相關的DHlibxls 的下載,DHlibxls這個是已經把libxls 庫添加到該項目中了,可以直接點擊

TestDHlibxls.xcodeproj 查看使用方法 .

源碼下載地址:http://ishare.iask.sina.com.cn/f/36736718.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM