当前位置:编程学习 > C#/ASP.NET >>

请问哪位大哥有asp.net 的word VBA操作实例, 最好是带表格操作的(C#语言),

请问哪位大哥有asp.net 的word VBA操作实例, 最好是带表格操作的(C#), 最近搞项目遇到这个问题, 不知道如何下手啊, 哪位大哥能帮一下忙, 在下感激不尽.. --------------------编程问答-------------------- oo --------------------编程问答--------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Word = Microsoft.Office.Interop.Word; 

namespace ChangeWord
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.killAllProcess();
            this.Change();
        }

        /// <summary>
        /// 打开Word文件读取表格
        /// </summary>
        private void Change()
        {
            object fileName = "D:\\242021_2003631310153陈祥文.doc";
            object readOnly = true;
            object isVisible = true;
            object missing = System.Reflection.Missing.Value;
            //开启应用程序WINWORD.EXE
            //Word.ApplicationClass oWordApp = new Word.ApplicationClass();
            Word._Application oWordApp = new Word.Application();
            Word._Document oDoc = oWordApp.Documents.Open(ref fileName, ref missing, ref readOnly,
            ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);

            oDoc.Activate();

            //循环取出表格
            for (int tablePos = 1; tablePos <= oDoc.Tables.Count; tablePos++)
            {
                Word.Table nowTable = oDoc.Tables[tablePos];
                 string tableMessage = string.Format("第{0}/{1}个表:\n", tablePos, oDoc.Tables.Count);

                 for (int rowPos = 1; rowPos <= nowTable.Rows.Count; rowPos++)
                 {
                     for (int columPos = 1; columPos <= nowTable.Columns.Count; columPos++)
                     {
                         tableMessage += nowTable.Cell(rowPos, columPos).Range.Text;
                         tableMessage = tableMessage.Remove(tableMessage.Length - 2, 2);//remove \r\a
                         tableMessage += "\t";
                     }
                     tableMessage += "\n";
                 }
                 this.lbltableMessage.Text = tableMessage;
            }             
                                    
            //关闭
            //object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
            //oDoc.Close(ref doNotSaveChanges, ref missing, ref missing);

            this.killAllProcess();
            
         }

        /// <summary>
        /// 关闭所有winword.exe进程 
        /// </summary>
        protected void killAllProcess()
        {
            System.Diagnostics.Process[] myPs;
            myPs = System.Diagnostics.Process.GetProcesses();
            foreach (System.Diagnostics.Process p in myPs)
            {
                if (p.Id != 0)
                {
                    string myS = "WINWORD.EXE" + p.ProcessName + " ID:" + p.Id.ToString();
                    try
                    {
                        if (p.Modules != null)
                            if (p.Modules.Count > 0)
                            {
                                System.Diagnostics.ProcessModule pm = p.Modules[0];
                                myS += "\n Modules[0].FileName:" + pm.FileName;
                                myS += "\n Modules[0].ModuleName:" + pm.ModuleName;
                                myS += "\n Modules[0].FileVersionInfo:\n" + pm.FileVersionInfo.ToString();
                                if (pm.ModuleName.ToLower() == "winword.exe")
                                    p.Kill();
                            }
                    }
                    catch
                    { }
                    finally
                    {
                    }
                }
            }
        } 



      }
}

  

主要是这句话,
if(oDoc.Tables.Count > 0)
大于0就是有表格,对话框自己写一个就行了 --------------------编程问答-------------------- 操作Word,为什么还得C#操作VBA呢
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,