InBlock.gif using Word;
InBlock.gif下面的例子中包括C#对Word文档的创建、插入表格、设置样式等操作:
InBlock.gif
InBlock.gif(例子中代码有些涉及数据信息部分被省略,重要是介绍一些C#操作word文档的方法)
InBlock.gif
InBlock.gif                 public string CreateWordFile( string CheckedInfo)
InBlock.gif                ...{
InBlock.gif                         string message = "";
InBlock.gif                         try
InBlock.gif                        ...{
InBlock.gif                                Object Nothing = System.Reflection.Missing.Value;
InBlock.gif                                Directory.CreateDirectory( "C:/CNSI");     //创建文件所在目录
InBlock.gif                                 string name = "CNSI_" + DateTime.Now.ToShortString()+ ".doc";
InBlock.gif                                 object filename = "C://CNSI//" + name;    //文件保存路径
InBlock.gif                                //创建Word文档
InBlock.gif                                Word.Application WordApp = new Word.ApplicationClass();
InBlock.gif                                Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
InBlock.gif
InBlock.gif                                //添加页眉
InBlock.gif                                WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
InBlock.gif                                WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
InBlock.gif                                WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[页眉内容]");
InBlock.gif                                WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐
InBlock.gif                                WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页眉设置
InBlock.gif
InBlock.gif                                WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距
InBlock.gif
InBlock.gif                                //移动焦点并换行
InBlock.gif                                object count = 14;
InBlock.gif                                object WdLine = Word.WdUnits.wdLine;//换一行;
InBlock.gif                                 WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
InBlock.gif                                 WordApp.Selection.TypeParagraph();//插入段落
InBlock.gif
InBlock.gif                                 //文档中创建表格
InBlock.gif                                 Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref Nothing, ref Nothing);
InBlock.gif                                 //设置表格样式
InBlock.gif                                 newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleThickThinLargeGap;
InBlock.gif                                 newTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
InBlock.gif                                 newTable.Columns[1].Width = 100f;
InBlock.gif                                 newTable.Columns[2].Width = 220f;
InBlock.gif                                 newTable.Columns[3].Width = 105f;
InBlock.gif
InBlock.gif                                 //填充表格内容
InBlock.gif                                 newTable.Cell(1, 1).Range.Text = "产品详细信息表";
InBlock.gif                                 newTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体
InBlock.gif                                 //合并单元格
InBlock.gif                                 newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));
InBlock.gif                                 WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
InBlock.gif                                 WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
InBlock.gif                                                
InBlock.gif                                 //填充表格内容
InBlock.gif                                 newTable.Cell(2, 1).Range.Text = "产品基本信息";
InBlock.gif                                 newTable.Cell(2, 1).Range.Font.Color = Word.WdColor.wdColorDarkBlue;//设置单元格内字体颜色
InBlock.gif                                 //合并单元格
InBlock.gif                                 newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));
InBlock.gif                                 WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
InBlock.gif
InBlock.gif                                    //填充表格内容
InBlock.gif                                    newTable.Cell(3, 1).Range.Text = "品牌名称:";
InBlock.gif                                    newTable.Cell(3, 2).Range.Text = BrandName;
InBlock.gif                                    //纵向合并单元格
InBlock.gif                                    newTable.Cell(3, 3).Select();//选中一行
InBlock.gif                                    object moveUnit = Word.WdUnits.wdLine;
InBlock.gif                                    object moveCount = 5;
InBlock.gif                                    object moveExtend = Word.WdMovementType.wdExtend;
InBlock.gif                                     WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
InBlock.gif                                     WordApp.Selection.Cells.Merge();
InBlock.gif                                     //插入图片
InBlock.gif                                     string FileName = Picture;//图片所在路径
InBlock.gif                                     object LinkToFile = false;
InBlock.gif                                     object SaveWithDocument = true;
InBlock.gif                                     object Anchor = WordDoc.Application.Selection.Range;
InBlock.gif                                     WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
InBlock.gif                                        WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 100f;//图片宽度
InBlock.gif                                        WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 100f;//图片高度
InBlock.gif                                        //将图片设置为四周环绕型
InBlock.gif                                        Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
InBlock.gif                                        s.WrapFormat.Type = Word.WdWrapType.wdWrapSquare;
InBlock.gif                                                
InBlock.gif                                        newTable.Cell(12, 1).Range.Text = "产品特殊属性";
InBlock.gif                                        newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));
InBlock.gif                                         //在表格中增加行
InBlock.gif                                         WordDoc.Content.Tables[1].Rows.Add(ref Nothing);
InBlock.gif                                            
InBlock.gif                                         WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();//“落款”
InBlock.gif                                         WordDoc.Paragraphs.Last.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
InBlock.gif
InBlock.gif                                        //文件保存
InBlock.gif                                        WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
InBlock.gif                                        WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
InBlock.gif                                        WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
InBlock.gif                                        message=name+"文档生成成功,以保存到C:CNSI下";
InBlock.gif                        }
InBlock.gif                        catch
InBlock.gif                        ...{
InBlock.gif                                message = "文件导出异常!";
InBlock.gif                        }
InBlock.gif                        return message;
InBlock.gif                }