如何:使用 .net 在 Word 上添加多个表格
我尝试使用 c# 在 Word 文档中添加多个表
// Tables is a list of items which I want to present each in a table
foreach (List<string[]> ClassTable in Tables)
{
// tbl is a "Microsoft.Office.Interop.Word.Table"
// myRange is like MyDoc.Range(ref missing, ref missing)
tbl = MyDoc.Tables.Add(myRange, ClassTable.Count(), 3, missing, missing);
tbl.Borders.Enable = 1;
RowCounter = 1;
foreach (string[] item in TableContent)
{
ColumnCounter = 1;
foreach (string str in item)
{
tbl.Cell(RowCounter, ColumnCounter).Range.Text = str;
ColumnCounter++;
}
RowCounter++;
}
}
,此代码仅添加一个表,每当在第二个循环中时,我应该创建另一个表并添加下一个项目值,
我尝试通过设置 myRange.start 或 myRange 来更改范围.setRange() ...等 如果在一张表上创建大量行,则仅添加到文档事件的一张表会失败 但不是多桌
I try to add multi tables within a word document using c#
// Tables is a list of items which I want to present each in a table
foreach (List<string[]> ClassTable in Tables)
{
// tbl is a "Microsoft.Office.Interop.Word.Table"
// myRange is like MyDoc.Range(ref missing, ref missing)
tbl = MyDoc.Tables.Add(myRange, ClassTable.Count(), 3, missing, missing);
tbl.Borders.Enable = 1;
RowCounter = 1;
foreach (string[] item in TableContent)
{
ColumnCounter = 1;
foreach (string str in item)
{
tbl.Cell(RowCounter, ColumnCounter).Range.Text = str;
ColumnCounter++;
}
RowCounter++;
}
}
this code add only one table , whenever in the second loop I should create another table and add the next item values on
I try to change the Range with set the myRange.start or myRange.setRange() ... etc
All fail only one table added to the document event if it create alot of rows on one table
but not multi table
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该行在
第二次执行时抛出异常,并显示消息“无法删除范围”。此异常被 Word 吞噬,但会停止进一步执行。添加 try/catch 并设置断点会对您有所帮助。
我将您的代码编辑为以下内容以重现并找到引发的异常:
msdn 上的文档声明:
事实证明,您需要的是通过折叠它来“移动”到范围的末端。如果这样做,您将遇到另一个单词问题,即如果文档中有两个紧邻的表格,单词会自动将它们连接到一个表格中。您的固定代码最终会向一个表添加越来越多的行,并不断用值覆盖前几行。所有这些都会导致以下代码应该解决您的问题:
最后一个警告是该段中的第一行读取:
如果文档为空,则将表添加到此范围将起作用,否则它将抛出相同的异常,因为我们是在这种情况下不是最后。 .Collapse() 也会在那里解决它。
The line
throws an exception the 2nd time it's executed with the message "The range cannot be deleted". This exception is swallowed by Word but does stop further execution. Addin a try/catch and setting a breakboint would have helped you.
I edited your code to the following to reproduce and find the exception that was raised:
The docs at msdn state:
What turns out to be needed is that you 'move' to the end of the range by collapsing it. If you do so you will run into another word issue that if you have 2 tables directly after eachother in a document word will automagically join them into 1 table. Your fixed code would end up adding more and more rows to 1 table and constantly overwrite the first few rows with the values. All of this leads to the following code that should fix your problem:
One last warning is that the first line in this segment reads:
Adding a table to this range will work if the document is empty otherwise it will throw the same exception since we are not at the end in that case. A .Collapse() would resolve it there as well.