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