【问题标题】:How to read .docx file & show its contents in UITextView?如何读取 .docx 文件并在 UITextView 中显示其内容?
【发布时间】:2012-07-26 00:48:52
【问题描述】:

我试过这个:

  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 打印出(null)。该文件存在于文档目录中。

那么,我该怎么做呢?

【问题讨论】:

  • NSString 无法读取 docx 文件。我想你将不得不写一个docx 阅读器,docx 是某种带有 XML 文件的存档,你可以解析。
  • 什么样的docx阅读器?你有任何链接/代码吗?

标签: iphone nsstring nsfilemanager docx nsdocumentdirectory


【解决方案1】:

试试这个:

- (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);
}

【讨论】:

  • 它的 UIWebView,我想读取 .docx 文件的文本并在 UITextView 中显示。我已经尝试过上述解决方案。但我需要从 docx 文件加载的可编辑文本,所以我不能使用 UIWebView。有没有可以读取 docx 文件然后我可以在 UITextView 中显示它的解决方案?
  • 您可以将接收到的字符串设置为textview,也可以更改。我试过了。
  • 您是如何从文档目录中读取 docx 文件的。我从早上开始尝试这个。你能在这里放一些代码(从 docx 文件中读取内容)吗?
  • yourFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"file.docx"]];
【解决方案2】:

您可以使用这个免费的库来呈现 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]];

}

【讨论】:

  • 你为什么拒绝我的回答。我提供了一个免费 SDK 的链接,它提供了加载和读取 .docx 文件的可能性,这是本文试图解决的问题。这种态度让人想知道我为什么要在这里分享东西或试图帮助别人。
  • 你是对的!你提供的图书馆对我很有帮助,非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-24
  • 1970-01-01
  • 2016-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多