【发布时间】:2016-06-12 16:55:34
【问题描述】:
我正在寻找如何使用我的 c# 应用程序中已将数据存储到 SQL CE db 中的数据填充 word .doc 文件。
到目前为止,我发现的所有方法都是使用源 .doc 文件来搜索并用值替换变量并将其保存在原始文件中,这看起来很棒,但我需要:
1° 使用 savefiledialog 将源 .doc 文件复制到用户想要的位置。
2°那么副本就完成了我想要的数据然后保存了。
因为.doc文件会是后面很多profile的模型,所以无法编辑原件,需要用户自己选择保存位置。
或者也许:
1° 编辑 .doc 模型,然后保存文件对话框(不更改模型 .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", "");
}
【问题讨论】:
标签: c# ms-word output interop savefiledialog