问题
I have created a desktop application which in that I need to save texts of textbox into into a word document, the document should be created by user and the position of the document would be chosed by user and then the value of textbox should be saved into that.
for example:
"this is textBox value"
then there is a save button, by clicking that the save window should be opened then user will give a name to that and set its position then click OK finally the text of textbox will be saved into that word document.
any idea how to do it? I have searched allot in google but I couldn't find anything to find the way ...
回答1:
Word exposes a COM
API, search for Word Interop or Office Interop.
With this API you may start word, load a document, add to this document, for example at the current cursor position and save the document to a new file.
回答2:
I have found a solution for my own question, I thought to share it if anyone else has faced this kind of problems, here is the code:
private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Word document|*.doc";
saveFileDialog1.Title = "Save the Word Document";
saveFileDialog1.FileName = "MS word document.docx";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(saveFileDialog1.FileName,txtResult.Text.ToString());
}
}
this works fine, but I want to set font-family and font-size, if anyone knows please share your solution?
来源:https://stackoverflow.com/questions/28761877/how-to-insert-text-from-c-sharp-application-into-ms-word-document-and-save-it-as