【问题标题】:C# docx bookmarks loopC# docx 书签循环
【发布时间】:2016-04-22 08:46:01
【问题描述】:

我想遍历文档中的所有书签并将文本设置为每个书签。名称来自已加载的 datagridview 单元格值。我被困在这个循环中。请问有什么建议吗?

using (Novacode.DocX document = DocX.Load(template))
{
    foreach (Novacode.Bookmark bookmark in document.Bookmarks)
    {
        //MessageBox.Show("\tFound bookmarks {0}", bookmark.Name);
        //var bookmarks = bookmark.Name;

        //document.Bookmarks[bookmark.Name].SetText(dataGridViewRow.Cells[0].Value.ToString());

        int i = document.Bookmarks.Count;

        var bookmarks = document.Bookmarks[i].Name;

        document.Bookmarks[bookmark.Name].SetText(dataGridViewRow.Cells[0].Value.ToString());
        document.Bookmarks[0].SetText(dataGridViewRow.Cells[1].Value.ToString());
        document.Bookmarks[1].SetText(dataGridViewRow.Cells[2].Value.ToString());
        document.Bookmarks[2].SetText(dataGridViewRow.Cells[3].Value.ToString());
        document.Bookmarks[3].SetText(dataGridViewRow.Cells[4].Value.ToString());

        //document.Bookmarks[bookmark.Name].SetText(dataGridViewRow.Cells[2].Value.ToString());
        //document.Bookmarks[bookmark.Name].SetText(dataGridViewRow.Cells[3].Value.ToString());
        //document.Bookmarks[bookmark.Name].SetText(dataGridViewRow.Cells[4].Value.ToString());
        //document.Bookmarks[bookmark.Name].SetText(dataGridViewRow.Cells[5].Value.ToString());
        //document.Bookmarks[bookmark.Name].SetText(dataGridViewRow.Cells[6].Value.ToString());
        //document.Bookmarks[bookmark.Name].SetText(dataGridViewRow.Cells[7].Value.ToString());
        //document.Bookmarks[bookmark.Name].SetText(dataGridViewRow.Cells[8].Value.ToString());
        //document.Bookmarks[bookmark.Name].SetText(dataGridViewRow.Cells[9].Value.ToString());
        //document.Bookmarks[bookmark.Name].SetText(dataGridViewRow.Cells[10].Value.ToString());
        //document.Bookmarks[bookmark.Name].SetText(dataGridViewRow.Cells[11].Value.ToString());
    }                   
    document.SaveAs(path2);
}

【问题讨论】:

  • 您是说您知道如何设置属性但不知道如何循环执行?
  • int i = document.Bookmarks.Count置于循环外。
  • @Alex,是的,没错。我想使用 datagridview 中的值为文档内的所有书签设置 .Name 属性。例如,第一个找到的书签将从 dataGridViewRow.Cells[0] 获取值,第二个将从 dataGridViewRow.Cells[1] 获取值,等等......但我被卡住了。

标签: c# loops docx bookmarks


【解决方案1】:

如果我理解正确,这就是你试图通过循环实现的目标:

using (Novacode.DocX document = DocX.Load(template))
{
int i = 0;

foreach (Novacode.Bookmark bookmark in document.Bookmarks)
{
    var bookmarks = document.Bookmarks[i].Name;

    document.Bookmarks[bookmark.Name].SetText(dataGridViewRow.Cells[i+1].Value.ToString());

    i++;
}                   
document.SaveAs(path2);
}

我们在这里所做的是声明了一个变量i,它在循环之外,但我们会在每次 foreach 迭代时增加它的值。或者,您可以重写循环并改用 for 循环:

for(int i=0; i< document.Bookmarks.Count)
{
       //change the code here accordingly
}

如果这有帮助,请告诉我。 谢谢。

【讨论】:

  • tnx 伙计,它正在工作,但是当涉及到最后一个书签时,它会绑定最后两个单元格值。我猜这是因为 [i+1],然后当我只放 [i] 时,它会绑定上一列。怎么办?
  • @dilesko 所以它对除了最后一个书签之外的所有内容都适用?
  • 是的,当“i”计数达到最后一个书签时,来自 datagridview 单元格的值,例如单元格 [4] 在那里,加上来自下一个单元格 [5] 的值。所以基本上在书签位置创建的文档里面是单元格[4].value +单元格[5].value,而不仅仅是单元格[4].value。你了解我吗? :)
  • @dilesko 是的,我了解您的查询...但是,此逻辑与其他项目不一致吗?在其他地方,gridview 中书签 id 和单元格之间的差异为 1。您可以避免进行最后一次迭代或在代码中使用 if 语句来满足这种特殊情况
  • 我终于成功了。内部文档中有一些东西,我猜它是一个带有值“_GoBack”的内置书签。 for (int i = 0; i
猜你喜欢
  • 2017-08-06
  • 1970-01-01
  • 2020-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多