且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从word文件中获取特定内容

更新时间:2023-02-23 21:58:04

你需要使用循环,即:

   int  i =  1 ; i< 6; i ++)
{
var content = inputWord.GetRange( String .Format( 成本{0},i), Rs。);
// 您的代码
}

b $ b或

  int  i =  1 跨度>; 
执行
{
var content = inputWord.GetRange( String
.Format( 费用{0},i), Rs。);
// 此处的代码
i + = 1 跨度>;
} while (content!= null );


Hi,
   
I have one input word document file with content like
 
Cost of 1 (1 Jan 2017 -12 Jan 2017): 50 Rs .       
Cost of 2(3 Jan 2017 -12 Jan 2017): 60 Rs .       
Cost of 3(5 Jan 2017 -12 Jan 2017): 20 Rs .   
Cost of 4(6 Jan 2017 -12 Jan 2017): 10 Rs . 
Cost of 5(8 Jan 2017-12 Jan 2017): 90 Rs .


I need to display these contents to my output word file.I tried with this

public Range GetRange(object findText, object LastText)
        {
            Range range1 = doc.Content;
            range1.Find.Execute(findText);
            range1.Find.Execute(findText);
    
            Range range2 = doc.Content;
            range2.Find.Execute(LastText);
            range2.Find.Execute(LastText);
    
            if (range2.Start > range1.Start)
            {               
                Range rng = doc.Range(range1.Start, range2.End);
    
                return rng;
            }
            else
                return null;
        }




var content= inputWord.GetRange("Cost of 1", "Rs.");



But I get two lines cost of 1 and cost of 2.So how to get all the five cost frrom word file?

What I have tried:

I have tried with get content from word as range.

You need to use loop, i.e.:
for(int i=1;i<6;i++)
{
    var content= inputWord.GetRange(String.Format("Cost of {0}", i), "Rs.");
    //your code here
}


or

int i= 1;
do
{
    var content= inputWord.GetRange(String.Format("Cost of {0}", i), "Rs.");
    //your code here
    i+=1;
} while(content!=null);