在c#后面的代码中创建Word文档

时间:2012-02-21 11:00:49

标签: c# sharepoint-2010 ms-word

我使用了microsoft的一个例子,介绍了如何从后面的代码创建word文档。我已在一个简单的Web部件中实现了此代码,以便首先对其进行测试。我有一个按钮,按下时会创建Word文档,但是当单击按钮时,当前没有任何反应。我不确定我在这里缺少什么,所以我希望有人可以提供一个见解,下面是我目前的代码:(我将列出importat部分


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>

理论上,这可以在选择Button时创建带有段落和表格的Word文档。但如前所述,没有任何事情发生。

任何sugestions将不胜感激。

非常感谢

2 个答案:

答案 0 :(得分:0)

考虑与Open Document libraries from Microsoft进行摔跤。他们有一个可怕的API但是:

  • 可以生成MS Word 2007 +
  • 支持的本机格式的docx文件
  • 不需要安装MS Word(实际上是100%.NET代码)。
  • 实际上支持大多数版本的单词via the compatibility pack(您可以在Word 2000或更高版本中打开它们。)
  • 最近版本的Open Office也支持

答案 1 :(得分:0)

您应该考虑使用Open XML Format。它只适用于docx文档,但在制作Office Word文档或操作word文档时非常简单