2、如果word中有表格,如何读出某一个单元格中的内容?(考虑合并过的单元格)
分不够可以再加,只要问题解决,分的问题好商量!:)
5 个解决方案
#1
To PerfectCK:
利用Automation,可以完成对Word文档的各种复杂操作,包括文档的生成、修改、统计字数等等等等。在MSDN里面可以参考“Working with Microsoft Word Objects”一文。
对于你提出的问题,我写了下面一段例子代码仅供参考:
private void menuItem2_Click(object sender, System.EventArgs e)
{
Object Nothing=System.Reflection.Missing.Value;
object filename=@"c:\test.doc";
Word.Application wordApp=new Word.ApplicationClass();
Word.Document wordDoc=wordApp.Documents.Open(ref filename,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing);
this.textBox1.Text+="\r\n"+wordDoc.Paragraphs.Last.Range.Text.ToString();
this.textBox1.Text+="\r\n"+wordDoc.Tables.Item(1).Cell(1,1).Range.Text.ToString();
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
Object Nothing=System.Reflection.Missing.Value;
object filename=@"c:\test.doc";
Word.Application wordApp=new Word.ApplicationClass();
Word.Document wordDoc=wordApp.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing);
Word.Table table=wordDoc.Tables.Add(wordApp.Selection.Range,2,3,ref Nothing,ref Nothing);
table.Cell(1,1).Range.Text="1892730987098";
wordDoc.Paragraphs.Last.Range.Text="Hello";
wordDoc.SaveAs(ref filename,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing);
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}
在这段例子里面,menuItem3_Click()函数新建了一个Word文档,并在里面插入了一个表格和一段文字。表格的大小是两行三列,最左上的cell里面的内容是"1892730987098",后面一段文字的内容是"Hello"。其大致如下:
+---------------+--------------+--------------+
| 1892730987098 | | |
+---------------+--------------+--------------+
| | | |
+---------------+--------------+--------------+
Hello
上面的例子代码中,menuItem2_Click()完成的工作就是打开上面创建的Word文档,并读取表格的第一个cell的内容以及下面一段文字的内容,然后将其显示在this.textBox1中。
您可以试试看上面这段例子代码,运行前需要在项目的Reference里面添加Microsoft Word 10.0 Object Library。
Hogwarts - S(u)ddenly dis@ppeared...
-
本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
利用Automation,可以完成对Word文档的各种复杂操作,包括文档的生成、修改、统计字数等等等等。在MSDN里面可以参考“Working with Microsoft Word Objects”一文。
对于你提出的问题,我写了下面一段例子代码仅供参考:
private void menuItem2_Click(object sender, System.EventArgs e)
{
Object Nothing=System.Reflection.Missing.Value;
object filename=@"c:\test.doc";
Word.Application wordApp=new Word.ApplicationClass();
Word.Document wordDoc=wordApp.Documents.Open(ref filename,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing);
this.textBox1.Text+="\r\n"+wordDoc.Paragraphs.Last.Range.Text.ToString();
this.textBox1.Text+="\r\n"+wordDoc.Tables.Item(1).Cell(1,1).Range.Text.ToString();
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
Object Nothing=System.Reflection.Missing.Value;
object filename=@"c:\test.doc";
Word.Application wordApp=new Word.ApplicationClass();
Word.Document wordDoc=wordApp.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing);
Word.Table table=wordDoc.Tables.Add(wordApp.Selection.Range,2,3,ref Nothing,ref Nothing);
table.Cell(1,1).Range.Text="1892730987098";
wordDoc.Paragraphs.Last.Range.Text="Hello";
wordDoc.SaveAs(ref filename,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing);
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}
在这段例子里面,menuItem3_Click()函数新建了一个Word文档,并在里面插入了一个表格和一段文字。表格的大小是两行三列,最左上的cell里面的内容是"1892730987098",后面一段文字的内容是"Hello"。其大致如下:
+---------------+--------------+--------------+
| 1892730987098 | | |
+---------------+--------------+--------------+
| | | |
+---------------+--------------+--------------+
Hello
上面的例子代码中,menuItem2_Click()完成的工作就是打开上面创建的Word文档,并读取表格的第一个cell的内容以及下面一段文字的内容,然后将其显示在this.textBox1中。
您可以试试看上面这段例子代码,运行前需要在项目的Reference里面添加Microsoft Word 10.0 Object Library。
Hogwarts - S(u)ddenly dis@ppeared...
-
本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
#2
string xx,yy;
Word.ApplicationClass oWord = new Word.ApplicationClass();
Word.Document oDoc;
Word.Range oRange;
Word.Table otable;
Word.Selection oselection;
object a = Missing.Value;
object b = Missing.Value;
object filename=@"c:\test.doc";
oDoc = oWord.Documents.Open(ref filename,ref a,ref a,ref a,ref
//or oDoc = oWord.Documents.Add(ref a,ref a,ref a,ref a);
a,ref a,ref a,ref a,ref a,ref a,ref a,ref a);
oWord.Visible = true;
oDoc = oWord.ActiveDocument
oRange = oDoc.Range(ref a,ref b);
oRange.Rows.Count = 9;
oRange.Select;
xx =oRange.Text;
otable = oDoc.Tables.Item(1);
otable.Select;
yy = otable.Cell(0,1).Range.Text;
...
Word.ApplicationClass oWord = new Word.ApplicationClass();
Word.Document oDoc;
Word.Range oRange;
Word.Table otable;
Word.Selection oselection;
object a = Missing.Value;
object b = Missing.Value;
object filename=@"c:\test.doc";
oDoc = oWord.Documents.Open(ref filename,ref a,ref a,ref a,ref
//or oDoc = oWord.Documents.Add(ref a,ref a,ref a,ref a);
a,ref a,ref a,ref a,ref a,ref a,ref a,ref a);
oWord.Visible = true;
oDoc = oWord.ActiveDocument
oRange = oDoc.Range(ref a,ref b);
oRange.Rows.Count = 9;
oRange.Select;
xx =oRange.Text;
otable = oDoc.Tables.Item(1);
otable.Select;
yy = otable.Cell(0,1).Range.Text;
...
#3
同意楼上的说法
对WORD的操作,VBA很好的
你可以通在WORD 中录制宏,查看 用VBA编写的代码
然后参考楼上的方式,转换为C#。
对WORD的操作,VBA很好的
你可以通在WORD 中录制宏,查看 用VBA编写的代码
然后参考楼上的方式,转换为C#。
#4
我是指Hogwarts([微软]S(u)ddenly dis@ppeared...)发的帖子说的
#5
up!gz!
#1
To PerfectCK:
利用Automation,可以完成对Word文档的各种复杂操作,包括文档的生成、修改、统计字数等等等等。在MSDN里面可以参考“Working with Microsoft Word Objects”一文。
对于你提出的问题,我写了下面一段例子代码仅供参考:
private void menuItem2_Click(object sender, System.EventArgs e)
{
Object Nothing=System.Reflection.Missing.Value;
object filename=@"c:\test.doc";
Word.Application wordApp=new Word.ApplicationClass();
Word.Document wordDoc=wordApp.Documents.Open(ref filename,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing);
this.textBox1.Text+="\r\n"+wordDoc.Paragraphs.Last.Range.Text.ToString();
this.textBox1.Text+="\r\n"+wordDoc.Tables.Item(1).Cell(1,1).Range.Text.ToString();
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
Object Nothing=System.Reflection.Missing.Value;
object filename=@"c:\test.doc";
Word.Application wordApp=new Word.ApplicationClass();
Word.Document wordDoc=wordApp.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing);
Word.Table table=wordDoc.Tables.Add(wordApp.Selection.Range,2,3,ref Nothing,ref Nothing);
table.Cell(1,1).Range.Text="1892730987098";
wordDoc.Paragraphs.Last.Range.Text="Hello";
wordDoc.SaveAs(ref filename,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing);
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}
在这段例子里面,menuItem3_Click()函数新建了一个Word文档,并在里面插入了一个表格和一段文字。表格的大小是两行三列,最左上的cell里面的内容是"1892730987098",后面一段文字的内容是"Hello"。其大致如下:
+---------------+--------------+--------------+
| 1892730987098 | | |
+---------------+--------------+--------------+
| | | |
+---------------+--------------+--------------+
Hello
上面的例子代码中,menuItem2_Click()完成的工作就是打开上面创建的Word文档,并读取表格的第一个cell的内容以及下面一段文字的内容,然后将其显示在this.textBox1中。
您可以试试看上面这段例子代码,运行前需要在项目的Reference里面添加Microsoft Word 10.0 Object Library。
Hogwarts - S(u)ddenly dis@ppeared...
-
本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
利用Automation,可以完成对Word文档的各种复杂操作,包括文档的生成、修改、统计字数等等等等。在MSDN里面可以参考“Working with Microsoft Word Objects”一文。
对于你提出的问题,我写了下面一段例子代码仅供参考:
private void menuItem2_Click(object sender, System.EventArgs e)
{
Object Nothing=System.Reflection.Missing.Value;
object filename=@"c:\test.doc";
Word.Application wordApp=new Word.ApplicationClass();
Word.Document wordDoc=wordApp.Documents.Open(ref filename,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing);
this.textBox1.Text+="\r\n"+wordDoc.Paragraphs.Last.Range.Text.ToString();
this.textBox1.Text+="\r\n"+wordDoc.Tables.Item(1).Cell(1,1).Range.Text.ToString();
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
Object Nothing=System.Reflection.Missing.Value;
object filename=@"c:\test.doc";
Word.Application wordApp=new Word.ApplicationClass();
Word.Document wordDoc=wordApp.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing);
Word.Table table=wordDoc.Tables.Add(wordApp.Selection.Range,2,3,ref Nothing,ref Nothing);
table.Cell(1,1).Range.Text="1892730987098";
wordDoc.Paragraphs.Last.Range.Text="Hello";
wordDoc.SaveAs(ref filename,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing);
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}
在这段例子里面,menuItem3_Click()函数新建了一个Word文档,并在里面插入了一个表格和一段文字。表格的大小是两行三列,最左上的cell里面的内容是"1892730987098",后面一段文字的内容是"Hello"。其大致如下:
+---------------+--------------+--------------+
| 1892730987098 | | |
+---------------+--------------+--------------+
| | | |
+---------------+--------------+--------------+
Hello
上面的例子代码中,menuItem2_Click()完成的工作就是打开上面创建的Word文档,并读取表格的第一个cell的内容以及下面一段文字的内容,然后将其显示在this.textBox1中。
您可以试试看上面这段例子代码,运行前需要在项目的Reference里面添加Microsoft Word 10.0 Object Library。
Hogwarts - S(u)ddenly dis@ppeared...
-
本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
#2
string xx,yy;
Word.ApplicationClass oWord = new Word.ApplicationClass();
Word.Document oDoc;
Word.Range oRange;
Word.Table otable;
Word.Selection oselection;
object a = Missing.Value;
object b = Missing.Value;
object filename=@"c:\test.doc";
oDoc = oWord.Documents.Open(ref filename,ref a,ref a,ref a,ref
//or oDoc = oWord.Documents.Add(ref a,ref a,ref a,ref a);
a,ref a,ref a,ref a,ref a,ref a,ref a,ref a);
oWord.Visible = true;
oDoc = oWord.ActiveDocument
oRange = oDoc.Range(ref a,ref b);
oRange.Rows.Count = 9;
oRange.Select;
xx =oRange.Text;
otable = oDoc.Tables.Item(1);
otable.Select;
yy = otable.Cell(0,1).Range.Text;
...
Word.ApplicationClass oWord = new Word.ApplicationClass();
Word.Document oDoc;
Word.Range oRange;
Word.Table otable;
Word.Selection oselection;
object a = Missing.Value;
object b = Missing.Value;
object filename=@"c:\test.doc";
oDoc = oWord.Documents.Open(ref filename,ref a,ref a,ref a,ref
//or oDoc = oWord.Documents.Add(ref a,ref a,ref a,ref a);
a,ref a,ref a,ref a,ref a,ref a,ref a,ref a);
oWord.Visible = true;
oDoc = oWord.ActiveDocument
oRange = oDoc.Range(ref a,ref b);
oRange.Rows.Count = 9;
oRange.Select;
xx =oRange.Text;
otable = oDoc.Tables.Item(1);
otable.Select;
yy = otable.Cell(0,1).Range.Text;
...
#3
同意楼上的说法
对WORD的操作,VBA很好的
你可以通在WORD 中录制宏,查看 用VBA编写的代码
然后参考楼上的方式,转换为C#。
对WORD的操作,VBA很好的
你可以通在WORD 中录制宏,查看 用VBA编写的代码
然后参考楼上的方式,转换为C#。
#4
我是指Hogwarts([微软]S(u)ddenly dis@ppeared...)发的帖子说的
#5
up!gz!