C#&放大器;字 - 用格式化更换 [英] C# & Word - Replace with Formating

查看:167
本文介绍了C#&放大器;字 - 用格式化更换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在它是什么,我试图做简单的形式是填写上已存在的word文档的一些信息。其中我的代码确实相当不错...



什么是没有做到的是修复了格式化喜欢它需要,被替换的文本需要进行黑色粗体在桌子上总计字段需要右对齐。



如果任何人都可以指向我怎么可能会做,在我的代码,将不胜感激。



下面是我当前的代码:

 使用系统; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;使用System.Windows.Forms的
;使用Word =的Microsoft.Office.Interop.Word
;

命名空间WordDocTesting
{
公共部分Form1类:表格
{
公共Form1中()
{
的InitializeComponent() ;
}

私人无效FindAndReplace(Word.Application WordApp,
对象FINDTEXT,
对象replaceWithText)
{
对象缺少=类型。失踪;

对象matchCase = TRUE;
对象matchWholeWord = TRUE;
对象matchWildCards = FALSE;
对象matchSoundLike = FALSE;
对象nmatchAllWordForms = FALSE;
对象向前= TRUE;
对象格式= FALSE;
对象matchKashida = FALSE;
对象matchDiacritics = FALSE;
对象matchAlefHamza = FALSE;
对象matchControl = FALSE;
对象READ_ONLY = FALSE;
对象可见=真实的;
对象替换= 2;
对象包= 1;

如果(replaceWithText.ToString()长度< 250)//正常执行
{
WordApp.Selection.Find.Execute(REF FINDTEXT,
REF matchCase,
裁判matchWholeWord,
裁判matchWildCards,
裁判matchSoundLike,
裁判nmatchAllWordForms,
裁判前进,
裁判包裹,
参考格式,
裁判replaceWithText,
裁判替换,
裁判matchKashida,
裁判matchDiacritics,
裁判matchAlefHamza,
参考matchControl);
}
,否则
{
WordApp.Application.Selection.Find.Execute(REF FINDTEXT,
裁判失踪,
裁判失踪,
REF缺失,
裁判失踪,
裁判失踪,
裁判失踪,
裁判失踪,
裁判失踪,
裁判失踪,
REF失踪,
裁判失踪,
裁判失踪,
裁判失踪,
REF丢失);

WordApp.Application.Selection.Text。

WordApp.Application.Selection.Text =(字符串)replaceWithText;
}

}

私人无效的button1_Click(对象发件人,EventArgs五)
{
Word.Application字=新Word.Application ();
Word.Document文档=新Word.Document();

//定义对象传递给这个词API缺少的参数
对象缺少= Type.Missing;


{
//一切这个词接受必须是一个对象
目标文件名= @R:\Project\Proposals\TestMe.docx

对象readOnly的= TRUE;
=文件word.Documents.Open(REF文件名,REF失踪,楼盘只读,已
参考失踪,失踪参考,参考失踪,
参考失踪,失踪参考,参考失踪,
裁判失踪,失踪参考,参考失踪,
参考失踪,失踪参考,参考失踪,
REF丢失);

document.Activate();

字符串propsalTitle =Lorem存有悲坐阿梅德
this.FindAndReplace(文字,输入标题的建议在这里,propsalTitle);

串defaultTechDesc =输入相关的技术资料我们的项目在这里使用图片时,可以利用和应用。
字符串techDesc =Lorem存有悲坐阿梅德,consectetur adipiscing ELIT。整型tempor rutrum自由人SED dapibus。Praesent vehicula油树ultricies。保护者vulputate enim简历暂准孕妇euismod。Morbi aliquam拉克丝enim。Donec suscipit英里的SEM油树ID孕妇SAPIEN tempor。Donec在SEM在华富dignissim placerat等人乌仁娜。保护者NEC得力士德维尔存有volutpat aliquet NEC UT tortor。法无facilisi。Donec德维尔华富lectus,AC马蒂斯SAPIEN。Nullam一个胡斯托nisl,坐阿梅德congue velit。UT斯达康imperdiet ,velit ID luctus vulputate,augue自由人consectetur爱神,QUIS孕妇华富奥奇交流拉克丝。Curabitur油树,MI坐阿梅特interdum feugiat,risus拉克丝dignissim metus,UT luctus法无奥奇一个赌注。Nunc公司等turpis德维尔存有faucibus rhoncus欧盟EU velit。桑达interdum,麦格纳坐阿梅特PORTA euismod,奥奇猫tincidunt mauris等iaculis ligula arcu molestie胡斯托DUIS sollicitudin普鲁斯UT普鲁斯hendrerit adipiscing Donec aliquam ultricies爱神+\\\
\\\
+ defaultTechDesc。;
this.FindAndReplace(文字,defaultTechDesc,techDesc);

字符串交付=Lorem存有悲坐阿梅德,consectetur adipiscing ELIT。
this.FindAndReplace(字,输入将DELIVERD于性能为这个项目实施期间所有项,交付);

弦接触=珍妮
this.FindAndReplace(词,输入联系人的位置点,admContact);

字符串PocTitle =文字;
this.FindAndReplace(文字,ENTER POC TITLE HERE,admPocTitle);

字符串PocTeam =悲;
this.FindAndReplace(文字,ENTER POC TEAM NAME HERE,admPocTeam);

字符串PocPhone =867-5309;
this.FindAndReplace(文字,ENTER POC电话号码HERE,admPocPhone);

字符串PocEmail =Jenny.G.Time@Sit.amet;
this.FindAndReplace(文字,ENTER POC电子邮件Here,admPocEmail);

//表
Word.Table表= document.Tables [1];

双TOTALCOST = 0;
Word.Row排;
的for(int i = 1; I< = 10;我++)
{
行= table.Rows.Add(REF丢失);
row.Cells [1] .Range.Text =新任务+我;
row.Cells [2] .Range.Text =的String.Format({0:C}我);
= TOTALCOST + TOTALCOST我;
}

行= table.Rows.Add(REF丢失);
row.Cells [1] .Range.Text =总计;
row.Cells [2] .Range.Text =的String.Format({0:C},TOTALCOST);

对象newFileName = @R:\Project\Proposals\TestMe2.docx

document.SaveAs(REF newFileName,
参考失踪,失踪参考,参考失踪,失踪参考,
参考失踪,失踪参考,参考失踪,失踪参考,
裁判失踪,失踪参考,参考失踪,失踪参考,
参考失踪,失踪参考,参考失踪);
}

赶上(异常前)
{
//这里留言
}
终于
{
document.Close(参考失踪,失踪参考,参考失踪);
word.Application.Quit(参考失踪,失踪参考,参考失踪);
}
}
}
}


