基于WebForms的操作Word(小宝.NET)测试的问题!
首先一个问题就是按照帖子的介绍,我完成添加MSWORD.OLB引用后,MSWORD.OLB并没有转化为相关DLL文件,在项目的BIN目录下没有相关的DLL文件,请问是哪里出问题了?
先放上源代码:
第二个问题就是代码中黑体的Word缺少引用,我试图通过using Microsoft.Office.Interop引用此命名空间,但是运行后还是同样的错误!这又是怎么回事呢?如果在黑体的Word前面都加上Microsoft.Office.Interop.就可以编译通过。
第三个问题就是,编译通过以后
提示该行未将对象引用设置到对象实例,尝试着改了几个形式还是不对,请问到底应该怎么改呢?请大家帮忙给看一下,非常感谢!
先放上源代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace word
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox SaveAs;
protected System.Web.UI.WebControls.Button Button;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label result;
protected System.Web.UI.WebControls.TextBox wordText;
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
public void Button_Click(object sender, System.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,1,1,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>生成结果:成功!";
}
private void Page_Load(object sender, System.EventArgs e)
{
}
}
}
完成CS源码后,我们就可以设计WebForm页面了,完整的代码如下:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="word.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>基于WebForms的操作Word</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body ms_positioning="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:TextBox id="wordText" style="Z-INDEX: 101; LEFT: 144px; POSITION: absolute; TOP: 129px" runat="server" Height="190px" Width="360px" TextMode="MultiLine"></asp:TextBox>
<asp:TextBox id="SaveAs" style="Z-INDEX: 102; LEFT: 143px; POSITION: absolute; TOP: 80px" runat="server" Width="360px">C:\myword.doc</asp:TextBox>
<asp:Button id="Button" style="Z-INDEX: 103; LEFT: 237px; POSITION: absolute; TOP: 340px" runat="server" Width="98px" on OnClick="Button_Click" Text="生成Word文档"></asp:Button>
<INPUT style="Z-INDEX: 104; LEFT: 361px; WIDTH: 49px; POSITION: absolute; TOP: 340px; HEIGHT: 24px" type="reset" value="重填" size="20"></FONT>
<FONT face="宋体">基于WebForms的操作Word(小宝.NET)</FONT>
<asp:Label id="Label1" style="Z-INDEX: 105; LEFT: 143px; POSITION: absolute; TOP: 54px" runat="server" Width="187px" Height="18px">Word文件保存路径:</asp:Label>
<asp:Label id="Label2" style="Z-INDEX: 106; LEFT: 142px; POSITION: absolute; TOP: 107px" runat="server" Width="159px" Height="12px">Word文件内容:</asp:Label>
<asp:Label id="result" style="Z-INDEX: 107; LEFT: 148px; POSITION: absolute; TOP: 387px" runat="server" Width="352px" Height="18px" ForeColor="Red"></asp:Label>
</form>
</body>
</HTML>
第二个问题就是代码中黑体的Word缺少引用,我试图通过using Microsoft.Office.Interop引用此命名空间,但是运行后还是同样的错误!这又是怎么回事呢?如果在黑体的Word前面都加上Microsoft.Office.Interop.就可以编译通过。
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,1,1,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);
第三个问题就是,编译通过以后
//增加一表格
Word.Table table=WordDoc.Tables.Add(WordApp.Selection.Range,1,1,ref Nothing,ref Nothing); 提示该行未将对象引用设置到对象实例,尝试着改了几个形式还是不对,请问到底应该怎么改呢?请大家帮忙给看一下,非常感谢!
...全文
相关推荐
【WebForms王者归来】在 ASP.NET Core 中运行 WebForms 业务代码,99%相似度!
根据官方测试,ASP.NET Core的应用程序比传统的ASP.NET应用程序快很多倍,这对于需要处理大量并发请求的网站尤为重要。
我们可以! 开源DaST与MVC和WebForms竞争
MVC鼓励测试驱动的开发,提供灵活的架构,但对新手可能学习曲线较陡峭。然后是ASP.NET WebForms,它是.NET Framework的传统Web开发模型。
Cross-Origin Resource Sharing in ASP.NET WebForms
在ASP.NET WebForms中,开发者经常会遇到由于同源策略限制导致的跨域问题,这时就需要利用CORS来解决。
aspnet.webforms-angularjs:带有Angularjs 1.x的Asp.Net Webforms
它扩展了HTML,使其能够声明式地处理数据绑定和DOM操作。AngularJS的核心特性包括数据绑定、依赖注入、指令、服务、过滤器和路由,这些都极大地增强了Web应用的可维护性和可测试性。
Facade:我的WebForms-> ASP.NET Core MVC流项目
**ASP.NET Core MVC**:是ASP.NET的最新版本,支持跨平台开发,并且采用了模型-视图-控制器(MVC)架构模式,强调代码的分离和可测试性。


