使用.net生成的打开word文档文件时出错

时间:2010-07-17 08:11:17

标签: c# asp.net document

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

错误详细信息:文件已损坏且无法打开

之后再次收到错误消息:“word在myfile中找到了不可读的内容。你想恢复该文档的内容吗?如果你信任该文档的来源,请单击是”

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);

    }

}

1 个答案:

答案 0 :(得分:1)

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