解决方案

明白了。



文字加粗,斜体删除,改变颜色,文本对齐......都包含在下面的代码。



再次感谢所有帮助。 :)

 使用系统; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;使用System.Windows.Forms的
;使用Word =的Microsoft.Office.Interop.Word
;使用Color = Microsoft.Office.Interop.Word.WdColor
;

命名空间WordDocTesting
{
公共部分Form1类:表格
{
公共Form1中()
{
的InitializeComponent() ;
}

私人无效FindAndReplace(Word.Application WordApp,
对象FINDTEXT,
对象replaceWithText,
布尔boldIt)
{
对象缺少= Type.Missing;

对象matchCase = TRUE;
对象matchWholeWord = TRUE;
对象matchWildCards = FALSE;
对象matchSoundLike = FALSE;
对象nmatchAllWordForms = FALSE;
对象向前= TRUE;
对象格式= FALSE;
对象matchKashida = FALSE;
对象matchDiacritics = FALSE;
对象matchAlefHamza = FALSE;
对象matchControl = FALSE;
对象READ_ONLY = FALSE;
对象可见=真实的;
对象替换= 2;
对象包= 1;


WordApp.Selection.Find.Execute(REF FINDTEXT,
裁判matchCase,
裁判matchWholeWord,
裁判matchWildCards,
REF matchSoundLike ,
裁判nmatchAllWordForms,
裁判前进,
裁判包裹,
裁判格式,
/ * REF replaceWithText,* / REF丢失,
/ * REF更换* / REF丢失,
裁判matchKashida,
裁判matchDiacritics,
裁判matchAlefHamza,
参考matchControl);


如果(boldIt)
{
WordApp.Application.Selection.Font.Bold = 1;
}

WordApp.Application.Selection.Font.Italic = 0;
WordApp.Application.Selection.Font.Color = Color.wdColorBlack;

WordApp.Application.Selection.Text =(字符串)replaceWithText;
}

私人无效的button1_Click(对象发件人,EventArgs五)
{
Word.Application字=新Word.Application();
Word.Document文档=新Word.Document();

//定义对象传递给这个词API缺少的参数
对象缺少= Type.Missing;


{
//一切这个词接受必须是一个对象
目标文件名= @R:\Project\Proposals\TestMe.docx

对象readOnly的= TRUE;
=文件word.Documents.Open(REF文件名,REF失踪,楼盘只读,已
参考失踪,失踪参考,参考失踪,
参考失踪,失踪参考,参考失踪,
裁判失踪,失踪参考,参考失踪,
参考失踪,失踪参考,参考失踪,
REF丢失);

document.Activate();

字符串propsalTitle =Lorem存有悲坐阿梅德
this.FindAndReplace(文字,输入标题的建议在这里,propsalTitle,真正的);

串defaultTechDesc =输入相关的技术资料我们的项目在这里使用图片时,可以利用和应用。
字符串techDesc =Lorem存有悲坐阿梅德,consectetur adipiscing ELIT。整型tempor rutrum自由人SED dapibus。Praesent vehicula油树ultricies。保护者vulputate enim简历暂准孕妇euismod。Morbi aliquam拉克丝enim。Donec suscipit英里的SEM油树ID孕妇SAPIEN tempor。Donec在SEM在华富dignissim placerat等人乌仁娜。保护者NEC得力士德维尔存有volutpat aliquet NEC UT tortor。法无facilisi。Donec德维尔华富lectus,AC马蒂斯SAPIEN。Nullam一个胡斯托nisl,坐阿梅德congue velit。UT斯达康imperdiet ,velit ID luctus vulputate,augue自由人consectetur爱神,QUIS孕妇华富奥奇交流拉克丝。Curabitur油树,MI坐阿梅特interdum feugiat,risus拉克丝dignissim metus,UT luctus法无奥奇一个赌注。Nunc公司等turpis德维尔存有faucibus rhoncus欧盟EU velit。桑达interdum,麦格纳坐阿梅特PORTA euismod,奥奇猫tincidunt mauris等iaculis ligula arcu molestie胡斯托DUIS sollicitudin普鲁斯UT普鲁斯hendrerit adipiscing Donec aliquam ultricies爱神。;
this.FindAndReplace(文字,defaultTechDesc,techDesc,FALSE);

字符串交付=Lorem存有悲坐阿梅德,consectetur adipiscing ELIT。
this.FindAndReplace(词,输入将DELIVERD期末较性能的该项目所有项目。,交付,FALSE);

弦接触=珍妮
this.FindAndReplace(词,输入联系人的位置点,联系,FALSE);

字符串PocTitle =文字;
this.FindAndReplace(文字,ENTER POC TITLE HERE,PocTitle,FALSE);

字符串PocTeam =悲;
this.FindAndReplace(文字,ENTER POC TEAM NAME HERE,PocTeam,FALSE);

字符串PocPhone =867-5309;
this.FindAndReplace(文字,ENTER POC电话号码HERE,PocPhone,FALSE);

字符串PocEmail =Jenny.G.Time@Sit.amet;
this.FindAndReplace(文字,ENTER POC电子邮件Here,PocEmail,FALSE);

//表
Word.Table表= document.Tables [1];

INT X = 0;
双TOTALCOST = 0;
Word.Row排;
的for(int i = 1; I< = 10;我++)
{
X = 1 + 1;
行= table.Rows.Add(REF丢失);
row.Cells [1] .Range.Text =新任务+我;
row.Cells [2] .Range.Text =的String.Format({0:C}我);
table.Cell(X,1)= .Range.ParagraphFormat.Alignment Word.WdParagraphAlignment.wdAlignParagraphLeft;
table.Cell(X,1).Range.Font.Bold = 0;
table.Cell(X,2)= .Range.ParagraphFormat.Alignment Word.WdParagraphAlignment.wdAlignParagraphRight;
table.Cell(X,2).Range.Font.Bold = 0;
= TOTALCOST + TOTALCOST我;
}

X = X + 1;
行= table.Rows.Add(REF丢失);
row.Cells [1] .Range.Text =总计;
row.Cells [2] .Range.Text =的String.Format({0:C},TOTALCOST);
table.Cell(X,1)= .Range.ParagraphFormat.Alignment Word.WdParagraphAlignment.wdAlignParagraphRight;
table.Cell(X,1).Range.Font.Bold = 1;
table.Cell(X,2)= .Range.ParagraphFormat.Alignment Word.WdParagraphAlignment.wdAlignParagraphRight;
table.Cell(X,2).Range.Font.Bold = 1;

对象newFileName = @R:\Project\Proposals\TestMe2.docx

document.SaveAs(REF newFileName,
参考失踪,失踪参考,参考失踪,失踪参考,
参考失踪,失踪参考,参考失踪,失踪参考,
裁判失踪,失踪参考,参考失踪,失踪参考,
参考失踪,失踪参考,参考失踪);
}

赶上(异常前)
{
//这里留言
}
终于
{
document.Close(参考丢失,
裁判失踪,
参考遗漏);

word.Application.Quit(参考丢失,
裁判失踪,
REF丢失);
}
}
}
}


