我正在搜索如何用我的c#app中的数据填充单词.doc文件,该数据已将数据存储到SQL CE数据库中。
我发现的所有approuches'到现在为止,都是使用源.doc文件来搜索并用值替换变量并将其保存在原始文件中,这看起来很棒,但我需要:
1°使用savefiledialog将源.doc文件复制到用户想要的位置。
2°然后用我想要的数据完成复制,然后保存。
因为.doc文件以后会成为很多配置文件的模型,所以我无法编辑原文,用户必须选择要保存的位置。
或者也许:
1°编辑.doc模型,然后savefiledialog(不改变模型.doc)
编辑:可以解决它,对任何对未来感兴趣的人:
private void CreateWordDocument(object fileName,
object saveAs)
{
//Set Missing Value parameter - used to represent
// a missing value when calling methods through
// interop.
object missing = System.Reflection.Missing.Value;
//Setup the Word.Application class.
Word.Application wordApp =
new Word.Application();
//Setup our Word.Document class we'll use.
Word.Document aDoc = null;
// Check to see that file exists
if (File.Exists((string)fileName))
{
DateTime today = DateTime.Now;
object readOnly = false;
object isVisible = false;
//Set Word to be not visible.
wordApp.Visible = false;
//Open the word document
aDoc = wordApp.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);
// Activate the document
aDoc.Activate();
// Find Place Holders and Replace them with Values.
this.FindAndReplace(wordApp, "$$name$$", "Zach Smith");
}
else
{
MessageBox.Show("Arquivo faltando.");
return;
}
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Documento do Word|*.doc";
saveFileDialog1.Title = "Salvar";
saveFileDialog1.FileName = "Ficha Reg";
if (DialogResult.OK == saveFileDialog1.ShowDialog())
{
string docName = saveFileDialog1.FileName;
if (docName.Length > 0)
{
saveAs = (object)docName;
//Save the document as the correct file name.
aDoc.SaveAs(ref saveAs, 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);
}
}
//Close the document - you have to do this.
aDoc.Close(ref missing, ref missing, ref missing);
MessageBox.Show("File created.");
}
/// <summary>
/// This is simply a helper method to find/replace
/// text.
/// </summary>
/// <param name="WordApp">Word Application to use</param>
/// <param name="findText">Text to find</param>
/// <param name="replaceWithText">Replacement text</param>
private void FindAndReplace(Word.Application WordApp,
object findText,
object replaceWithText)
{
object matchCase = true;
object matchWholeWord = true;
object matchWildCards = false;
object matchSoundsLike = 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 matchSoundsLike,
ref nmatchAllWordForms, ref forward,
ref wrap, ref format, ref replaceWithText,
ref replace, ref matchKashida,
ref matchDiacritics, ref matchAlefHamza,
ref matchControl);
}
private void button1_Click(object sender, EventArgs e)
{
CreateWordDocument(@"C:\Users\Blind\Desktop\FICHA.doc", "");
}
答案 0 :(得分:0)
解决此问题的正确方法是将* .doc文件另存为模板(* .dot),然后使用Documents.Add
方法从模板创建副本。< / p>