2010-02-26 2 views
1

주어진 문서에서 문자열의 위치 또는 위치를 찾는 방법. 한 단어의 문서가 있으며 그 단어와 단어 위치를 데이터베이스에 모두 저장하여 그 이유는 무엇입니까? 단어의 위치를 ​​찾아야합니다.주어진 문서에서 문자열의 위치 또는 위치를 찾는 방법

그래서 주어진 문서에서 단어 나 문자열의 위치 나 위치를 어떻게 찾을 수 있는지 알려주세요.

내가 vb.net 나에 대한 C# 및 .DOC 문서

+1

를 작동 보인다 모든 위치를 저장하는 대신? 일부 단어 나 텍스트의 위치를 ​​동적으로 계산하는 방법을 고려해 보셨습니까? 그냥 문서를 통과하고 모든 단어의 위치를 ​​저장하는 것 같다 지루한입니다 –

+0

네 .. 시작을 위해 잘 잘 각 위치를 잘하고 "stopwords"를 제거하고 내가 진짜 필요한 위치를 저장할 수 있어야합니다 – ryder1211212

답변

1

흠을 사용하려는 ... 나는 더 스마트 해결책을 발견 haven't : - /하지만 어쩌면이 당신을하는 데 도움이 ... 당연 하죠 가정 시스템에 MS Office 버전이 설치되어 있어야합니다. 모든

첫째, 당신은 "* 개체 라이브러리 마이크로 소프트 워드?"

*라는 마이크로 소프트 COM 구성 요소 프로젝트에 대한 참조를 추가해야? 이 버전의 deppends 당신의 MS 오피스

피고측은 참조를 추가 한 후에는이 코드를 테스트 할 수 있습니다 :

using System; 
using System.Collections.Generic; 
using System.Text; 
using Word; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 

     static void Main(string[] args) 
     { 

      // Find the full path of our document 

      System.IO.FileInfo ExecutableFileInfo = new System.IO.FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location);    
      object docFileName = System.IO.Path.Combine(ExecutableFileInfo.DirectoryName, "document.doc"); 

      // Create the needed Word.Application and Word.Document objects 

      object nullObject = System.Reflection.Missing.Value; 
      Word.Application application = new Word.ApplicationClass(); 
      Word.Document document = application.Documents.Open(ref docFileName, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject); 


      string wholeTextContent = document.Content.Text; 
      wholeTextContent = wholeTextContent.Replace('\r', ' '); // Delete lines between paragraphs 
      string[] splittedTextContent = wholeTextContent.Split(' '); // Get the separate words 

      int index = 1; 
      foreach (string singleWord in splittedTextContent) 
      { 
       if (singleWord.Trim().Length > 0) // We don´t need to store white spaces 
       { 
        Console.WriteLine("Word: " + singleWord + "(position: " + index.ToString() + ")"); 
        index++; 
       } 
      } 

      // Dispose Word.Application and Word.Document objects resources 

      document.Close(ref nullObject, ref nullObject, ref nullObject); 
      application.Quit(ref nullObject, ref nullObject, ref nullObject); 
      document = null; 
      application = null; 

      Console.ReadLine(); 
     } 
    } 
} 

거 야 테스트를하고 그것이 =)

+0

고마워요, 그것 작품 ..... 나는 단어 COM의 내 버전의 16 cos에 누락 된 악기를 증가 시켰으며 어떤 문서의 마지막 단락을 표시하기 때문에 약간의 접촉이 필요합니다. 고마워요. tho – ryder1211212

+0

=) 그것이 당신을 도왔다 니 기쁘다. 인사말 – Javier

+0

이 awfull 코드를 작성하는 동안 좋은 아이디어가 .NET 용 오픈 오피스 라이브러리에 대해 자세히 배우고 싶다고 생각했습니다 ... – Javier