In it's simplest form what I am attempting to do is fill out some information on a preexisting word document. Which my code does quite nicely...

What it doesn't do is fix up the formatting like it needs to, the replaced text needs be made black and bold the totals field on the table needs to be right justified.

If anyone can point to how I might do that in my code it would be much appreciated.

Here is my current code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;

namespace WordDocTesting
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void FindAndReplace(Word.Application WordApp,
                                    object findText,
                                    object replaceWithText)
        {
            object missing = Type.Missing;

            object matchCase = true;
            object matchWholeWord = true;
            object matchWildCards = false;
            object matchSoundLike = false;
            object nmatchAllWordForms = false;
            object forward = true;
            object format = false;
            object matchKashida = false;
            object matchDiacritics = false;
            object matchAlefHamza = false;
            object matchControl = false;
            object read_only = false;
            object visible = true;
            object replace = 2;
            object wrap = 1;

            if (replaceWithText.ToString().Length < 250) // Normal execution
            {
                WordApp.Selection.Find.Execute(ref findText,
                                               ref matchCase,
                                               ref matchWholeWord,
                                               ref matchWildCards,
                                               ref matchSoundLike,
                                               ref nmatchAllWordForms,
                                               ref forward,
                                               ref wrap,
                                               ref format,
                                               ref replaceWithText,
                                               ref replace,
                                               ref matchKashida,
                                               ref matchDiacritics,
                                               ref matchAlefHamza,
                                               ref matchControl);
            }
            else
            {
                WordApp.Application.Selection.Find.Execute(ref findText, 
                                                           ref missing, 
                                                           ref missing, 
                                                           ref missing, 
                                                           ref missing,
                                                           ref missing, 
                                                           ref missing, 
                                                           ref missing, 
                                                           ref missing, 
                                                           ref missing,
                                                           ref missing, 
                                                           ref missing,
                                                           ref missing, 
                                                           ref missing, 
                                                           ref missing);

                WordApp.Application.Selection.Text.

                WordApp.Application.Selection.Text = (string)replaceWithText; 
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Word.Application word = new Word.Application();
            Word.Document document = new Word.Document();

            // Define an object to pass to the word API for missing parameters
            object missing = Type.Missing;

            try
            {
                // Everything that word accepts must be an object
                object fileName = @"R:\Project\Proposals\TestMe.docx";

                object readOnly = true;
                document = word.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 missing, 
                                               ref missing, ref missing, ref missing,
                                               ref missing);

                document.Activate();

                String propsalTitle = "Lorem ipsum dolor sit amet";
                this.FindAndReplace(word, "Enter Proposal Title Here", propsalTitle);

                string defaultTechDesc = "ENTER RELEVANT TECHNICAL INFORMATION ABOUT PROJECT HERE.  USE PICTURES WHEN AVAILABLE AND APPLICABLE.";
                String techDesc = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tempor rutrum libero sed dapibus. Praesent vehicula mollis ultricies. Maecenas vulputate enim vitae nisi gravida euismod. Morbi aliquam lacus enim. Donec suscipit mi at sem mollis id gravida sapien tempor. Donec in sem at quam dignissim placerat et a urna. Maecenas nec tellus vel ipsum volutpat aliquet nec ut tortor. Nulla facilisi. Donec vel quam lectus, ac mattis sapien. Nullam a justo nisl, sit amet congue velit. Ut imperdiet, velit id luctus vulputate, augue libero consectetur eros, quis gravida quam orci ac lacus. Curabitur mollis, mi sit amet interdum feugiat, risus lacus dignissim metus, ut luctus nulla orci a ante. Nunc et turpis vel ipsum faucibus rhoncus eu eu velit. Sed interdum, magna sit amet porta euismod, orci felis tincidunt mauris, et iaculis ligula arcu molestie justo. Duis sollicitudin purus ut purus hendrerit adipiscing. Donec aliquam ultricies eros. " + "\n\n" + defaultTechDesc;
                this.FindAndReplace(word, defaultTechDesc, techDesc);

                String deliverables = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
                this.FindAndReplace(word, "ENTER ALL ITEMS THAT WILL BE DELIVERD OVER PERIOD OF PERFORMANCE FOR THIS PROJECT.", deliverables);

                String Contact = "Jenny";
                this.FindAndReplace(word, "ENTER POINT OF CONTACT HERE", admContact);

                String PocTitle = "ipsum";
                this.FindAndReplace(word, "ENTER POC TITLE HERE", admPocTitle);

                String PocTeam = "dolor";
                this.FindAndReplace(word, "ENTER POC TEAM NAME HERE", admPocTeam);

                String PocPhone = "867-5309";
                this.FindAndReplace(word, "ENTER POC PHONE NUMBER HERE", admPocPhone);

                String PocEmail = "Jenny.G.Time@Sit.amet";
                this.FindAndReplace(word, "ENTER POC EMAIL HERE", admPocEmail);

                // Tables
                Word.Table table = document.Tables[1];

                double totalCost = 0;
                Word.Row row;
                for (int i = 1; i <= 10; i++)
                {
                    row = table.Rows.Add(ref missing);
                    row.Cells[1].Range.Text = "Task New " + i;
                    row.Cells[2].Range.Text = String.Format("{0:C}", i);
                    totalCost = totalCost + i;
                }

                row = table.Rows.Add(ref missing);
                row.Cells[1].Range.Text = "Total";
                row.Cells[2].Range.Text = String.Format("{0:C}", totalCost);

                object newFileName = @"R:\Project\Proposals\TestMe2.docx";

                document.SaveAs(ref newFileName,
                                ref missing, ref missing, ref missing, ref missing,
                                ref missing, ref missing, ref missing, ref missing,
                                ref missing, ref missing, ref missing, ref missing,
                                ref missing, ref missing, ref missing);
            }

            catch (Exception ex)
            {
                // Message here
            }
            finally
            {
                document.Close(ref missing, ref missing, ref missing);
                word.Application.Quit(ref missing, ref missing, ref missing);
            }
        }
    }
}

