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

本文关键字:创建 Word 文档 代码 | 更新日期: 2023-09-27 18:25:21

我使用了微软的一个示例,介绍如何从代码背后创建word文档。我在一个简单的web部件中实现了这段代码,以便首先对其进行测试。我有一个按钮,当按下它时,它可以创建Word文档,但目前单击该按钮时什么都不会发生。我不确定我在这里缺少了什么,所以我希望有人能提供一个见解,下面是我目前拥有的代码:(我只列出的重要部件


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
}
}
}

这是带有按钮的web部件

<%@ 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>

理论上,当选择按钮时,这种方法可以创建一个带有段落和表格的Word文档。但如前所述,什么也没发生。

任何建议都将不胜感激。

非常感谢

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

考虑与Microsoft的开放文档库进行斗争。他们有一个可怕的API但是:

  • 可以生成MS Word 2007支持的本地格式的docx文件+
  • 不需要安装MS Word(实际上是100%的.NET代码)
  • 实际上,通过兼容包支持大多数版本的word(您可以在Word2000或更高版本中打开它们)
  • 最新版本的Open Office也支持

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