c# 与 js 的交互(MSScriptControl)

需下载 Microsoft(R) Script Control 是向开发人员提供简单方法以使应用程序脚本化的 ActiveX(R) 

http://www.microsoft.com/downloads/details.aspx?displaylang=zh-cn&FamilyID=D05FCF37-4D9F-4769-9442-0BCEEF907033

             string strJS=@"

             function RowCount(){ return parseInt((parameterCount / 4)+1); };
             function CellCount(){ return 5; };
             function Table(intRow, intCell, parmJson)
            {     
                  table.Cell(1, 1).Range.Font.Color = Word.GetColor('Blue');
                  table.Cell(1, 1).Range.Font.Underline = Word.underline;
                  table.Cell(1, 1).Range.Font.Bold = 5;
                  table.Cell(1, 1).Range.Font.Italic = 5;
                  table.Cell(1, 1).Range.Text = '序号';  
                  table.Cell(1, 1).Range.ParagraphFormat.Alignment = Word.rCenter;
                  table.Cell(1, 1).Range.Cells.VerticalAlignment = Word.cCenter;

                  table.Cell(1, 2).Range.Text = '资料名称';                      
                  table.Cell(1, 2).Range.ParagraphFormat.Alignment = Word.rCenter;
                  table.Cell(1, 2).Range.Cells.VerticalAlignment = Word.cCenter;

                  table.Cell(1, 3).Range.Text = '是否原件/复印件';                       
                  table.Cell(1, 3).Range.ParagraphFormat.Alignment = Word.rCenter;
                  table.Cell(1, 3).Range.Cells.VerticalAlignment = Word.cCenter;

                  table.Cell(1, 4).Range.Text = '页数';                      
                  table.Cell(1, 4).Range.ParagraphFormat.Alignment = Word.rCenter;
                  table.Cell(1, 4).Range.Cells.VerticalAlignment = Word.cCenter;

                  table.Cell(1, 5).Range.Text = '备注';                       
                  table.Cell(1, 5).Range.ParagraphFormat.Alignment = Word.rCenter;
                  table.Cell(1, 5).Range.Cells.VerticalAlignment = Word.cCenter;  
            };";

            Word.Application wordApp = null;
            Word.Document wordDoc = null;
            object objMissing = System.Reflection.Missing.Value;

             object objTempDoc = @"xxx.doc";
            wordApp = new Word.ApplicationClass();
            wordDoc = wordApp.Documents.Open(

            ref objTempDoc,    //FileName

            ref objMissing,   //ConfirmVersions

            ref objMissing,   //ReadOnly

            ref objMissing,   //AddToRecentFiles

            ref objMissing,   //PasswordDocument

            ref objMissing,   //PasswordTemplate

            ref objMissing,   //Revert

            ref objMissing,   //WritePasswordDocument

            ref objMissing,   //WritePasswordTemplate

            ref objMissing,   //Format

            ref objMissing,   //Enconding

            ref objMissing,   //Visible

            ref objMissing,   //OpenAndRepair

            ref objMissing,  //DocumentDirection

            ref objMissing,  //NoEncodingDialog

            ref objMissing   //XMLTransform

            );

            wordDoc.Activate();

               MSScriptControl.ScriptControlClass sc = new MSScriptControl.ScriptControlClass();
                sc.Language = "JavaScript";
                sc.AddCode(strJS);
                int parameterCount =2 0;
                sc.AddObject("parameterCount", parameterCount, true);
                var parm = new object[0];
                int intRow = (int)sc.Run("RowCount", ref parm);
                int intCell = (int)sc.Run("CellCount", ref parm);
                object bookTableName = "Table";

                Microsoft.Office.Interop.Word.Table table = wordDoc.Tables.Add(wordDoc.Bookmarks.get_Item(ref bookTableName).Range, intRow, intCell, ref objMissing, ref objMissing);
                table.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
                table.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
                WordOperate wordOperate = new WordOperate();        
                sc.AddObject("Word", wordOperate, true);
                sc.AddObject("table", table, true);                               

                var parm1 = new object[] { intRow, intCell };
                sc.Run(strTable, ref parm1);

             
                wordDoc.Save();
                wordDoc.Close(ref objMissing, ref objMissing, ref objMissing); 
                wordApp.Quit(ref objMissing, ref objMissing, ref objMissing); }


    public class WordOperate
    {
        //左右
        public Word.WdParagraphAlignment rLeft = Word.WdParagraphAlignment.wdAlignParagraphLeft;
        public Word.WdParagraphAlignment rCenter = Word.WdParagraphAlignment.wdAlignParagraphCenter;
        public Word.WdParagraphAlignment rRight = Word.WdParagraphAlignment.wdAlignParagraphRight;

        //上下  
        public Word.WdCellVerticalAlignment cBottom = Word.WdCellVerticalAlignment.wdCellAlignVerticalBottom;
        public Word.WdCellVerticalAlignment cCenter = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
        public Word.WdCellVerticalAlignment cTop = Word.WdCellVerticalAlignment.wdCellAlignVerticalTop;

        //下划线
        public Word.WdUnderline underline = Word.WdUnderline.wdUnderlineSingle;

        public Word.WdColor GetColor(string strColor)
        {
            Word.WdColor wdColor = Word.WdColor.wdColorBlack;
            wdColor = Word.WdColor.wdColorBlue;
            switch (strColor)
            {
                case "Black": //黑
                    wdColor = Word.WdColor.wdColorBlack;
                    break;
                case "Blue": //蓝
                    wdColor = Word.WdColor.wdColorBlue;
                    break;
                case "Brown": //棕色
                    wdColor = Word.WdColor.wdColorBrown;
                    break;
                case "Green": //绿
                    wdColor = Word.WdColor.wdColorGreen;
                    break;
                case "Orange": //橙色
                    wdColor = Word.WdColor.wdColorOrange;
                    break;
                case "PaleBlue": //淡蓝色
                    wdColor = Word.WdColor.wdColorPaleBlue;
                    break;
                case "Pink": //粉红色
                    wdColor = Word.WdColor.wdColorPink;
                    break;
                case "Red": //红色
                    wdColor = Word.WdColor.wdColorRed;
                    break;
                case "Tan": //黄褐色
                    wdColor = Word.WdColor.wdColorTan;
                    break;
                case "Violet": //紫色
                    wdColor = Word.WdColor.wdColorViolet;
                    break;
                case "White": //白
                    wdColor = Word.WdColor.wdColorWhite;
                    break;
                case "Yellow": //黄
                    wdColor = Word.WdColor.wdColorYellow;
                    break;
                case "Lime": //灰色
                    wdColor = Word.WdColor.wdColorLime;
                    break;
                case "Indigo": //青色
                    wdColor = Word.WdColor.wdColorIndigo;
                    break;
                case "Gold": //金色
                    wdColor = Word.WdColor.wdColorGold;
                    break;
            }
            return wdColor;
        }
    }


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值