C#将多个单元格从Excel复制到同一列中的Word

时间:2019-07-08 17:11:34

标签: c# excel ms-word

我试图用Excel数据填充Word文档,但是我只能填充每列的第一个单元格。如何填写多个列?

I want to change "missing" into "credits" somehow but I really don't know how to correctly do it: 

app.Selection.Find.Execute ("credits", missing, missing, missing, missing, missing, missing, missing, missing, tmp [0], 2);


private void Button1_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = null;
object filename = "D:\\Exceltowordtemp\\report1.docx";
object missing = Type.Missing;
for (int i = 0; i < 2; i++)
{
doc = app.Documents.Open(filename, missing, missing);
app.Selection.Find.ClearFormatting();
app.Selection.Find.Replacement.ClearFormatting();

string[] tmp = new string[4];
tmp = readExcel(i);

app.Selection.Find.Execute("<credits>", missing, missing, missing, missing, missing, missing, missing, missing, tmp[0],2);
app.Selection.Find.Execute("<marks>", missing, missing, missing, missing, missing, missing, missing, missing, tmp[1],2);
app.Selection.Find.Execute("<ng>", missing, missing, missing, missing, missing, missing, missing, missing, tmp[2],2);
app.Selection.Find.Execute("<eg>", missing, missing, missing, missing, missing, missing, missing, missing, tmp[3],2);

object SaveAsFile = (object)"D:\\Exceltowordtemp\\outcomes" + tmp[0] + ".doc";
doc.SaveAs2(SaveAsFile, missing, missing, missing);
}

我希望同一列中的多个单元格在一个Word文档中输出。现在每列只有一个

1 个答案:

答案 0 :(得分:0)

不幸的是,出于您的目的,Word并非旨在以这种方式工作。不可能一步一步地将数组写到Word表中,无论是行还是列。

Word可以做的就是将以字符分隔的文本转换为表格,但这就是它的范围。

这意味着代码将需要循环tmp[]数组以填充列中的每个单元格。