将List<string>插入word文档

Insert List<string> into word document

我目前正在尝试找到一种方法来读入并将数据插入到 word 文档中。到目前为止,这是我得到的:

class Program
{
    static void Main(string[] args)
    {
        var FileName = @"C:\temp\test.DOC";
        List<string> data = new List<string>();
        Application app = new Application();
        Document doc = app.Documents.Open(@"C:\temp\test.DOC");

        foreach (Paragraph objParagraph in doc.Paragraphs)
        {
            data.Add(objParagraph.Range.Text.Trim());
        }
        //data.Insert


        data.Insert(16, "Test 1");
        data.Insert(16, "\tTest 2\tName\tAmount");
        data.Insert(16, "Test 3");
        data.Insert(16, "Test 4");
        data.Insert(16, "Test 5");
        data.Insert(16, "Test 6");
        data.Insert(16, "Test 7");
        data.Insert(16, "Test 8");
        data.Insert(16, "Test 9");
        data.Insert(16, "Test 10");

        var x = doc.Paragraphs.Add();
        x.Range.Text.Insert(0,"\tTest 2\tName\tAmount");

        doc.SaveAs2(@"C:\temp\test3.DOC");

        ((_Document)doc).Close();
        ((_Application)app).Quit();

    }
}

现在,这成功填充了列表数据 - 但我正在尝试将每个新测试元素附加到第 [16] 个索引处,并将其保存到 word 文档中。有没有一种简单的方法可以做到这一点,或者我只是 over-thinking 这个问题?

我意识到字符串列表与代表单词文档的文档 object 是分开的。

我在文档中的其他一些地方使用书签来添加数据,但我认为在这种情况下不可能使用书签来放置数据 - 或者如果我没有使用书签我想远离它。

编辑:我试图在数据 [] 中的第 [16] 个位置插入 X 个元素。

编辑 2: 本质上,我是动态获取数据的,我不确定需要向文档中添加多少 records/rows,因此可能如下所示:

[15]
[16]\tName\tID\tAMOUNT
[17]\tName\tID\tAMOUNT
[18]\tName\tID\tAMOUNT

因为 headers 已经存在 (NAME,ID,AMOUNT),每次我 运行 程序我都不确定我要插入多少元素文档 - 只要每个元素都放在另一个元素之下,并且在文档模板的第 16 行,我的设置应该可以完成我想要做的事情。

图像 1 - 图像到字符串数组 图像 2 - 将内容添加到字符串后的图像 - 这就是生成的文档。 (这个要存)

我试图将每个元素即:Test1 Test2 Test3 放在各自的列中(见上文)

我再次对为什么要将单词文件读入字符串列表数组感到困惑。这只是将您在第 15 行之后显示的文本添加到 word 文档中。您没有指定测试 1、测试 2、测试 3... 的来源。

编辑: 添加了一个 try-catch 以防文档没有至少 16 个段落。

static void Main(string[] args)
{

  List<string> data = new List<string>();
  Application app = new Application();
  Document doc = app.Documents.Open(@"C:\temp\test.DOC");

  string testRows = "Test 1\n\tTest 2\tName\tAmount\nTest 3\nTest 4\nTest 5\nTest 6\nTest 7\nTest 8\nTest 9\nTest 10\n";

  try
  {
    var x = doc.Paragraphs[16];
    x = doc.Paragraphs.Add(x.Range);
    x.Range.Text = testRows;
    doc.SaveAs2(@"C:\temp\test3.DOC");
  }
  catch (System.Runtime.InteropServices.COMException e)
  {
    Console.WriteLine("COMException: " +  e.StackTrace.ToString());
    Console.ReadKey();
  }
  ((_Document)doc).Close();
  ((_Application)app).Quit();

}

所以我发现(出于我的目的)最简单的方法是通过插入特定段落将字符串列表插入由制表符分隔的临时列中。

因为我也使用书签来放置文本 - 我发现从文档的副本开始工作而不是每次都担心 removing/creating 个书签很有用。

填充要放置在特定段落标记处的列表时,动态添加制表符和换行符很有用。稍后这将使遍历列表并将它们很好地放在文档中变得更容易。

根据您打算放置列的方式,必须确定一些逻辑以确保 space 一切正确。我通过为列和修剪创建最大长度,并通过添加特定数量的制表符来适应 smaller/larger 长度来做到这一点。

因此,我正在填充的列如下所示:

myList.Add("\t12345678912345\tJohn Doe\t\t\t\t123456\r\n");
myList.Add("7654321654987\tJohn Smith\t\t\t765\r\n");

这些行将插入第 17 段并整齐地放在 headers 下。

最后,我决定使用书签来放置单行文本,例如日期、标题和签名值,因为这些值不需要正确 spaced 或任何内容。

最后我删除了我正在处理的 word 文档的副本,并删除了 pdf(因为在我的情况下我是通过电子邮件发送的)

感谢@JohnG 的帮助 - 我希望这个答案可以帮助遇到它的其他人。我删除了 try-catch,因为我也在使用模板。

File.Copy(sCurrentPath + "\" + "testTemplate.DOC", sCurrentPath + "\" + "test.DOC");

Application app = new Application();
Document doc = app.Documents.Add(sCurrentPath + "\" + "test.DOC");

foreach (string sValue in myList)
{
    var List = doc.Paragraphs[17];
    myList = doc.Paragraphs.Add(myList.Range);
    myList.Range.Text = sValue;
}

if (doc.Bookmarks.Exists("Date"))
{
    object oBookMark = "Date";
    doc.Bookmarks.get_Item(ref oBookMark).Range.Text = DateTime.Now.ToString("MM/dd/yyyy");
}
if (doc.Bookmarks.Exists("Signature"))
{
    object oBookMark = "Signature";
    doc.Bookmarks.get_Item(ref oBookMark).Range.Text = "My Name";
}
if (doc.Bookmarks.Exists("Title"))
{
    object oBookMark = "Title";
    doc.Bookmarks.get_Item(ref oBookMark).Range.Text = "Title Here";
}

doc.ExportAsFixedFormat(sCurrentPath + "\" + "test.pdf", WdExportFormat.wdExportFormatPDF);

File.Delete(sCurrentPath + "\" + "testCopy.DOC");

File.Delete(sCurrentPath + "\" + "test.pdf");

((_Document)doc).Close();
((_Application)app).Quit();