葡京网投哪个正规 > 新葡亰-办公 > Word2003无法打印多篇文档的属性信息

原标题:Word2003无法打印多篇文档的属性信息

浏览次数:175 时间:2020-03-24

在 Microsoft Word 的最先版本中,可选用“打字与印刷”对话框使 Word 贰回打字与印刷多篇文书档案的性质音讯。未来 Word 的打印成效已更新为与别的 Microsoft Windows 程序相仿,不可能贰遍打印多篇文书档案的天性消息。可是仍可叁次打字与印刷一篇文书档案的属性音讯。

c# 操作Word总结

  在治疗管理类别中为保存病者的体格检查和医疗记录,方便以往的医务职员或其余人查看。当把多长史存到数据库中,须要新建非常多的字段,何况操作很麻烦,于是想到网页的音信制造到一个word文本中,在体现的时,能够在线展开word,也足以把word转变来html标签展现。 那样使用word替代网页的因由有:   第一:网页生成数学公式和特殊符号存款和储蓄和体现比较辛劳(怎样操作word生成数学公式,有待测验)   第二:生成Word版的告诉更易于存档和未有条件下的传阅及打印  第三:顾客直接操作Word以为更亲昵,并且十一分领悟  Msdn上的word操作api(可是唯有西班牙语版,芬兰语差的先闪过) Word二〇〇六的API: Word2010的API:
  Word对象模型  (.Net Perspective)   中国共产党第五次全国代表大会对象 Application           :代表Microsoft Word应用程序本身  是Document和Selection的基类。通过Application的属性和方法,大家得以调节Word的大景况。 Document            :代表贰个Word文书档案  当您新建贰个Word文书档案只怕张开三个原来就有的Word文书档案,你将创造五个Document对象,该指标被投入到Words Documents Collection中。具有宗旨的Document称为ActiveDocument,能够透过Application对象的ActiveDocument属性取稳当前文书档案对象 Selection              :代表当前当选的区域(高亮卡塔尔,未有当选区域时代表光标点   它平日是高亮显示的(举个例子,你要转移一段文字的字体,你首先得选中这段文字,那么选中的那块区域正是眼前文书档案的Selection对象所蕴藏的区域) Bookmarks           :书签     1>书签日常常盛名字     2>Saved with the document,且文书档案关闭了随后书签继续存在     3>书签日常是暗藏的,但也能够透过代码设置其为可以看到
Range                  :代表一块区域,与Selection相通,可是貌似不可以知道     1>包涵三个开局位置和多个说尽地方     2>它能够分包光标点,一段文本大概全部文书档案     3>它包括空格,tab以至paragraph marks   4>它能够是近些日子选中的区域,当然也得以不是当下相中区域     5>它被动态成立     6>当您在三个Range的最后插入文本,那将扩充该Range

  word文书档案对象的组织图 图片 1
有关指标的详细使用,能够参谋msdn api

实例使用  

始建Word 文书档案所使用的要害方法是通过微软公司提供的Microsoft Word X Object Library, 在那之中X 为版本号。Word2010对应14.0, Word 2006 对应12.0,Word 二〇〇一 对应11.0。 通过在品种中增加该零器件,就可以使用微软集团提供的办法成立相应版本的Word 文书档案。 在实例中自己将所要生成word的格式设置为二〇〇〇本子

新建三个winForm项目文件, Com组件中增多 Microsoft Word 12.0 Object Library,援引面板中多出Microsoft.Office.Core、Microsoft.Office.Interop.Word五个引用。 在类公事中充分应用如下: using MSWord = Microsoft.Office.Interop.Word; using System.IO; using System.Reflection; using Microsoft.Office.Interop.Word;

  上边从word创造、格式设置、文本增添、图片增加、表格增多体现部分代码:

图片 2图片 3

void CreateWord()
        {
            object path;//文件路径
            string strContent;//文件内容
            MSWord.Application wordApp;//Word应用程序变量
            MSWord.Document wordDoc;//Word文档变量
            path = "d:\myWord.doc";//保存为Word2003文档
           // path = "d:\myWord.doc";//保存为Word2007文档
            wordApp = new MSWord.ApplicationClass();//初始化
            if (File.Exists((string)path))
            {
                File.Delete((string)path);
            }
            //由于使用的是COM 库,因此有许多变量需要用Missing.Value 代替
            Object Nothing = Missing.Value;
            //新建一个word对象
            wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
            //WdSaveDocument为Word2003文档的保存格式(文档后缀.doc)wdFormatDocumentDefault为Word2007的保存格式(文档后缀.docx)
            object format = MSWord.WdSaveFormat.wdFormatDocument;
           //将wordDoc 文档对象的内容保存为DOC 文档,并保存到path指定的路径
            wordDoc.SaveAs(ref path, ref format, 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);
            //关闭wordDoc文档
            wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
            //关闭wordApp组件对象
            wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
            Response.Write("<script>alert('" + path + ": Word文档创建完毕!');</script>");
        }

创建Word文档

 

图片 4图片 5

private void SetWordStyle()
        {
            object path;//文件路径
            string strContent;//文件内容
            MSWord.Application wordApp;//Word应用程序变量
            MSWord.Document wordDoc;//Word文档变量
            path = "d:\myWord.doc";//保存为Word2003文档
            // path = "d:\myWord.doc";//保存为Word2007文档
            wordApp = new MSWord.ApplicationClass();//初始化
            if (File.Exists((string)path))
            {
                File.Delete((string)path);
            }
            Object Nothing = Missing.Value;
            wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

            //页面设置
            wordDoc.PageSetup.PaperSize = Microsoft.Office.Interop.Word.WdPaperSize.wdPaperA4;//设置纸张样式
            wordDoc.PageSetup.Orientation = Microsoft.Office.Interop.Word.WdOrientation.wdOrientPortrait;//排列方式为垂直方向
            wordDoc.PageSetup.TopMargin = 57.0f;
            wordDoc.PageSetup.BottomMargin = 57.0f;
            wordDoc.PageSetup.LeftMargin = 57.0f;
            wordDoc.PageSetup.RightMargin = 57.0f;
            wordDoc.PageSetup.HeaderDistance = 30.0f;//页眉位置

            //设置页眉
            wordApp.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView;//视图样式
            wordApp.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;//进入页眉设置,其中页眉边距在页面设置中已完成
            wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;

            //插入页眉图片(测试结果图片未插入成功)
            wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
            string headerfile = "d:\header.jpg";
            Microsoft.Office.Interop.Word.InlineShape shape1 = wordApp.ActiveWindow.ActivePane.Selection.InlineShapes.AddPicture(headerfile, ref Nothing, ref Nothing, ref Nothing);
            shape1.Height = 20;
            shape1.Width = 80;
            wordApp.ActiveWindow.ActivePane.Selection.InsertAfter("  文档页眉");
            //去掉页眉的横线
            wordApp.ActiveWindow.ActivePane.Selection.ParagraphFormat.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].LineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleNone;
            wordApp.ActiveWindow.ActivePane.Selection.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].Visible = false;
            wordApp.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;//退出页眉设置

            //为当前页添加页码
            Microsoft.Office.Interop.Word.PageNumbers pns = wordApp.Selection.Sections[1].Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers;//获取当前页的号码
            pns.NumberStyle = Microsoft.Office.Interop.Word.WdPageNumberStyle.wdPageNumberStyleNumberInDash;
            pns.HeadingLevelForChapter = 0;
            pns.IncludeChapterNumber = false;
            pns.RestartNumberingAtSection = false;
            pns.StartingNumber = 0;
            object pagenmbetal = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter;//将号码设置在中间
            object first = true;
            wordApp.Selection.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers.Add(ref pagenmbetal, ref first);


            object format = MSWord.WdSaveFormat.wdFormatDocument;
            wordDoc.SaveAs(ref path, ref format, 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);
            wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
            wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
            Response.Write("<script>alert('" + path + ": Word文档格式设置完毕!');</script>");
        }

