【问题标题】:Getting error when open word document file which is generated using .net打开使用 .net 生成的 Word 文档文件时出错
【发布时间】:2010-07-17 16:36:31
【问题描述】:

我正在使用下面的代码使用 asp.net/c#.net 生成 word 文档,请告诉我我在哪里失踪 它正在生成word文件,所有内容都是他们的..但问题是当我打开它时它给了我错误: “Office Open XML 文件 filename.doc 无法打开,因为内容存在问题” 点击确定后,我得到了我需要的文件。请告诉我如何删除这个错误。

在错误详情中:文件已损坏,无法打开

在它再次收到错误消息后:“word found unreadable content in myfile.do you want to recovery this document的内容吗?如果您信任该文档的来源,请单击yes”

using System;
 using System.Collections.Generic;
using System.Text;
using Microsoft.Office;
using Microsoft.Office.Interop.Word;
 public class clsWordLetterGenerator
{
    private int m_Enquiryid;
    private DateTime m_EnquiryDate;
    private string m_Filename;
    private string m_Templatepath;
    private string m_Templatename;
    private string m_Lettertext;
    private string m_Recieptentfirstname;
    private string m_Recieptentlastname;
    private DateTime m_Letterdate;
    private string m_Generatedfilepath;
    private string m_Generatedfilename;
    private string m_Subjecttext;
    private Microsoft.Office.Interop.Word.Application oWord;
    private Microsoft.Office.Interop.Word.Document oWordDoc;

    public string FileName
    {
        get { return m_Filename; }
        set { m_Filename = value; }
    }

    public string LetterText
    {
        get { return m_Lettertext; }
        set { m_Lettertext = value; }
    }

    public string RecieptentFirstName
    {
        get { return m_Recieptentfirstname; }
        set { m_Recieptentfirstname = value; }
    }

    public string RecieptentLastName
    {
        get { return m_Recieptentlastname; }
        set { m_Recieptentlastname = value; }
    }

    public string SubjectText
    {
        get { return m_Subjecttext; }
        set { m_Subjecttext = value; }
    }

    public string GenerateFileName
    {
        get { return m_Generatedfilename; }
        set { m_Generatedfilename = value; }
    }

    public string GenerateFilePath
    {
        get { return m_Generatedfilepath; }
        set { m_Generatedfilepath = value; }
    }

    public string TemplatePath
    {
        get { return m_Templatepath; }
        set { m_Templatepath = value; }
    }

    public string TemplateName
    {
        get { return m_Templatename; }
        set { m_Templatename = value; }
    }

    public int EnquiryId
    {
        get { return m_Enquiryid; }
        set { m_Enquiryid = value; }
    }

    public DateTime EnquiryDate
    {
        get { return m_EnquiryDate; }
        set { m_EnquiryDate = value; }
    }


    public clsWordLetterGenerator()
    {
          m_Letterdate = DateTime.Now;
        m_Filename = Guid.NewGuid().ToString();
    }

    public void GenerateLetter()
    {
        oWord = new Microsoft.Office.Interop.Word.Application();

        oWordDoc = new Microsoft.Office.Interop.Word.Document();

        //OBJECT OF MISSING "NULL VALUE"

        Object oMissing = System.Reflection.Missing.Value;

        //OBJECTS OF FALSE AND TRUE

        Object oTrue = true;

        Object oFalse = false;

        //CREATING OBJECTS OF WORD AND DOCUMENT

        //MAKING THE APPLICATION VISIBLE

        //oWord.Visible = true;

        //ADDING A NEW DOCUMENT TO THE APPLICATION

        oWordDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);


        oMissing = System.Reflection.Missing.Value;



        //OBJECTS OF FALSE AND TRUE

        oTrue = true;

        oFalse = false;

        //CREATING OBJECTS OF WORD AND DOCUMENT




        //THE LOCATION OF THE TEMPLATE FILE ON THE MACHINE

        Object oTemplatePath = m_Templatepath + "\\" + m_Templatename;

        //ADDING A NEW DOCUMENT FROM A TEMPLATE

        oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);

        int iTotalFields = 0;
        foreach (Microsoft.Office.Interop.Word.Field myMergeField in oWordDoc.Fields)
        {

            iTotalFields++;

            Microsoft.Office.Interop.Word.Range rngFieldCode = myMergeField.Code;

            String fieldText = rngFieldCode.Text;



            // ONLY GETTING THE MAILMERGE FIELDS

            if (fieldText.StartsWith(" MERGEFIELD"))
            {

                // THE TEXT COMES IN THE FORMAT OF

                // MERGEFIELD  MyFieldName  \\* MERGEFORMAT

                // THIS HAS TO BE EDITED TO GET ONLY THE FIELDNAME "MyFieldName"

                Int32 endMerge = fieldText.IndexOf("\\");

                Int32 fieldNameLength = fieldText.Length - endMerge;

                //String fieldName = fieldText.Substring(11, endMerge - 11);
                String fieldName = fieldText.Replace("MERGEFIELD", "");

                // GIVES THE FIELDNAMES AS THE USER HAD ENTERED IN .dot FILE

                fieldName = fieldName.Trim();


                // **** FIELD REPLACEMENT IMPLEMENTATION GOES HERE ****//

                // THE PROGRAMMER CAN HAVE HIS OWN IMPLEMENTATIONS HERE

                //if (fieldName.ToUpper()  == "FIRST_NAME")
                //{

                //    myMergeField.Select();

                //    oWord.Selection.TypeText("Bhaskar");

                //}

                switch (fieldName.ToUpper())
                {
                    case "FIRST_NAME":
                        myMergeField.Select();
                        oWord.Selection.TypeText(m_Recieptentfirstname);
                        break;

                    case "LAST_NAME":
                        myMergeField.Select();
                        oWord.Selection.TypeText(m_Recieptentlastname);
                        break;

                    case "LETTERDATE":
                        myMergeField.Select();
                        oWord.Selection.TypeText(m_Letterdate.ToShortDateString());
                        break;

                    case "GREETINGLINE":
                        myMergeField.Select();
                        oWord.Selection.TypeText(" " + m_Enquiryid.ToString() + " Dated " + m_EnquiryDate.ToString());
                        break;


                    case "LETTERSUBJECT":
                        myMergeField.Select();
                        oWord.Selection.TypeText(m_Subjecttext);
                        break;

                    case "LETTERBODY":
                        myMergeField.Select();
                        oWord.Selection.TypeText(m_Lettertext);
                        break;

                    default:
                        break;
                }


            }
            else
            {
                if (fieldText.ToUpper().Trim().Contains("GREETING"))
                {
                    myMergeField.Select();
                    oWord.Selection.TypeText(" " + m_Enquiryid.ToString() + " Dated " + m_EnquiryDate.ToString("dd-MM-yyyy"));
                }
            }

        }

        //SETTING THE VISIBILITY TO TRUE

        //oWord.Visible = true;
    }

    public  void SaveFile()
    {
        Object oSaveAsFile = (Object)m_Generatedfilepath + "\\" + m_Generatedfilename;
        Object oMissing = System.Reflection.Missing.Value;

        oWordDoc.SaveAs(ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,

            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

            ref oMissing, ref oMissing);
    }

    public void ClearMe()
    {
        Object oMissing = System.Reflection.Missing.Value;
        Object oFalse = false;
        //CLOSING THE FILE
        oWordDoc.Close(ref oFalse, ref oMissing, ref oMissing);

        //QUITTING THE APPLICATION
        oWord.Quit(ref oMissing, ref oMissing, ref oMissing);

    }

}

【问题讨论】:

  • 点击详情按钮可查看错误详情

标签: c# asp.net document


【解决方案1】:

难道 Microsoft.Office.Interop.Word 是用于旧版 word 的吗?如果是这样,我建议使用 System.IO.Packaging 中处理读取和写入 Open Xml 文档的功能。几年前,我编写了一个应用程序,我在其中获取了一个 Word Open Xml 文档模板并将数据合并到其中以生成数千封信函、信用票据等。所以我知道这种方法有效。

【讨论】:

    猜你喜欢
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多