C#和Word-替换为格式
|
我正在尝试以最简单的形式在预先存在的Word文档中填写一些信息。我的代码做得很好...
它不执行的工作是修复所需的格式,替换后的文本必须变为黑色和粗体,表格上的totals字段需要正确对齐。
如果有人可以指出如何在我的代码中执行此操作,将不胜感激。
这是我当前的代码:
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);
}
}
}
}
没有找到相关结果
已邀请:
2 个回复
外镶受继
体悉