日期:2014-05-19  浏览次数:21035 次

菜鸟请问高手一个c#动态生成word的问题。
我是用c#做的应用程序。
代码我是这样的。
string   message   =   " ";
                        try
                        {
                                Object   Nothing   =   System.Reflection.Missing.Value;
                                System.IO.Directory.CreateDirectory( "C://CNSI ");
                                //Directory.CreateDirectory( "C:/CNSI ");     //创建文件所在目录
                                string   name   =   "CNSI_ "   +   DateTime.Now.ToShortTimeString()+ ".doc ";
                                object   filename   =   "C://CNSI// "   +   name;     //文件保存路径
                                //创建Word文档
                                Microsoft.Office.Interop.Word.Application   WordApp   =   new   Microsoft.Office.Interop.Word.ApplicationClass();
                                Microsoft.Office.Interop.Word.Document   WordDoc   =   WordApp.Documents.Add(ref   Nothing,   ref   Nothing,   ref   Nothing,   ref   Nothing);

                                //添加页眉
                                WordApp.ActiveWindow.View.Type   =   Microsoft.Office.Interop.Word.WdViewType.wdOutlineView;
                                WordApp.ActiveWindow.View.SeekView   =   Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;
                                WordApp.ActiveWindow.ActivePane.Selection.InsertAfter( "[页眉内容] ");
                                WordApp.Selection.ParagraphFormat.Alignment   =   Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐
                                WordApp.ActiveWindow.View.SeekView   =   Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;//跳出页眉设置

                                WordApp.Selection.ParagraphFormat.LineSpacing   =   15f;//设置文档的行间距

                                //移动焦点并换行