文件流问题文件无法打开

时间:2016-08-25 09:57:31

标签: c# .net ms-word office-interop filestream

这里我将参数写入已创建的word文件,并在创建相关文件后将其保存到新文件中:

public void CreateWordDocument(object fileName,
                                    object saveAs, string nbt, string vat,string total)
    {
        Word._Application wApp = new Word.Application();
        //Set Missing Value parameter - used to represent
        // a missing value when calling methods through
        // interop.
        object missing = System.Reflection.Missing.Value;

        //Setup the Word.Application class.
        Word.Application wordApp = 
            new Word.ApplicationClass();

        //Setup our Word.Document class we'll use.
        Word.Document aDoc = null;

        // Check to see that file exists
        if (File.Exists((string)fileName))
        {
            DateTime today = DateTime.Now;

            object readOnly = false;
            object isVisible = false;
            //Set Word to be not visible.
            wordApp.Visible = false;
            //Open the word document
            aDoc = wordApp.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);
            // Activate the document
            aDoc.Activate();                
            // Find Place Holders and Replace them with Values.
            this.FindAndReplace(wordApp, "NBT",nbt);
            this.FindAndReplace(wordApp, "VAT", vat);
            this.FindAndReplace(wordApp, "TOTAL", total)                                                
        }
        else
        {
            MessageBox.Show("File dose not exist.");
            return;
        }
        //Save the document as the correct file name.
        aDoc.SaveAs(ref saveAs, ref missing, ref missing, ref missing, 
                ref missing, ref missing, ref missing, ref missing, 
                ref missing, ref missing, ref missing, ref missing, 
                ref missing, ref missing, ref missing, ref missing);
        //Close the document - you have to do this.
        aDoc.Close(ref missing, ref missing, ref missing);
        MessageBox.Show("File created.");
        wordApp.Visible = true;            
        //using (var filestresam = new FileStream(@"D:\Invoice\new.docx", FileMode.Open, FileAccess.Read));
        using (FileStream stream =File.Open(@"D:\Invoice\new.docx",FileMode.Open,FileAccess.Read,FileShare.Read));

    }
    private void FindAndReplace(Word.Application WordApp, 
                                object findText, 
                                object replaceWithText)
    {
        object matchCase = true;
        object matchWholeWord = true;
        object matchWildCards = false;
        object matchSoundsLike = false;
        object nmatchAllWordForms = false;
        object forward = true;
        object format = false;
        object matchKashida = false;
        object matchDiacritics = false;
        object matchAlefHamza = false;
        object matchControl = false;
        object read_only = false;
        object visible = true;
        object replace = 2;
        object wrap = 1;

        WordApp.Selection.Find.Execute(ref findText,
            ref matchCase, ref matchWholeWord,
            ref matchWildCards, ref matchSoundsLike,
            ref nmatchAllWordForms, ref forward,
            ref wrap, ref format, ref replaceWithText,
            ref replace, ref matchKashida,
            ref matchDiacritics, ref matchAlefHamza, 
            ref matchControl);
    }

除了

之外,每一个都完美无缺
using (FileStream stream =File.Open(@"D:\Invoice\new.docx",FileMode.Open,FileAccess.Read,FileShare.Read));

相关文件没有打开没有显示的错误也无法找到问题

2 个答案:

答案 0 :(得分:0)

using语句行以冒号结束:它没有正文。

因此文件将被打开,并立即关闭。

你需要在他们的东西中加入一些东西来打开文件:

using (var strm = File.Open(…)) {
  strm.Write(some data);
}

答案 1 :(得分:0)

您似乎希望File.Open()实际上使用与文件扩展名关联的程序打开文件。

这不是该方法的作用,请参阅MSDN: File.Open Method

  

在指定路径上打开 FileStream

事实上你正在尝试Open file with associated application

System.Diagnostics.Process.Start(@"D:\Invoice\new.docx");

请注意,这不适用于ASP.NET上下文,但仅适用于实际在客户端上运行的应用程序。