求ASP.NET操作word实例。。
点击就打开WORD编辑完,点保存就保存到服务器上制定的文件夹下,文件名按时间,保证不重复。。
点击就打开WORD编辑完,点保存就保存到服务器上制定的文件夹下,文件名按时间,保证不重复。。
解决方案 »
- 这句话是什么意思?
- 网页中在层里播放一个视频 关闭层后视屏是没有了 但是还是有声音!
- 新站“欢迎大家”提建议~
- 没想到画条线都这么难吖
- 获取GridView的图片URL
- 错误 CS0246: 找不到类型或命名空间名称“_User”(是否缺少 using 指令或程序集引用?)
- VS2005中OBJECT数据源控件刷选出错 筛选条件应该怎么加财对呀
- 请教一个javascript脚本(当鼠标离开某个textbox框时,指定一个button为回车时的默认按钮)
- 怎么才能让HTML的TABLE不显示?
- 代码分离中的page_load问题
- |M| 判断符号 | 和|| 有什么样的区别 & &&
- 怎样解决点IE的后退按钮。网页乱码
怎么没个高手来,看看
楼主说的这些不懂.
http://blog.joycode.com/kaneboy/archive/2004/11/03/37889.aspx
http://edu.lzcg.com/Document/ASP/221223319.htm
Type wordType = word.GetType();
Word.Documents docs = word.Documents;
// 打开文件
Type docsType = docs.GetType();
object filename = "c:\\";
Word.Document doc = (Word.Document)docsType.InvokeMember("Open",System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] {filename, true, true});
// 转换格式,另存为
Type docType = doc.GetType();
object saveFileName = Server.MapPath("../") + "OfficeFile\\HTML\\"+ "aa.html";
//下面是Microsoft Word 9 Object Library的写法,如果是10,可能写成:
//docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML});
///其它格式:
///wdFormatHTML
///wdFormatDocument
///wdFormatDOSText
///wdFormatDOSTextLineBreaks
///wdFormatEncodedText
///wdFormatRTF
///wdFormatTemplate
///wdFormatText
///wdFormatTextLineBreaks
///wdFormatUnicodeText
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatHTML});
// 退出 Word
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod,null, word, null);
ASP.NET操作Word文档一直是一个大家比较关心的话题,其实在ASP.NET里操作Word文档一点也不难,大家只需按本文提示,就能轻轻松松操作Word文档!
一、准备工作
首先请确认服务端已经安装了Office Word(以下将以Office XP为例),操作系统为win2000或XP,并且已配置好.NET的运行环境及安装VS.NET C#开发环境后,我们就可以打开VS.NET,并新建一个Visual C#项目>ASP.NET Web应用程序,位置为“http://localhost/word”。
二、引用Word对象库文件
要操作Word,我们就需要Word的对象库文件“MSWORD.OLB”(word 2000为MSWORD9.OLB),通常安装了Office Word后,你就可以在office安装目录的Office10文件夹下面找到这个文件,当我们将这个文件引入到项目后,我们就可以在源码中使用各种操作函数来操作Word。具体做法是打开菜单栏中的项目>添加引用>浏览,在打开的“选择组件”对话框中找到MSWORD.OLB后按确定即可引入此对象库文件,vs.net将会自动将库文件转化为DLL组件,这样我们只要在源码中创建该组件对象即可达到操作Word的目的!
三、Webform1.aspx.cs代码(vs.net2003默认新建页文件名都为webform1,在vs.net2005里已经换成了default1.aspx.cs了)
完成添加引用后,MSWORD.OLB已经转化为相关DLL文件并放置于项目的BIN目录下了,这样我们只需在源码中创建该对象,并使用word库文件内置的操作函数即可轻松实现操作Word,Webform1.aspx.cs源码如下: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;
/**////这些引用都可以不要了.在vs.net 2005里的web.config已经添加了上述引用.
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;
Web form Designer generated code#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 [email protected];
//创建一个名为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)
...{
}
}
}
<%...@ 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>
五、web.config设置
web.config文件还需添加一句 <identity impersonate="true"/>以启用模拟身份,因为默认ASPNET这个用户是没有权限访问Word.ApplicationClass(),当启用模拟身份后所有页面将会使用匿名Internet用户帐户(IUSR_machinename)这个用户名的权限执行,这样我们就能成功访问Word.ApplicationClass()并在ASP.NET中操作Word!
amandag(高歌) ( ) 已经给答案了操作word的话我一般是用标签做的,遍历标签,填数据,只能用在格式固定的情况下。自由格式没做过
报错。; 找不到类型或命名空间名称“Word”(是否缺少 using 指令或程序集引用?)
错误代码:
object filename = fname;
行 38: //object filename = Environment.CurrentDirectory + "\\temp";
行 39: Word.Application WordApp = new Word.ApplicationClass();
行 40: WordApp.Visible = true;
行 41: Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
原代码:protected void Button1_Click(object sender, EventArgs e)
{
string fname = "";
fname = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); fname = fname.Trim();
fname = fname.Replace("-", "");
fname = fname.Replace("/", "");
fname = fname.Replace(" ", "");
fname = fname.Replace(":", "");
fname = fname.Replace("PM", "");
fname = fname.Replace("AM", "");
fname = fname.Replace("上午", "");
fname = fname.Replace("下午", ""); Object Nothing = System.Reflection.Missing.Value;
//object filename = @SaveAs.Text;
object filename = fname;
//object filename = Environment.CurrentDirectory + "\\temp";
Word.Application WordApp = new Word.ApplicationClass();
WordApp.Visible = true;
Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
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);
}
然后
Using Word = Microsoft.Office.Interop.Word;
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(this.Title, Encoding.UTF8) + "-" + DateTime.Today.ToString("d") + ".xls;charset=gb2312");
content=text/html;charset=gb2312>");
HttpContext.Current.Response.ContentType = "application/ms-word";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}