解决方案

Got it.

Bolding Text, Removing Italics, Changing Colors, Text Alignment... all contained in the code below.

Thanks again to all that helped. :)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
using Color = Microsoft.Office.Interop.Word.WdColor;

namespace WordDocTesting
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void FindAndReplace(Word.Application WordApp,
                                    object findText,
                                    object replaceWithText,
                                    bool boldIt)
        {
            object missing = Type.Missing;

            object matchCase = true;
            object matchWholeWord = true;
            object matchWildCards = false;
            object matchSoundLike = false;
            object nmatchAllWordForms = false;
            object forward = true;
            object format = false;
            object matchKashida = false;
            object matchDiacritics = false;
            object matchAlefHamza = false;
            object matchControl = false;
            object read_only = false;
            object visible = true;
            object replace = 2;
            object wrap = 1;


            WordApp.Selection.Find.Execute(ref findText,
                                           ref matchCase,
                                           ref matchWholeWord,
                                           ref matchWildCards,
                                           ref matchSoundLike,
                                           ref nmatchAllWordForms,
                                           ref forward,
                                           ref wrap,
                                           ref format,
                                           /*ref replaceWithText,*/ ref missing,
                                           /*ref replace,*/         ref missing,
                                           ref matchKashida,
                                           ref matchDiacritics,
                                           ref matchAlefHamza,
                                           ref matchControl);


            if (boldIt)
            {
                WordApp.Application.Selection.Font.Bold = 1;
            }

            WordApp.Application.Selection.Font.Italic = 0;
            WordApp.Application.Selection.Font.Color = Color.wdColorBlack;

            WordApp.Application.Selection.Text = (string)replaceWithText; 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Word.Application word = new Word.Application();
            Word.Document document = new Word.Document();

            // Define an object to pass to the word API for missing parameters
            object missing = Type.Missing;

            try
            {
                // Everything that word accepts must be an object
                object fileName = @"R:\Project\Proposals\TestMe.docx";

                object readOnly = true;
                document = word.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 missing, 
                                               ref missing, ref missing, ref missing,
                                               ref missing);

                document.Activate();

                String propsalTitle = "Lorem ipsum dolor sit amet";
                this.FindAndReplace(word, "Enter Proposal Title Here", propsalTitle, true);

                string defaultTechDesc = "ENTER RELEVANT TECHNICAL INFORMATION ABOUT PROJECT HERE.  USE PICTURES WHEN AVAILABLE AND APPLICABLE.";
                String techDesc = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tempor rutrum libero sed dapibus. Praesent vehicula mollis ultricies. Maecenas vulputate enim vitae nisi gravida euismod. Morbi aliquam lacus enim. Donec suscipit mi at sem mollis id gravida sapien tempor. Donec in sem at quam dignissim placerat et a urna. Maecenas nec tellus vel ipsum volutpat aliquet nec ut tortor. Nulla facilisi. Donec vel quam lectus, ac mattis sapien. Nullam a justo nisl, sit amet congue velit. Ut imperdiet, velit id luctus vulputate, augue libero consectetur eros, quis gravida quam orci ac lacus. Curabitur mollis, mi sit amet interdum feugiat, risus lacus dignissim metus, ut luctus nulla orci a ante. Nunc et turpis vel ipsum faucibus rhoncus eu eu velit. Sed interdum, magna sit amet porta euismod, orci felis tincidunt mauris, et iaculis ligula arcu molestie justo. Duis sollicitudin purus ut purus hendrerit adipiscing. Donec aliquam ultricies eros. ";
                this.FindAndReplace(word, defaultTechDesc, techDesc, false);

                String deliverables = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
                this.FindAndReplace(word, "ENTER ALL ITEMS THAT WILL BE DELIVERD OVER PERIOD OF PERFORMANCE FOR THIS PROJECT.", deliverables, false);

                String Contact = "Jenny";
                this.FindAndReplace(word, "ENTER POINT OF CONTACT HERE", Contact, false);

                String PocTitle = "ipsum";
                this.FindAndReplace(word, "ENTER POC TITLE HERE", PocTitle, false);

                String PocTeam = "dolor";
                this.FindAndReplace(word, "ENTER POC TEAM NAME HERE", PocTeam, false);

                String PocPhone = "867-5309";
                this.FindAndReplace(word, "ENTER POC PHONE NUMBER HERE", PocPhone, false);

                String PocEmail = "Jenny.G.Time@Sit.amet";
                this.FindAndReplace(word, "ENTER POC EMAIL HERE", PocEmail, false);

                // Tables
                Word.Table table = document.Tables[1];

                int x = 0;
                double totalCost = 0;
                Word.Row row;
                for (int i = 1; i <= 10; i++)
                {
                    x = i + 1;
                    row = table.Rows.Add(ref missing);
                    row.Cells[1].Range.Text = "Task New " + i;
                    row.Cells[2].Range.Text = String.Format("{0:C}", i);
                    table.Cell(x, 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
                    table.Cell(x, 1).Range.Font.Bold = 0;
                    table.Cell(x, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
                    table.Cell(x, 2).Range.Font.Bold = 0;
                    totalCost = totalCost + i;
                }

                x = x + 1;
                row = table.Rows.Add(ref missing);
                row.Cells[1].Range.Text = "Total";
                row.Cells[2].Range.Text = String.Format("{0:C}", totalCost);
                table.Cell(x, 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
                table.Cell(x, 1).Range.Font.Bold = 1;
                table.Cell(x, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
                table.Cell(x, 2).Range.Font.Bold = 1;

                object newFileName = @"R:\Project\Proposals\TestMe2.docx";

                document.SaveAs(ref newFileName,
                                ref missing, ref missing, ref missing, ref missing,
                                ref missing, ref missing, ref missing, ref missing,
                                ref missing, ref missing, ref missing, ref missing,
                                ref missing, ref missing, ref missing);
            }

            catch (Exception ex)
            {
                // Message here
            }
            finally
            {
                document.Close(ref missing, 
                               ref missing, 
                               ref missing);

                word.Application.Quit(ref missing, 
                                      ref missing, 
                                      ref missing);
            }
        }
    }
}

这篇关于C#&放大器;字 - 用格式化更换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