首 页IT知识库收藏内容
当前位置:翔宇亭IT乐园IT知识库C#高级应用

使用C#操作word

减小字体 增大字体 作者:佚名  来源:转载整理  发布时间:2010-04-29 18:21:00

有时我们希望在C#程序中能够直接操作word,实现向word中写入数据打印或呈交给相关人员,下面就以一个例子讲解了具体的操作方法。

本例演示了打开word文档、在word文档中创建表格,合并单元格,保存word文档并退出等功能。

    using System;
    using System.Web;
    using Microsoft.Office.Interop.Word;

    namespace CustomFileAccess
    {
        public class WordOperator
        {
            public void CreateWord()
            {
                Microsoft.Office.Interop.Word.Application wordApp = new Application();
                Microsoft.Office.Interop.Word.Document wordDocument = new Document();
                Microsoft.Office.Interop.Word.Table wordTable;
                Microsoft.Office.Interop.Word.Table wordTableCopy;

                object myNull = System.Reflection.Missing.Value;

                object strPath = HttpContext.Current.Server.MapPath(@"WordTemplete\MyWordTemplete.doc");
                object styleName = "Table Grid 8";

                try
                {
                    wordDocument = wordApp.Documents.Open(ref strPath, ref myNull, ref myNull, ref myNull,
                        ref myNull, ref myNull, ref myNull, ref myNull, ref myNull, ref myNull, ref myNull, ref myNull,
                        ref myNull, ref myNull, ref myNull, ref myNull);

                    object start = 0;
                    object end = 0;

                    Microsoft.Office.Interop.Word.Range wordRange = wordDocument.Range(ref start, ref end);

                    wordTable = wordDocument.Tables.Add(wordRange, 3, 13, ref myNull, ref myNull);

                    wordTable.Borders.OutsideColor = WdColor.wdColorAutomatic;
                    wordTable.Borders.OutsideColorIndex = WdColorIndex.wdAuto;
                    wordTable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
                    wordTable.Borders.OutsideLineWidth = WdLineWidth.wdLineWidth050pt;

                    wordTable.Borders.InsideColor = WdColor.wdColorAutomatic;
                    wordTable.Borders.InsideColorIndex = WdColorIndex.wdAuto;
                    wordTable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
                    wordTable.Borders.InsideLineWidth = WdLineWidth.wdLineWidth050pt;

                    wordDocument.Tables[1].Cell(1, 1).Merge(wordDocument.Tables[1].Cell(2, 1));

                    wordDocument.Tables[1].Cell(1, 1).Range.Text = "cell 1, 1";
                    wordDocument.Tables[1].Cell(2, 2).Range.Text = "cell 2, 2";

                    wordDocument.Tables[1].Select();
                    wordApp.Selection.Copy();

                    wordDocument.Tables[1].Cell(1, 2).Range.Text = "The First Table";


                    object myunit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
                    wordApp.Selection.EndKey(ref myunit, ref myNull);

                    wordApp.Selection.TypeParagraph();

                    wordApp.Selection.Paste();

                    wordDocument.Tables[2].Cell(1, 1).Range.Text = "The Second Table";

                    wordDocument.Save();
                }
                catch
                {
                    wordDocument.Close(ref myNull, ref myNull, ref myNull);
                    wordApp.Quit(ref myNull, ref myNull, ref myNull);
                    if (wordDocument != null)
                    {
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDocument);
                        wordDocument = null;
                    }
                    if (wordApp != null)
                    {
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
                        wordApp = null;
                    }
                    GC.Collect();
                    throw new Exception("文档生成失败!");

                }
                finally
                {
                    wordDocument.Close(ref myNull, ref myNull, ref myNull);
                    wordApp.Quit(ref myNull, ref myNull, ref myNull);
                }

            }
        }
    }


本文源自:翔宇亭——IT乐园(http://www.biye5u.com),转载请保留此信息!

微信搜索“优雅的代码”关注本站的公众号,或直接使用微信扫描下面二维码关注本站公众号,以获取最新内容。

个人成长离不开各位的关注,你的关注就是我继续前行的动力。

Tags:C# word 源代码

知识评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 1 条,得分 100 分,平均 100 分) 查看完整评论
[回复] 1不错的东西   打分:100分  发表时间:2012-04-26
· 很好,配图说明就好了,不然不知道到底是是什么效果
愿您的精彩评论引起共鸣,带来思考和价值。
用户名: 查看更多评论
分 值:100分 90分 80分 70分 60分 40分 20分
内 容:
验证码:
关于本站 | 网站帮助 | 广告合作 | 网站声明 | 友情连接 | 网站地图
本站部分内容来自互联网,如有侵权,请来信告之,谢谢!
Copyright © 2007-2024 biye5u.com. All Rights Reserved.