2012-07-27 65 views
0

我曾嘗試與此:如何讀取.docx文件並在iPhone中的UITextView中顯示其內容?

BOOL success; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
            NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 


    NSString *writableDBPath = [documentsDirectory 
           stringByAppendingPathComponent:@"FT1.docx"]; 
    success = [fileManager fileExistsAtPath:writableDBPath]; 
    NSLog(@"writableDBPath : %@", writableDBPath); 

    NSString *str = [NSString stringWithContentsOfFile:writableDBPath 
          encoding:NSUTF8StringEncoding error:&error]; 
    NSLog(@"My Data From File : %@",str); 

但「STR」打印(空)。該文件存在於文檔目錄中。

那麼,我該怎麼做呢?

在此先感謝..

+1

'NSString'不能讀作docx'文件。我想你必須寫一個'docx'閱讀器,'docx'是某種你可以解析的XML文件的存檔。 – rckoenes 2012-07-27 10:58:47

+0

什麼樣的docx讀者?你有任何鏈接/代碼? – 2012-07-27 11:09:00

回答

0

試試這個:

- (void)viewDidLoad 
{ 
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)]; 
    [webView setDelegate:self]; 
    NSURL *url = [NSURL fileURLWithPath:yourFilePath]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    [webView loadRequest:request]; 
} 
- (void)webViewDidFinishLoad:(UIWebView *)webView{ 
    NSString *document = [theWebView stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerText"]; 
    NSLog(@"%@",document); 
} 
+0

它的UIWebView,我想讀取.docx文件的文本並在UITextView中顯示它。我已經嘗試過以上解決方案。但我需要從docx文件加載的可編輯文本,所以我不能使用UIWebView。有沒有可以讀取docx文件的解決方案,然後我可以在UITextView中顯示它? – 2012-07-27 11:38:04

+0

你可以將收到的字符串設置爲textview,並可以更改。我試了一下。 – Dhruv 2012-07-27 11:42:22

+0

如何從文檔目錄讀取docx文件。我從早上開始嘗試。你能否在這裏放一些代碼(從docx文件中讀取內容)? – 2012-07-27 12:01:10

4

您可以使用此自由庫呈現DOCX文檔。他們還提供和SDK進行本地編輯,但它是支付的。看一看:http://www.mswordsdk.com

-(void)presentWordViewController:(NSString *)path { 
if (nil==self.wordViewController) { 
    self.wordViewController=[[WordViewController alloc] initWithNibName:nil bundle:nil]; 
    UIBarButtonItem *fixedSpace=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; 
    fixedSpace.width=80; 
    self.wordViewController.titleBar.items=[NSArray arrayWithObjects:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self.wordViewController action:@selector(dismissWordViewController)] , 
              fixedSpace, 
              [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], 
              self.wordViewController.pageInfoBarButtonItem, 
              [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], 
              [[UIBarButtonItem alloc] initWithTitle:@"PDF" style:UIBarButtonItemStyleBordered target:self action:@selector(pdfQuickLook:)], 
              nil]; 
} 

if (nil==self.modalNavigationConroller) { 
    self.modalNavigationConroller=[[UINavigationController alloc]initWithRootViewController:self.wordViewController]; 
    [self presentViewController:self.modalNavigationConroller animated:YES completion:nil]; 
} else { 
    if (nil==self.modalNavigationConroller.presentingViewController) { 
     [self presentViewController:self.wordViewController.navigationController animated:YES completion:nil]; 
    } 
} 
[self.wordViewController loadDocument:[WordViewControllerDocument documentWithURL:[NSURL fileURLWithPath:path] andTitle:nil]]; 

}

+2

你爲什麼拒絕我的答案。我提供了一個免費SDK的鏈接,它提供了加載和讀取.docx文件的可能性,這是試圖在這篇文章中解決的問題。這種態度讓人想知道爲什麼我應該在這裏分享東西或者試圖幫助某人。 – npereira 2014-07-18 10:01:18

+0

你是對的!您提供的圖書館對我非常有幫助,非常感謝。 – Aziz 2015-04-08 01:22:59

相關問題