我有一个程序,我试图将数据库中的字段放入包含书签的Word文档中。我可以一次将其用于一个条目,但是我试图使用包含书签的文档来使其读取数据集并将数据集中的每一行放在新页面上。我能够将其放置在第一页上,但是当它读取第二行时,它将用该内容替换第一行中的内容。有没有一种方法可以使用其中带有书签的文档为每一行创建新页面?
下面是我当前正在使用的代码。
使用Word = Microsoft.Office.Interop.Word;
public static void printAll()
{
SQLiteConnection con = new SQLiteConnection(ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString);
SQLiteDataAdapter da;
DataSet ds;
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
//Start Word and open a document.
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Open(Path.Combine(Environment.CurrentDirectory, "Recipe.docx"));
Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
string Title = null;
string Author;
string Type;
string Recipe;
string query = "SELECT RecipeID, Type, Name, Author, Recipe FROM Recipes";
da = new SQLiteDataAdapter(query, con);
ds = new DataSet();
da.Fill(ds, "Recipes");
for (int y = 0; y < ds.Tables[0].Rows.Count; y++)
{
Title = ds.Tables[0].Rows[y]["Name"].ToString();
Author = ds.Tables[0].Rows[y]["Author"].ToString();
Type = ds.Tables[0].Rows[y]["Type"].ToString();
Recipe = Dangl.TextConverter.Rtf.RtfToText.ConvertRtfToText(ds.Tables[0].Rows[y]["Recipe"].ToString());
//Inserts title string into title bookmark in word document
Dictionary<string, string> titleBold = new Dictionary<string, string> { { "Title", Title } };
foreach (var bookmark in titleBold)
{
Word.Bookmark bm = oDoc.Bookmarks[bookmark.Key];
Word.Range range = bm.Range;
range.Text = bookmark.Value;
range.Font.Bold = 5;
oDoc.Bookmarks.Add(bookmark.Key, range);
}
// Inserts author, type and recipe into corosponding bookmarks in word document
Dictionary<string, string> bookmarks = new Dictionary<string, string> { { "Author", Author }, { "Type", Type }, { "Recipe", Recipe } };
foreach (var bookmark in bookmarks)
{
Word.Bookmark bm = oDoc.Bookmarks[bookmark.Key];
Word.Range range = bm.Range;
range.Text = bookmark.Value;
range.Font.Bold = 0;
oDoc.Bookmarks.Add(bookmark.Key, range);
}
object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range.InsertParagraphAfter();
object oPageBreak = Word.WdBreakType.wdPageBreak;
wrdRng.InsertBreak(ref oPageBreak);
wrdRng.Collapse(ref oCollapseEnd);
wrdRng.InsertAfter(Title);
wrdRng.InsertParagraphAfter();
}
}
任何帮助将不胜感激。预先感谢
更新 我已经在辛迪·梅斯特(Cindy Meister)的回答下工作了。 现在,我可以使用构件块将书签作为数据集中的每个条目。 现在,我添加了将每个数据集条目放在其自己的页面上的功能。我仍在努力使它不打印末尾带有构建基块的空白页。
以下是任何可能正在寻找此解决方案的人的工作代码。
public static void printAll()
{
SQLiteConnection con = new SQLiteConnection(ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString);
SQLiteDataAdapter da;
DataSet ds;
object oMissing = Missing.Value;
object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
//Start Word and open a document.
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Open(Path.Combine(Environment.CurrentDirectory, "Recipe.dotx"));
Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
string Title;
string Author;
string Type;
string Recipe;
string query = "SELECT RecipeID, Type, Name, Author, Recipe FROM Recipes";
da = new SQLiteDataAdapter(query, con);
ds = new DataSet();
da.Fill(ds, "Recipes");
for (int y = 0; y < ds.Tables[0].Rows.Count; y++)
{
Title = ds.Tables[0].Rows[y]["Name"].ToString();
Author = ds.Tables[0].Rows[y]["Author"].ToString();
Type = ds.Tables[0].Rows[y]["Type"].ToString();
Recipe = Dangl.TextConverter.Rtf.RtfToText.ConvertRtfToText(ds.Tables[0].Rows[y]["Recipe"].ToString());
//Inserts title string into title bookmark in word document
Dictionary<string, string> titleBold = new Dictionary<string, string> { { "Title", Title } };
foreach (var bookmark in titleBold)
{
Word.Bookmark bm = oDoc.Bookmarks[bookmark.Key];
Word.Range range = bm.Range;
range.Text = bookmark.Value;
range.Font.Bold = 5;
oDoc.Bookmarks.Add(bookmark.Key, range);
}
// Inserts author, type and recipe into corosponding bookmarks in word document
Dictionary<string, string> bookmarks = new Dictionary<string, string> { { "Author", Author }, { "Type", Type }, { "Recipe", Recipe } };
foreach (var bookmark in bookmarks)
{
Word.Bookmark bm = oDoc.Bookmarks[bookmark.Key];
Word.Range range = bm.Range;
range.Text = bookmark.Value;
range.Font.Bold = 0;
oDoc.Bookmarks.Add(bookmark.Key, range);
}
object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range.InsertParagraphAfter();
wrdRng.Collapse(ref oCollapseEnd);
Object nullobj = System.Reflection.Missing.Value;
Object objBreak = Word.WdBreakType.wdPageBreak;
Object objUnit = Word.WdUnits.wdStory;
oWord.Selection.EndKey(ref objUnit, ref nullobj);
oWord.Selection.InsertBreak(ref objBreak);
oWord.NormalTemplate.BuildingBlockEntries.Item("BuildingBlockBookmarks").Insert(oDoc.Characters.Last, true);
}
}
答案 0 :(得分:0)
可以使用Building Block插入“样板文字”。一个Building Block可以包含几乎所有内容。
插入带有书签的Building Block将删除文档前面的所有“具有相同名称”的书签。这意味着,在为第一行数据填充书签之后,插入构件块,然后将下一行数据放入该组书签中。依此类推。
Save Selection to Quick Part Gallery
。在对话框中输入“名称”,“类别”,“图库”等信息。这将创建一个构建块。以下代码示例演示了如何将Normal.dotm中存储的名为BB with bookmarks
的构建基块插入文档的末尾。
oWord.NormalTemplate.BuildingBlockEntries.Item("BB with bookmarks").Insert(oDoc.Characters.Last, true);