打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
c#实现word创建表格合并单元格操作功能

c#实现word创建表格合并单元格操作功能

减小字体
增大字体
作者:Hd资源网  来源:本站整理  发布时间:2010-04-19 10:43:55

word操作,包括打开word文档,创建表格,合并单元格,保存退出

    WordOperator

    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);
                }

            }
        }
    }

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Delphi控制Word编程手记
c# 操作Word总结
c#如何把datagridview导出到exce
利用C Builder 中OLE自动化功能实现调用Word进行报表制作
CSDN 论坛
部分Office 2007文件格式转换为xps和pdf代码整理
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服