安装Word文书档案格式

效果图:

图片 6

图片 7图片 8

private void AddWordText()
        {
            object path;//文件路径
            string strContent;//文件内容
            MSWord.Application wordApp;//Word应用程序变量
            MSWord.Document wordDoc;//Word文档变量
            path = "d:\myWord.doc";//保存为Word2003文档
            // path = "d:\myWord.doc";//保存为Word2007文档
            wordApp = new MSWord.ApplicationClass();//初始化
            if (File.Exists((string)path))
            {
                File.Delete((string)path);
            }
            Object Nothing = Missing.Value;
            wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

            wordApp.Selection.ParagraphFormat.LineSpacing = 35f;//设置文档的行间距
            //写入普通文本
            wordApp.Selection.ParagraphFormat.FirstLineIndent = 30;//首行缩进的长度
            strContent = "c#向Word写入文本   普通文本:n";  
            wordDoc.Paragraphs.Last.Range.Text = strContent;

            //将文档的前三个字替换成"asdfasdf",并将其颜色设为蓝色
            object start = 0;
            object end = 3;
            Microsoft.Office.Interop.Word.Range rang = wordDoc.Range(ref start, ref end);
            rang.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBrightGreen;
            rang.Text = "我是替换文字";
            wordDoc.Range(ref start, ref end);



            //写入黑体文本
            object unite = Microsoft.Office.Interop.Word.WdUnits.wdStory;
            wordApp.Selection.EndKey(ref unite, ref Nothing);
            wordApp.Selection.ParagraphFormat.FirstLineIndent = 0;//取消首行缩进的长度
            strContent = "黑体文本n ";//在文本中使用'n'换行
            wordDoc.Paragraphs.Last.Range.Font.Name = "黑体";
            wordDoc.Paragraphs.Last.Range.Text = strContent;
           // wordApp.Selection.Text = strContent;
            //写入加粗文本
            strContent = "加粗文本n ";
            wordApp.Selection.EndKey(ref unite, ref Nothing);
            wordDoc.Paragraphs.Last.Range.Font.Bold = 1;//Bold=0为不加粗
            wordDoc.Paragraphs.Last.Range.Text = strContent;
            //  wordApp.Selection.Text = strContent;
            //写入15号字体文本
            strContent = "15号字体文本n ";
            wordApp.Selection.EndKey(ref unite, ref Nothing);

            wordDoc.Paragraphs.Last.Range.Font.Size = 15;
            wordDoc.Paragraphs.Last.Range.Text = strContent;
            //写入斜体文本
            strContent = "斜体文本n ";
            wordApp.Selection.EndKey(ref unite, ref Nothing);
            wordDoc.Paragraphs.Last.Range.Font.Italic = 1;
            wordDoc.Paragraphs.Last.Range.Text = strContent;
            //写入蓝色文本
            strContent = "蓝色文本n ";
            wordApp.Selection.EndKey(ref unite, ref Nothing);
            wordDoc.Paragraphs.Last.Range.Font.Color = MSWord.WdColor.wdColorBlue;
            wordDoc.Paragraphs.Last.Range.Text = strContent;
            //写入下划线文本
            strContent = "下划线文本n ";
            wordApp.Selection.EndKey(ref unite, ref Nothing);
            wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineThick;
            wordDoc.Paragraphs.Last.Range.Text = strContent;

            object format = MSWord.WdSaveFormat.wdFormatDocument;
            wordDoc.SaveAs(ref path, ref format, 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);
            wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
            wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
            Response.Write("<script> alert('" + path + ": Word文档写入文本完毕!');</script>");
        }

