gpt4 book ai didi

c# - 如何: Adding Multi tables on Word with .网

转载 作者:行者123 更新时间:2023-12-02 01:19:23 25 4
gpt4 key购买 nike

我尝试使用 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() ...等来更改范围如果在一张表上创建大量行,则仅添加到文档事件的一张表会失败但不是多表

最佳答案

线路

tbl = MyDoc.Tables.Add(myRange, ClassTable.Count(), 3, missing, missing); 

第二次执行时抛出异常,并显示消息“无法删除范围”。此异常被 Word 吞噬,但会停止进一步执行。添加 try/catch 并设置断点会对您有所帮助。

我将您的代码编辑为以下内容以重现并查找引发的异常:

    var myRange = Globals.ThisAddIn.Application.ActiveDocument.Range();
foreach (List<List<string>> ClassTable in new List<List<List<string>>> { new List<List<string>> { new List<string> { "A" }, new List<string> { "B" } }, new List<List<string>> { new List<string> { "C" }, new List<string> { "D" } } })
{
// tbl is a "Microsoft.Office.Interop.Word.Table"
// myRange is like MyDoc.Range(ref missing, ref missing)
Microsoft.Office.Interop.Word.Table tbl = null;
try
{
tbl = Globals.ThisAddIn.Application.ActiveDocument.Tables.Add(myRange, ClassTable.Count(), 3);

tbl.Borders.Enable = 1;
int RowCounter = 1;
foreach (var item in ClassTable)
{
int ColumnCounter = 1;
foreach (string str in item)
{
tbl.Cell(RowCounter, ColumnCounter).Range.Text = str;
ColumnCounter++;
}
RowCounter++;
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}

docs at msdn状态:

Required Range object. The range where you want the table to appear. The table replaces the range, if the range isn't collapsed.

事实证明,您需要通过折叠范围来“移动”到范围的末尾。如果这样做,您将遇到另一个单词问题,即如果文档中有两个紧邻的表格,单词会自动将它们连接到一个表格中。您的固定代码最终会向一个表添加越来越多的行,并不断用值覆盖前几行。所有这些都会导致以下代码应该可以解决您的问题:

    var myRange = Globals.ThisAddIn.Application.ActiveDocument.Range();
foreach (List<List<string>> ClassTable in new List<List<List<string>>> { new List<List<string>> { new List<string> { "A" }, new List<string> { "B" } }, new List<List<string>> { new List<string> { "C" }, new List<string> { "D" } } })
{
// tbl is a "Microsoft.Office.Interop.Word.Table"
// myRange is like MyDoc.Range(ref missing, ref missing)

Microsoft.Office.Interop.Word.Table tbl = null;
try
{
tbl = Globals.ThisAddIn.Application.ActiveDocument.Tables.Add(myRange, ClassTable.Count(), 3);

tbl.Borders.Enable = 1;
int RowCounter = 1;
foreach (var item in ClassTable)
{
int ColumnCounter = 1;
foreach (string str in item)
{
tbl.Cell(RowCounter, ColumnCounter).Range.Text = str;
ColumnCounter++;
}
RowCounter++;
}

// Move to the end
myRange.Collapse(Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd);
// Now add something behind the table to prevent word from joining tables into one
myRange.InsertParagraphAfter();
// gosh need to move to the end again
myRange.Collapse(Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd);

}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}

}

最后一个警告是该段中的第一行内容为:

var myRange = Globals.ThisAddIn.Application.ActiveDocument.Range();

如果文档为空,则向此范围添加表格将起作用,否则它将引发相同的异常,因为在这种情况下我们还没有到达末尾。 .Collapse() 也会在那里解决它。

关于c# - 如何: Adding Multi tables on Word with .网,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6996242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com