不知道客户为什么要这么做,直接用了office,在搭配web程序不就OK了吗?

解决方案 »

  1.   

    : xport(郁闷中...) 
    我也是这样想的,他们总以为你这几千元很多,找机会折磨你呜呜。
      

  2.   

    方法很简单:
    1首先请确认服务端已经安装了Office Word(以下将以Office XP为例),操作系统为win2000或XP,并且已配置好.NET的运行环境及安装VS.NET C#开发环境后,我们就可以打开VS.NET,并新建一个Visual C#项目>ASP.NET Web应用程序,位置为“http://localhost/word”。(如图一)
    2要操作Word,我们就需要Word的对象库文件“MSWORD.OLB”(word 2000为MSWORD9.OLB),通常安装了Office Word后,你就可以在office安装目录的Office10文件夹下面找到这个文件,当我们将这个文件引入到项目后,我们就可以在源码中使用各种操作函数来操作Word。具体做法是打开菜单栏中的项目>添加引用>浏览,在打开的“选择组件”对话框中找到MSWORD.OLB后按确定即可引入此对象库文件,vs.net将会自动将库文件转化为DLL组件,这样我们只要在源码中创建该组件对象即可达到操作Word的目的!
    3完成添加引用后,MSWORD.OLB已经转化为相关DLL文件并放置于项目的BIN目录下了,这样我们只需在源码中创建该对象,并使用word库文件内置的操作函数即可轻松实现操作Word,WebForm1.aspx.cs源码如下:
      

  3.   

    接上:
    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);}
    #endregionpublic 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)
    {
    }
    }
    }
      

  4.   

    接上:
    4完成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>
      

  5.   

    5web.config文件还需添加一句 <identity impersonate="true"/>以启用模拟身份,因为默认ASPNET这个用户是没有权限访问Word.ApplicationClass(),当启用模拟身份后所有页面将会使用匿名Internet用户帐户(IUSR_machinename)这个用户名的权限执行,这样我们就能成功访问Word.ApplicationClass()并在ASP.NET中操作Word!
    6同样,此方法也可就用于操作Excel,只不过引用的库文件不同而已。
      

  6.   

    http://expert.csdn.net/Expert/topic/1690/1690229.xml?temp=.6692469
      

  7.   

    unfor(myes) 你好,有成功操作的例子吗?
    也就是说服务器端只要安装XP就OK了?客户端需要配置吗?
      

  8.   

    可以这样,我已完成生成word文件,并且上传到数据库了,但是有个问题我很苦恼:
    这样大家操作客户端时在服务器段会起无数个word进程的,服务器开销太大了
      

  9.   

    ref Nothing是什么意思啊???
      

  10.   

    caoxiaohua(caoxh)
    提供一点思路呀
      

  11.   

    可以用ASP.NET结合C#来作,这是一段C#的示例程序,不知道对你有没有帮助.// ==============================================================================
    // Filename: wordapp.cs
    //
    // Summary:  C# application demonstrating the use of the Microsoft Word object model
    //
    // This file is part of the Microsoft CLR Samples
    //
    // Copyright (C) 2000 - 2001 Microsoft Corporation. All rights reserved
    //
    // This source code is intended only as a supplement to Microsoft
    // Development Tools and/or on-line documentation.  See these other
    // materials for detailed information reagrding Microsoft code samples.
    //
    // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
    // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
    // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
    // PARTICULAR PURPOSE.
    //using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using System.Reflection;
    using System.Collections;
    using System.Threading;
    using Word;namespace WordApp
    {
    class WordAppMain
        {
    static object missing = Missing.Value;

         static int Main()
         {

    WordAppMain myWord = new WordAppMain();
    int return_Result = 0;

    // Create a word object that we can manipulate
    Word.Application Word_App = null;
    Word.Document Word_doc=null;
    try
    {
    Word_App = new Word.Application();
    Word_doc=new Word.Document();
    }
    catch(Exception e)
    {
    Console.WriteLine("Can't create a word document " + e.ToString());
    return_Result = 1;
    goto Exit;
    } AutoCorrect autocorrect = Word_App.AutoCorrect;
    Word.AutoCorrectEntries autoEntries = autocorrect.Entries;  string theEnd= "\nThe End";
    autoEntries.Add("Inntroduction", "Introduction"); Word.Documents Docs = Word_App.Documents;
    if (Docs == null)
    {
    Console.WriteLine("Docs is null");
    }
    else
    {
    Console.WriteLine("Docs exists:" + Docs.Count);
    } Word_App.Visible=true;
    Word._Document my_Doc= (Word._Document) Word_doc;
    Word_doc=Docs.Add(ref missing, ref missing, ref missing, ref missing); object start = 0;
    object end = 0;
    Word.Range range = Word_doc.Range(ref missing,ref missing);

    // add text to the doc -- this contains some deliberate misspellings so that we can correct them in a short while
    range.Text="Microsoft Word Interoperability Sample\n\nInntroduction:\n\nMicrosoft .NET will alow the creation of truly distributed XML Web services. These services will integrate and collaborate with a range of complementary services to work for customers in ways that today's internet companies can only dream of. Microsoft .NET will drive the Next Generation Internet and will shift the focus from individual Web sites or devices connected to the Internet, to constellations of computers, devices, and services that work together to deliver broader, richer solutions.\nFor more info go to:\n   "; // Wait so the starting state can be admired
    Thread.Sleep(2000); // Format the title
    Word.FontClass fc= new Word.FontClass();
    try
    {
    Console.WriteLine("Formatting the title");
    start = 0; end = 40;
    range=Word_doc.Range(ref start, ref end);
    fc.Size=24;
    fc.Bold=1;
    fc.Color=Word.WdColor.wdColorGray30;
    range.Font=fc;
    start = 40; end = 54;
    range=Word_doc.Range(ref start, ref end);
    fc.Size=14;
    range.Font=fc; }
    catch(Exception e)
    {
    Console.WriteLine(" Font exception:{0}", e.ToString());
    }
    // Wait so the new formatting can be appreciated
    Thread.Sleep(3000); autocorrect.ReplaceTextFromSpellingChecker=true;
    // Fix inntroduction
    object obj = "Inntroduction";
    Word.AutoCorrectEntry errEntry= autoEntries.Item(ref obj); Word.Words myWords=Word_doc.Words;
    Word.Range errRange= myWords.Item(7);
    errEntry.Apply(errRange); // Add a caption to the window and get it back 
    Word.Window myWindow = Word_App.ActiveWindow;
    myWindow.Caption = "Managed Word execution from C# ";
    string gotCaption = myWindow.Caption;
    if (gotCaption.Equals("Managed Word execution from C# "))
    {
    Console.WriteLine("Caption assigned and got back");
    return_Result = 1;
    }
    Thread.Sleep(2000); // define the selection object, find and  replace text
    Word.Selection mySelection = myWindow.Selection; 
    Word.Find myFind = mySelection.Find;
    object findText = "alow";
    object replaceText ="allow"; // Find "alow" and replace with "allow"
    try
    {
    myFind.Execute(ref findText,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref replaceText,ref missing,ref missing,ref missing,ref missing,ref missing);
    }
    catch(Exception e)
    {
    Console.WriteLine(e);
    }
    Thread.Sleep(2000);
    Console.WriteLine(myFind.Text + " has been corrected"); try
    {
    start = 65; end = 69;
    range=Word_doc.Range(ref start, ref end);
    Console.WriteLine("The color of .NET is being changed");
    fc.Bold=16;
    fc.Color= Word.WdColor.wdColorLavender; 
    range.Font=fc; }
    catch(Exception e)
    {
    Console.WriteLine(" Font exception:{0}", e.ToString());
    }
    Thread.Sleep(2000);

    // underline the selected text
      range=Word_doc.Range(ref start,ref end);
    range.Underline=(Word.WdUnderline.wdUnderlineDouble); // add hyperlink and follow the hyperlink
    Word.Hyperlinks my_Hyperlinks = Word_doc.Hyperlinks; // Make the range past the end of all document text
    mySelection.Start = 9999;
    mySelection.End   = 9999;
    range = mySelection.Range; // Add a hyperlink
    string myAddress = "http://go.microsoft.com/fwlink/?linkid=3269&clcid=0x409";
    object obj_Address = myAddress;
    Console.WriteLine("Adding hyperlink to the document");
    Word.Hyperlink my_Hyperlink1= my_Hyperlinks._Add(range, ref obj_Address, ref missing);  
    Word_App.ActiveWindow.Selection.InsertAfter("\n"); Thread.Sleep(5000); // Open a window to Hyperlink
    Process ie = Process.Start("iexplore.exe", my_Hyperlink1.Address); // Wait for a short spell to allow the page to be examined
    Thread.Sleep(10000);

    // close the browser first
    Console.WriteLine("Removing browser window");
    ie.Kill(); // Display "The End"
    Word_App.ActiveWindow.Selection.InsertAfter(theEnd);
    Word_App.ActiveWindow.Selection.Start = 0;
    Word_App.ActiveWindow.Selection.End = 0;
    Word_App.Activate();
    Thread.Sleep(5000); // Close Microsoft Word
    object myBool = Word.WdSaveOptions.wdDoNotSaveChanges;
    Word_App.ActiveWindow.Close(ref myBool,ref missing); Word_App.Quit(ref missing, ref missing, ref missing);     Exit:
    return return_Result;
        }
    }
    }
      

  12.   

    为什么我用Unfor提供的代码报下面的错:
    Server Error in '/OnLineEditWord' Application.
    --------------------------------------------------------------------------------无法打开宏储存。 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Runtime.InteropServices.COMException: 无法打开宏储存。Source Error: 
    Line 54: 
    Line 55:  //创建一个名为WordDoc的文档对象
    Line 56:  Word.Document WordDoc=WordApp.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing);
    Line 57:  //增加一表格
    Line 58:  Word.Table table=WordDoc.Tables.Add(WordApp.Selection.Range,1,1,ref Nothing,ref Nothing);
     Source File: c:\inetpub\wwwroot\onlineeditword\webform1.aspx.cs    Line: 56