加上文件

效果图:

图片 9

图片 10图片 11

    private void AddWordPic()
        {
            object path;//文件路径
            string strContent;//文件内容
            MSWord.Application wordApp;//Word应用程序变量
            MSWord.Document wordDoc;//Word文档变量
            path = "d:\myWord.doc";//保存为Word2003文档
            // path = "d:\myWord.doc";//保存为Word2007文档
            wordApp = new MSWord.ApplicationClass();//初始化
            if (File.Exists((string)path))
            {
                File.Delete((string)path);
            }
            Object Nothing = Missing.Value;
            wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

            string filename = "d:\kk.jpg";
            //定义要向文档中插入图片的位置
            object range = wordDoc.Paragraphs.Last.Range;
            //定义该图片是否为外部链接
            object linkToFile = false;//默认
            //定义插入的图片是否随word一起保存
            object saveWithDocument = true;
            //向word中写入图片
            wordDoc.InlineShapes.AddPicture(filename, ref Nothing, ref Nothing, ref Nothing);

            object unite = Microsoft.Office.Interop.Word.WdUnits.wdStory;
            wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;//居中显示图片
            wordDoc.InlineShapes[1].Height = 130;
            wordDoc.InlineShapes[1].Width = 200;
            wordDoc.Content.InsertAfter("n");
            wordApp.Selection.EndKey(ref unite, ref Nothing);
            wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
            wordApp.Selection.Font.Size = 10;//字体大小
            wordApp.Selection.TypeText("图1 测试图片n");


            object format = MSWord.WdSaveFormat.wdFormatDocument;
            wordDoc.SaveAs(ref path, ref format, 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);
            wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
            wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
            Response.Write("<script>alert('" + path + ": Word文档创建图片完毕!');</script>");
        }

增加图片

效果图:

图片 12

图片 13图片 14

