2012-02-21 161 views
0

我已經使用了一個關於如何從後面的代碼創建一個word文檔的例子。我在一個簡單的Web部件中實現了這個代碼,以便先測試它。我有一個按鈕,當按下時會創建Word文檔,但當按鈕被點擊時目前沒有任何反應。我不知道我是缺少在這裏,所以我希望有人能提供洞察力,下面是我目前擁有的代碼:(我將只列出importat部分在c代碼後面創建一個Word Dirment代碼#


using Microsoft.Office.Core; 
using Microsoft.Office.Server; //for use when creatigna writing data to a MS 
           //Word Document 
using Microsoft.Office.Interop.Word; //for use when creatigna writing data to a MS 
            //Word Documenr 
using Word = Microsoft.Office.Interop.Word; //for use when creatign and writing data 
              //to a MS Word Document 
using System.Reflection; 

namespace Kemp.SP2010.Badges.Badges 
{ 
public partial class BadgesUserControl : UserControl 
{ 




protected void Page_Load(object sender, EventArgs e) 
{ 

GenerateBadges.Click += new EventHandler(GenerateBadges_Click); 

} 



//button to create the badges 
void GenerateBadges_Click(object sender, System.EventArgs e) 
{ 


#region Create Table in MS Word Document 


object oMissing = System.Reflection.Missing.Value; 
object oEndOfDoc = "\\endofdoc"; 

//Start word and create a new document 
Word._Application oWord; 
Word._Document oDoc; 
oWord = new Word.Application(); 
oWord.Visible = true; 

oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); 


//Creating a paragraph at the beginning of the document 
Word.Paragraph oPara1; 
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing); 
oPara1.Range.Text = "Heading 1"; 
oPara1.Range.Font.Bold = 1; 
oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph 
oPara1.Range.InsertParagraphAfter(); 

//Creating an 2x8 table 
Word.Table oTable; 
Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; 
oTable = oDoc.Tables.Add(wrdRng, 2, 8, ref oMissing, ref oMissing); 
oTable.Range.ParagraphFormat.SpaceAfter = 6; 

//for loop 
int r,c; 
string strText; 
for (r = 1; r <= 2; r++) 
for (c = 1; c <= 8; c++) 
{ 

strText = "r" + r +"c" + c; 
oTable.Cell(r, c).Range.Text = strText; 

} 

oTable.Rows[1].Range.Font.Bold = 1; 
oTable.Rows[1].Range.Font.Italic = 1; 

#endregion 
} 
} 
} 

這裏是與按鈕

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %> 
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, 
PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" 
Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, 
PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" 
Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, 
PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" 
Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, 
PublicKeyToken=31bf3856ad364e35" %> 
<%@ Import Namespace="Microsoft.SharePoint" %> 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" 
Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, 
PublicKeyToken=71e9bce111e9429c" %> 
<%@ Control Language="C#" AutoEventWireup="true" 
CodeBehind="BadgesUserControl.ascx.cs" 
Inherits="Kemp.SP2010.Badges.Badges.BadgesUserControl" %> 

<!--Button to generate the Badges in a MS Word Document--> 

<tr> 
<td valign="top" width="100%"> 

<asp:Button ID="GenerateBadges" runat="server" Text="Generate Badges" /> 

</td> 
</tr> 
</table> 

Web部件這shoould在理論上創建一個Word文檔以段落和表按鈕時,SE lected。但如前所述,沒有任何反應。

任何sugestions將不勝感激。

非常感謝

+0

您需要在服務器上完整安裝MS Word才能使其工作。 – 2012-02-21 11:02:36

+0

順便說一句:命名空間'Microsoft.Office.Server'已棄用:http://msdn.microsoft.com/en-us/library/microsoft.office.server.aspx – 2012-02-21 11:04:18

回答

0

考慮與Open Document libraries from Microsoft摔跤。他們有一個可怕的API,但:

  • 能產生的docx文件,這是由微軟Word 2007+
  • 支持不要求MS Word的安裝本機格式(其實是100%的.NET代碼)。
  • 實際上支持單詞via the compatibility pack的大多數版本(您可以讓它們在Word 2000或更高版本中打開)。
  • 也通過新版本的開放式辦公
+0

這聽起來很誘人,但這意味着必須安裝它並在我們的開發環境中進行配置。點擊webaprt按鈕後,必須有一種更簡單的方式來創建或打開word文檔。 – 2012-02-21 11:41:11

+0

您只需添加對.NET程序集的引用即可。你不會比這更簡單。 – 2012-02-21 11:43:35

0

你應該考慮使用Open XML Format,而不是支持。它只適用於docx文檔,但在製作Office Word文檔或操作文檔時很簡單