问题
Here's my dilemma. I've been given a task to generate a specific section of an existing word document based on user input from a web front end. The back end of the system is being written in C# with the part that edits the word document using the Microsoft.Office.Interop.Word
namespace.
Basically, they would select from a list of available instructions, each being of the type string
, which will then be used to generate the instructions part of the document, with each separate instruction being another bullet in the list. This part works fine. My problem is, the instructions can contain the character \
, which need to be replaced with an indent, or the equivalent of hitting TAB while in the bullet if you had the document opened in word. So far I'm able to get it to insert the bullets into the middle of the list just fine, it continues numbering them appropriately as expected. The kicker is I can't get it to indent them as needed.
I've tried pretty much all of the examples I was able to find here and on a few other sites to get this to work but with no success. The latest iteration is in the code below, which just indents the entire list as far as it can go.
var bookmark = "bookMarkName";
var docPath = @"c:\temp\Template.docx";
var app = new Application();
var doc = app.Documents.Open(docPath);
var range = doc.Bookmarks[bookmark].Range;
var listTemplate = range.ListFormat.ListTemplate;
range.ListFormat.ApplyListTemplate(listTemplate);
string[] bulletList = new string[] {
@"Point A",
@"\Point B",
@"\Point C",
@"\\Point D",
@"Point E"
}
var count = bulletList.Length;
for (var i = 0; i < count; i++)
{
var listLevel = 0;
var currentItem = bulletList[i];
var item = currentItem.Replace(@"\", "");
if (i < count - 1)
item = item + "\n";
listLevel += currentItem.ToList().Where(x => x == '\\').Select(x => x).Count();
for (var x = 0; x < listLevel; x++)
{
range.ListFormat.ListIndent();
}
range.InsertAfter(item);
}
doc.SaveAs(@"c:\temp\" + DateTime.Now.Ticks + ".docx");
doc.Close();
So the output of my code should be:
- 1 Point A
- 1.1 Point B
- 1.2 Point C
- 1.2.1 Point D
- 2 Point E
This is the first time I've ever really had to work with the Office Interop libraries so I'm positive there's something I'm missing here. Any help would be greatly appreciated.
回答1:
I don't have Office Interop on my computer, but you could try building the list with DocX, writing it to a file, then inserting that list into your document, from said file.
Something like this:
using System.Collections.Specialized;
...
...
DocX doc = DocX.Create("bullet-text.docx");
var firstItem = bulletList[0];
var firstItemLevel = firstItem.ToList().Count(c => c == '\\');
// Using full Namespace to avoid ambiguous reference error.
Xceed.Words.NET.List list = doc.AddList(firstItem.Replace("\\", ""), firstItemLevel, ListItemType.Numbered);
for (var i = 1; i < count; i++)
{
var currentItem = bulletList[i];
var item = currentItem.Replace(@"\", "");
int listLevel = currentItem.ToList().Count(c => c == '\\')
doc.AddListItem(list, item, listLevel, ListItemType.Numbered);
}
doc.InsertList(list);
doc.Save();
// Collapse the range to the end, as to not overwrite it. Unsure if you need this
range.Collapse(WdCollapseDirection.wdCollapseEnd);
// Insert into the selected range
range.InsertFile(Environment.CurrentDirectory + "\\bullet-text.docx");
References I canibalized:
Nested Bulleted lists in Novacode docx
How to embed file to word docx?
Collapse
InsertFile
回答2:
Please use this Code But Firstly Add DocX DLL
using (var document = DocX.Create(@"docs\Lists.docx"))
{
var numberedList = document.AddList("First List Item.", 0, ListItemType.Numbered);
//Add a numbered list starting at 2
document.AddListItem(numberedList, "Second List Item.");
document.AddListItem(numberedList, "Third list item.");
document.AddListItem(numberedList, "First sub list item", 1);
document.AddListItem(numberedList, "Nested item.", 2);
document.AddListItem(numberedList, "Fourth nested item.");
var bulletedList = document.AddList("First Bulleted Item.", 0, ListItemType.Bulleted);
document.AddListItem(bulletedList, "Second bullet item");
document.AddListItem(bulletedList, "Sub bullet item", 1);
document.AddListItem(bulletedList, "Second sub bullet item", 2);
document.AddListItem(bulletedList, "Third bullet item");
document.InsertList(numberedList);
document.InsertList(bulletedList);
document.Save();
Console.WriteLine("\tCreated: docs\\Lists.docx");
}
Find Reference From Here
来源:https://stackoverflow.com/questions/51031008/c-insert-and-indent-bullet-points-at-bookmark-in-word-document-using-office-in