生成word文档

时间:2013-02-11 06:44:49

标签: c# ms-word

我使用以下代码

创建一个word文档
private void button1_Click(object sender, EventArgs e)
    {
        object missing = System.Reflection.Missing.Value;
        object Visible=true;
        object start1 = 0;
        object end1 = 0;

         Microsoft.Office.Interop.Word.Application WordApp;
        WordApp = new Microsoft.Office.Interop.Word.Application();
        Document adoc = WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
        Range rng = adoc.Range(ref start1, ref missing);

        try
        {              
            rng.Font.Name = "Hai";
            rng.InsertAfter("Hello World!");
            object filename = @"D:\MyWord.doc";
            adoc.SaveAs(ref filename, 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);
            WordApp.Visible = true;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }        
    }

仅当系统具有MS Word时才有效。如果系统不包含ms字,那么我该如何创建word文档呢?

1 个答案:

答案 0 :(得分:2)

使用Open XML Format SDK 1234567890