1. 程式人生 > >asp.net2.0匯出生成word

asp.net2.0匯出生成word

    }
    protected void Button_Click(object sender, EventArgs e)
    {
        Object Nothing = System.Reflection.Missing.Value;
        //取得Word檔案儲存路徑
        object filename = @SaveAs.Text;
        //建立一個名為WordApp的元件物件
        Word.Application WordApp = new Word.ApplicationClass();
        //建立一個名為WordDoc的文件物件
        Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
        //增加一表格
        Word.Table table = WordDoc.Tables.Add(WordApp.Selection.Range, 3, 3, ref Nothing, ref Nothing);
        //在表格第一單元格中新增自定義的文字內容
                table.Cell(1, 1).Range.Text = wordText.Text;
        //在文件空白地方新增文字內容
        WordDoc.Paragraphs.Last.Range.Text = "Wellcome To Aspxcn.Com";
        //將WordDoc文件物件的內容儲存為DOC文件
        WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
        //關閉WordDoc文件物件
        WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
    
        //關閉WordApp元件物件
        WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
      
        //返回結果
        result.Text = "文件路徑:<a href='" + SaveAs.Text + "'>" + SaveAs.Text + "</a>(點選連結檢視)<br>生成結果:成功!";
    }
}