C#:Word自动化问题 [英] C#: Word Automation Problem

查看:135
本文介绍了C#:Word自动化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览器中编辑Word文档.每次我按保存按钮,都会生成一个副本.如何避免在asp.net中出现此重复副本?

I am editing a word document in the browser itself. Every time I press the save button it will generate a duplicate copy. How can I avoid this duplicate copy in asp.net?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop;
using Microsoft.Office;
public partial class MSWord_Display : System.Web.UI.Page
{
    string strPath = @"D:\sss\Tree\Tree\Tree\sand1.doc";
    //name= @"C:\Users\v-brahma\Downloads\Tree\Tree\sandhya_1.doc";
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (Request.Params.Count > 0)
        //{
        //IF(Request["word"]!=null)
        //{
        //if (Convert.ToString(Request["word"]).Length>0)
        //{//name = Convert.ToString(Request["word"]);
        if (!IsPostBack)
        {
            // Application  Class
            ApplicationClass oWordApp = new ApplicationClass();
            object fileName = strPath;
            object readOnly = false;
            object isVisible = true;
            object missing = System.Reflection.Missing.Value;
            try
            {
                Document oWordDoc = oWordApp.Documents.Open(
                                            ref fileName,
                                            ref missing, ref readOnly,
                                            ref missing, ref missing, ref missing,
                                            ref missing, ref missing, ref missing,
                                            ref missing, ref missing, ref isVisible,
                                            ref missing, ref missing, ref missing, ref missing);
                oWordDoc.Activate();
                //oWordDoc.LockServerFile(
                txtword.Text = oWordDoc.Content.Text;
                oWordDoc.Save();
                oWordDoc.Close(ref missing, ref missing, ref missing);
            }
            catch (Exception ex)
            {
                divMsword.InnerHtml = "OOPs  <b style=''color:red;''>Error Encountered Error while Reading The document" + ex.Message + "  </b>";
            }
            //}

            //else
            //{
            //    divMsword.InnerHtml = "<b> Document name is missing: </b>";
            //}
        }
    }
    protected void save_Click(object sender, EventArgs e)
    {
        ApplicationClass oWordApp = new ApplicationClass();
        object fileName = strPath;
        object readOnly = false;
        object isVisible = true;
        object missing = System.Reflection.Missing.Value;
        try
        {
            Document oWordDoc = oWordApp.Documents.Open(
                                        ref fileName,
                                        ref missing, ref readOnly,
                                        ref missing, ref missing, ref missing,
                                        ref missing, ref missing, ref missing,
                                        ref missing, ref missing, ref isVisible,
                                        ref missing, ref missing, ref missing, ref missing);
            oWordDoc.Activate();
            oWordDoc.Content.InsertAfter(txtword.Text);
            oWordDoc.Save();
            oWordDoc.Close(ref missing, ref missing, ref missing);

            divMsword.InnerHtml = "<b>Saved Sucess Fully </b> ";
        }
        catch(Exception ex)
        {
            divMsword.InnerHtml = "OOPs while saving <b style=''color:red;''>Error Encountered Error while Reading The document" + ex.Message + " while saving </b>";
        }
    }

}



[edit]代码块,主题已移至问题正文中-OriginalGriff [/edit]



[edit]Code block, subject moved into question body - OriginalGriff[/edit]

推荐答案

您正尝试在 ReadOnly 中打开您的Word文件>保存按钮上的模式单击
为什么会这样?
You are trying to open your word file in ReadOnly mode on save button click
why so ?




您可以轻松地 C#编辑Word 文件,而无需生成重复副本和 ASP.NET导出到Word 并带有C#/VB.NET Word 库.

该库不依赖于Microsoft Word,也不使用
Hi,

you can easily C# edit Word file without generating a duplicate copy and ASP.NET export to Word with this C# / VB.NET Word library.

This library doesn''t have any dependency on Microsoft Word and doesn''t use C# Word Automation which makes it ideal for ASP.NET server applications.

Here is a sample C# code:
// Use the component in free mode.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");

// Create and save an input document. 
// You don't have to do this if you already have an input document.
// This code is only provided as a reference how input document should look like.
var document = new DocumentModel();
document.Sections.Add(new Section(document, new Paragraph(document)));
document.Save(Path.Combine(Request.PhysicalApplicationPath, "Document.docx"), SaveOptions.DocxDefault);
// Instead use:
// Load an input document from application's root directory.
// var document = DocumentModel.Load(Path.Combine(Request.PhysicalApplicationPath, "Document.docx"), LoadOptions.DocxDefault);

// Edit first Run in first Paragraph or add new Run if Paragraph doesn't contain Run as first child.
var paragraph = document.Sections[0].Blocks.Cast<paragraph>(0);
if (paragraph.Inlines.Count > 0 && paragraph.Inlines[0].ElementType == ElementType.Run)
	paragraph.Inlines.Cast<run>(0).Text = txtword.Text;
else
	paragraph.Inlines.Add(new Run(document, txtword.Text));

// Microsoft Packaging API cannot write directly to Response.OutputStream.
// Therefore we use temporary MemoryStream.
using (MemoryStream documentStream = new MemoryStream())
{
	document.Save(documentStream, SaveOptions.DocxDefault);

	// Stream file to browser.
	Response.Clear();
	Response.ContentType = "application/vnd.openxmlformats";
	Response.AddHeader("Content-Disposition", "attachment; filename=Document.docx");

	documentStream.WriteTo(Response.OutputStream);

	Response.End();
}


这篇关于C#:Word自动化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