通过C#.NET在Word中创建动态表 [英] Creating Dynamic Tables in Word by C#.NET

查看:120
本文介绍了通过C#.NET在Word中创建动态表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

这里是将datagridview复制到单词表的代码:



参考是Microsoft.Office.Interop.Word
C:\Program文件(x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office12\ Microsoft.Office.Interop.Word.dll

 使用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是一个预定义的书签* /
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。表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(dgv.Rows [0]中的DataGridViewCell单元格.Cells)
{
newTable.Cell(newTable.Rows.Count,cell.ColumnIndex).Range.Text = dgv.Columns [cell.ColumnIndex]请将.Name;

}
newTable.Rows.Add();

foreach(dgv.Rows中的DataGridViewRow行)
{
foreach(row.Cells中的DataGridViewCell单元格)
{
newTable.Cell(newTable。 Rows.Count,cell.ColumnIndex).Range.Text = cell.Value.ToString();
}
newTable.Rows.Add();
}
}

}


I have a C# application where i want to implement a logic for a programm which will open the word document and go to a certain place in the page and create a Table and put values in that. Can any one tell me how to implement this. I am using Visual studio 2005

解决方案

Here is code to copy datagridview to a word table:

Reference is Microsoft.Office.Interop.Word C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for 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();
                }                                              
            }

        }

这篇关于通过C#.NET在Word中创建动态表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