2013-07-13 30 views
0

我有兩個文本框和一個按鈕在我的aspx頁面。當我單擊該按鈕時,Word文檔應該打開並且文本框值應該存儲在Word文檔中。如何在Visual Studio 2010中保存MS Word中的文本框值?

+0

沒有足夠的信息。請提供更多的背景 - 什麼文件?其中 - 在服務器上,在客戶機上還是在客戶機上?哪些瀏覽器需要這樣做?你想達到什麼目的? – Addys

回答

1

聽起來好像你必須下載Open Xml SDK。 比你能夠從你的asp頁面創建一個word文檔。

一些教程: http://msdn.microsoft.com/en-us/library/office/dd440953%28v=office.12%29.aspx

是這樣的:

using (WordprocessingDocument package = WordprocessingDocument.Create(docName, WordprocessingDocumentType.Document)) 
    { 
    // Add a new main document part. 
    package.AddMainDocumentPart(); 

    // Create the Document DOM. 
    package.MainDocumentPart.Document = 
     new Document( 
     new Body( 
      new Paragraph( 
      new Run( 
       new Text("Hello World!"))))); 

    // Save changes to the main document part. 
    package.MainDocumentPart.Document.Save(); 
    } 

,那麼你必須寫word文檔響應流。 這樣的事情:

FileInfo file = new FileInfo(PathToExcelFile); 
if (file.Exists) 
{ 
    Response.Clear(); 
    Response.ClearHeaders(); 
    Response.ClearContent(); 
    Response.AddHeader("content-disposition", "attachment; filename=" + fileName); 
    Response.AddHeader("Content-Type", "application/Excel"); 
    Response.ContentType = "application/vnd.xls"; 
    Response.AddHeader("Content-Length", file.Length.ToString()); 
    Response.WriteFile(file.FullName); 
    Response.End(); 
} 
else 
{ 
    Response.Write("This file does not exist."); 
} 
+0

+1,但也許你應該將類型從'Excel'改爲'Word' –

+0

抱歉,我剛剛從某處複製了片段。但應該清楚這裏發生了什麼。 –

相關問題