通过C#.NET在Word中创建动态表

时间:2008-11-12 10:28:35

标签: c# dynamic ms-word

我有一个C#应用程序,我想为程序实现一个逻辑,它将打开word文档并转到页面中的某个位置并创建一个表并将值放入其中。任何人都可以告诉我如何实现这一点。我正在使用Visual Studio 2005

6 个答案:

答案 0 :(得分:6)

以下是将datagridview复制到word表的代码:

参考是Microsoft.Office.Interop.Word C:\ Program Files(x86)\ Microsoft Visual Studio 10.0 \ Visual Studio工具用于Office \ PIA \ Office12 \ Microsoft.Office.Interop.Word.dll

using word = Microsoft.Office.Interop.Word;    
public static void ExportToWord(DataGridView dgv)
                {
                    SendMessage("Opening Word");

                    word.ApplicationClass word = null;



      word.Document doc = null;
            object oMissing = System.Reflection.Missing.Value;
            object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */ 
            try
            {
                word = new word.ApplicationClass();
                word.Visible = true;
                doc = word.Documents.Add(ref oMissing, ref oMissing,ref oMissing, ref oMissing);
            }
            catch (Exception ex)
            {
                ErrorLog(ex);
            }
            finally
            {
            }
            if (word != null && doc != null)
            {
                word.Table newTable;
                word.Range wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range;
                newTable = doc.Tables.Add(wrdRng, 1, dgv.Columns.Count-1, ref oMissing, ref oMissing);
                newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
                newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
                newTable.AllowAutoFit = true;

                foreach (DataGridViewCell cell in dgv.Rows[0].Cells)
                {
                    newTable.Cell(newTable.Rows.Count, cell.ColumnIndex).Range.Text = dgv.Columns[cell.ColumnIndex].Name;

                }
                newTable.Rows.Add();

                foreach (DataGridViewRow row in dgv.Rows)
                {
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        newTable.Cell(newTable.Rows.Count, cell.ColumnIndex).Range.Text = cell.Value.ToString();                      
                    }
                    newTable.Rows.Add();
                }                                              
            }

        }

答案 1 :(得分:3)

查找“Word Automation”。

例如,KB316384,其中包含:

本文中的示例代码演示了如何执行以下操作:

  • 插入带有文字和格式的段落。
  • 浏览和修改文档中的各种范围。
  • 插入表格,格式化表格,并用数据填充表格。
  • 添加图表。

答案 2 :(得分:0)

如果您不想使用Word Automation,例如如果您没有在运行程序的计算机上安装Word,则应该查看Aspose.Words

唯一的问题是它不是免费的。

答案 3 :(得分:0)

Word会很乐意用HTML扩展名为.Doc的文件。您可以使用内部样式表获得所需的所有格式。这里出现了一个非常类似的问题:

Export to Word Document in C#

答案 4 :(得分:0)

您可以尝试我的方法将数据导出到Word(* .docx),它易于使用,并且可以100%使用任何DataGridView,只需添加 Microsoft.Office.Interop.Word 参考并复制以下代码:

select ... for xml

谢谢。

答案 5 :(得分:0)

我有一个代码可以将表插入数据库中的特定书签检索模型中,希望对社区有所帮助,我使用mvc C#,Microsoft Office互操作性词来创建Word文件并从助手类中添加动态表

public void tableFromDatabase(Document doc, Application word, string risk, string bookmarkName, TableTemplate table) {
        Table newTable;//Create a new table
        Range wrdRng = doc.Bookmarks.get_Item(bookmarkName).Range;//Get a bookmark Range
        doc.Bookmarks[bookmarkName].Select();
        newTable = word.Selection.Tables.Add(wrdRng,1,1);//Add new table to selected bookmark by default set 1 row, 1 column (need set interval 1-63)
        newTable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
        newTable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
        int a=0, b=0;//Set integer values for iterate in model arrays
        //Iterate model rows
        for (int i = 1; i <= table.Rows.Count; i++)//Set in 1 the value because in word tables the begin is (1,1)
        {
            //Only add rows if is after first row
            if (i > 1)
            {
                newTable.Rows.Add();
            }
            //Iterate model columns from rows
            for (int j = 1; j <= table.Rows[a].Columns.Count; j++)
            {
                //Only Add rows if is after first
                if (j == 1 && i == 1)
                {
                    newTable.Cell(i, j).Range.Font.Name = table.Rows[a].Columns[b].cellFontName;
                    newTable.Cell(i, j).Range.Font.Size = table.Rows[a].Columns[b].cellFontSize;
                    newTable.Cell(i, j).Width = float.Parse(table.Rows[a].Columns[b].cellWidth);
                }
                else
                {
                    //Add Cells to rows only if columns of the model is largen than table, this is for not exceed the interval
                    if (newTable.Rows[i].Cells.Count < table.Rows[a].Columns.Count)
                    {
                        newTable.Rows[i].Cells.Add();
                    }
                    //Set the values to new table
                    //The width must be float type
                    newTable.Cell(i, j).Range.Font.Name = table.Rows[a].Columns[b].cellFontName;
                    newTable.Cell(i, j).Range.Font.Size = table.Rows[a].Columns[b].cellFontSize;
                    newTable.Cell(i, j).Width = float.Parse(table.Rows[a].Columns[b].cellWidth);
                }
                b++;
                //Set 0 to reset cycle
                if (b == table.Rows[a].Columns.Count)
                {
                    b = 0;
                }
            }
            a++;
            //Set 0 to reset cycle
            if (a == table.Rows.Count)
            {
                a = 0;
            }
        }
        newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
        newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
        newTable.AllowAutoFit = true;
        //Set gray color to borders
        newTable.Borders.InsideColor = (Microsoft.Office.Interop.Word.WdColor)12964311;
        newTable.Borders.OutsideColor = (Microsoft.Office.Interop.Word.WdColor)12964311;

    }