2010-07-17 121 views
1

產生我使用下面的代碼使用assp.net/c#.net下,生成word文檔時打開的Word文檔文件,請告訴我,我缺少 它生成的Word文件和所有內容是他們的..但問題是,當我打開它,它給我的錯誤: 「Office打開XML文件filename.doc不能打開,因爲有問題與內容」 並單擊確定,讓我的所需的文件以後。 plez so plez告訴我如何消除這個錯誤。獲取錯誤,使用.NET

在錯誤的詳細信息:文件已損壞,無法打開

再次得到一個錯誤消息後:「字發現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); 

    } 

} 
+0

錯誤的詳細信息可以通過單擊詳細信息按鈕中找到 – volody 2010-07-17 08:27:32

回答

1

難道說的Microsoft.Office.Interop.Word是字的舊版本?如果是這樣,我會建議使用System.IO.Packaging中處理讀取和寫入Open Xml文檔的功能。我幾年前寫的應用程序,我需要一個模板的Word打開XML文檔和合並數據到它產生數以千計的信件,信用票據,等等。所以我知道這種方法有效。