private void AddWordTable()
        {
            object path;//文件路径
            string strContent;//文件内容
            MSWord.Application wordApp;//Word应用程序变量
            MSWord.Document wordDoc;//Word文档变量
            path = "d:\myWord.doc";//保存为Word2003文档
            // path = "d:\myWord.doc";//保存为Word2007文档
            wordApp = new MSWord.ApplicationClass();//初始化
            if (File.Exists((string)path))
            {
                File.Delete((string)path);
            }
            Object Nothing = Missing.Value;
            wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

            int tableRow = 6;
            int tableColumn = 6;
            //定义一个word中的表格对象
            MSWord.Table table = wordDoc.Tables.Add(wordApp.Selection.Range, tableRow, tableColumn, ref Nothing, ref Nothing);


            wordDoc.Tables[1].Cell(1, 1).Range.Text = "列n行"; 
            for (int i = 1; i < tableRow; i++)
            {
                for (int j = 1; j < tableColumn; j++)
                {
                    if (i == 1)
                    {
                        table.Cell(i, j+1).Range.Text = "Column " + j;
                    }
                    if (j == 1)
                    {
                        table.Cell(i+1, j).Range.Text = "Row " + i;
                    }
                    table.Cell(i+1, j+1).Range.Text =  i + "行 " + j + "列";
                }
            }


            //添加行
            table.Rows.Add(ref Nothing);
            table.Rows[tableRow + 1].Height = 45;
            //向新添加的行的单元格中添加图片
            string FileName = "d:\kk.jpg";//图片所在路径
            object LinkToFile = false;
            object SaveWithDocument = true;
            object Anchor = table.Cell(tableRow+1, tableColumn).Range;//选中要添加图片的单元格
            wordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);

            wordDoc.Application.ActiveDocument.InlineShapes[1].Width = 75;//图片宽度
            wordDoc.Application.ActiveDocument.InlineShapes[1].Height = 45;//图片高度
            // 将图片设置为四周环绕型
            MSWord.Shape s = wordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
            s.WrapFormat.Type = MSWord.WdWrapType.wdWrapSquare;


            //设置table样式
            table.Rows.HeightRule = MSWord.WdRowHeightRule.wdRowHeightAtLeast;
            table.Rows.Height = wordApp.CentimetersToPoints(float.Parse("0.8"));

            table.Range.Font.Size = 10.5F;
            table.Range.Font.Bold = 0;

            table.Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
            table.Range.Cells.VerticalAlignment = MSWord.WdCellVerticalAlignment.wdCellAlignVerticalBottom;
            //设置table边框样式
            table.Borders.OutsideLineStyle = MSWord.WdLineStyle.wdLineStyleDouble;
            table.Borders.InsideLineStyle = MSWord.WdLineStyle.wdLineStyleSingle;

            table.Rows[1].Range.Font.Bold = 1;
            table.Rows[1].Range.Font.Size = 12F;
            table.Cell(1, 1).Range.Font.Size = 10.5F;
            wordApp.Selection.Cells.Height = 40;//所有单元格的高度
            for (int i = 2; i <= tableRow; i++)
            {
                table.Rows[i].Height = 20;
            }
            table.Cell(1, 1).Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
            table.Cell(1, 1).Range.Paragraphs[2].Format.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;

            table.Columns[1].Width = 50;
            for (int i = 2; i <=tableColumn; i++)
            {
                table.Columns[i].Width = 75;
            }


            //添加表头斜线,并设置表头的样式
            table.Cell(1, 1).Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderDiagonalDown].Visible = true;
            table.Cell(1, 1).Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderDiagonalDown].Color = Microsoft.Office.Interop.Word.WdColor.wdColorGray60;
            table.Cell(1, 1).Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderDiagonalDown].LineWidth = Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth050pt;

            //表格边框
            //表格内容行边框
            SetTableBorderStyle(table, Microsoft.Office.Interop.Word.WdBorderType.wdBorderHorizontal, Microsoft.Office.Interop.Word.WdColor.wdColorGray20, Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth025pt);
            //表格内容列边框
            SetTableBorderStyle(table, Microsoft.Office.Interop.Word.WdBorderType.wdBorderVertical, Microsoft.Office.Interop.Word.WdColor.wdColorGray20, Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth025pt);

            SetTableBorderStyle(table, Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft, Microsoft.Office.Interop.Word.WdColor.wdColorGray50, Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth050pt);

            SetTableBorderStyle(table, Microsoft.Office.Interop.Word.WdBorderType.wdBorderRight, Microsoft.Office.Interop.Word.WdColor.wdColorGray50, Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth050pt);

            SetTableBorderStyle(table, Microsoft.Office.Interop.Word.WdBorderType.wdBorderTop, Microsoft.Office.Interop.Word.WdColor.wdColorGray50, Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth050pt);

            SetTableBorderStyle(table, Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom, Microsoft.Office.Interop.Word.WdColor.wdColorGray50, Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth050pt);
            //合并单元格
            table.Cell(4, 4).Merge(table.Cell(4, 5));//横向合并

            table.Cell(2, 3).Merge(table.Cell(4, 3));//纵向合并


            object format = MSWord.WdSaveFormat.wdFormatDocument;
            wordDoc.SaveAs(ref path, ref format, 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);
            wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
            wordApp.Quit(ref Nothing, ref Nothing, ref

本文由葡京网投哪个正规发布于新葡亰-办公,转载请注明出处:Word2003无法打印多篇文档的属性信息

关键词:

上一篇:没有了

下一篇:没有了