如何生成word文档的源码

本文介绍如何使用ASP.NET Web窗体应用程序创建并保存Word文档。通过具体代码示例,展示了如何在用户交互中生成Word文件,并将文档保存到指定路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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; 
using Word; 

namespace WebApplication2 

   
/// <summary> 
   
/// WebForm1 的摘要说明。 
   
/// </summary> 

   public class WebForm1 : System.Web.UI.Page 
   

      
protected System.Web.UI.WebControls.TextBox SaveAs; 
      
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.Button Button1; 
      
protected System.Web.UI.WebControls.TextBox wordText; 
      
private void Page_Load(object sender, System.EventArgs e) 
      

         
// 在此处放置用户代码以初始化页面 
      }
 

      
Web 窗体设计器生成的代码 


      
private void Button1_Click(object sender, System.EventArgs e) 
      

         Object Nothing
=System.Reflection.Missing.Value;  
         
object filename=@SaveAs.Text; 
         Word.Application WordApp
=new Word.ApplicationClass();  
         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.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.Close(
ref Nothing, ref Nothing, ref Nothing); 
         WordApp.Quit(
ref Nothing, ref Nothing, ref Nothing);  
         result.Text
="文档路径:<a href='"+SaveAs.Text+"'>"+SaveAs.Text+"</a>(点击链接查看)<br>生成结果:成功!"
      }
 
   }
 
    
}
 




在web.config中要设置权限。


windows+R----->......
windows+R----->dcomcnfg------>我的电脑----->microsoft word文档------>属性------>安全性------>访问控制

运行环境:windows2k+jakarta-tomcat V4.0.6+j2sdk1.4.1 说明:压缩包中包含了jspAmartUpload组件的部分内容。有关此组件,请到http://www.jspsmart.com下载 本示例演示了使用在JSP中结合NTKO Office文档控件开发的实例。包含从JSP中读取和写入Access数据库的代码。请参阅压缩包中的readme.html进行安装设置。 本安装指南适用于windows 2000平台+ -------------------------------------------------------------------------------- 安装步骤: 1、安装j2sdk-1_4_1-windows-i586.exe。 您可以到Sun公司的下载地址下载:http://java.sun.com/j2se/1.4.1/download.html. 下载后安装。比如,安装到D:j2sdk1.4.1目录。 2、正确设置JAVA_HOME环境变量。 设置JDK_HOME环境变量的值为您在第1步中安装的java sdk的位置,如:D:j2sdk1.4.1 3、安装jakarta-tomcat V4.0.6版本(您也可以测试新的版本,本示例开发时运行于V4.0.6版本) 您可以到apache的如下位置下载:jakarta-tomcat V4.0.6的下载地址:http://archive.apache.org/dist/jakarta/tomcat-4/archive/v4.0.6/bin/.注意要下载jakarta-tomcat-4.0.6.exe文件。 下载之后安装。比如,安装到D:Tomcat 4.0目录。 4、安装示例程序 将jspsmartupload.rar解压缩,并将解压缩后生成的jspsmartupload整个目录,拷贝到您在第3步中安装jakarta-tomcat的目录下的webapps目录下。比如,D:Tomcat 4.0webapps,注意需要连同目录“jspsmartupload”一起拷贝。 5、创建数据源。 使用ODBC数据源管理器,添加系统DSN,驱动程序选择"Miscosoft Access Driver(*.mdb)",数据源名填写"files",并按"选择"按钮选择jakarta-tomcat目录下的webappsjspsmartuploaduploadfiles.mdb文件。比如,D:Tomcat 4.0webappsjspsmartuploaduploadfiles.mdb。 6、从浏览器访问:http://your_tomcat_server_ip:8080/jspsmartupload/default.jsp.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值