Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Html转换为c#中的文档,并将其作为二进制文件返回_C#_Ms Word - Fatal编程技术网

将Html转换为c#中的文档,并将其作为二进制文件返回

将Html转换为c#中的文档,并将其作为二进制文件返回,c#,ms-word,C#,Ms Word,我有这个代码将html页面转换为文档,但我有一个问题 Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document wordDoc = new Microsoft.Office.Interop.Word.Document(); Object

我有这个代码将html页面转换为文档,但我有一个问题

Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word.Document wordDoc = new Microsoft.Office.Interop.Word.Document();
            Object oMissing = System.Reflection.Missing.Value;
            wordDoc = word.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            word.Visible = false;
            Object filepath = "c:\\pagina.html";
            Object confirmconversion = System.Reflection.Missing.Value;
            Object readOnly = false;
            Object saveto = "c:\\doc.pdf";
            Object oallowsubstitution = System.Reflection.Missing.Value;

            wordDoc = word.Documents.Open(ref filepath, ref confirmconversion, ref readOnly, 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);
            object fileFormat = WdSaveFormat.wdFormatPDF;
            wordDoc.SaveAs(ref saveto, ref fileFormat, ref oMissing, ref oMissing, ref oMissing,
                           ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                           ref oMissing, ref oMissing, ref oMissing, ref oallowsubstitution, ref oMissing,
                           ref oMissing);

如何在可变字节[]中保存而不在hdd中保存?

您可以将文档写入MemoryStream并从MemoryStream中读取它-读取方法类似于FileStream。
因此,不涉及硬盘驱动器-您已经有了字节[],我想您想将其写入HTTP头以便下载。

我不确定SaveAs方法是否支持memorystream,为什么不尝试在保存后以字节形式读取文件,然后将其从硬盘中删除。下面的行将以字节形式读取文件

 byte[] pdfFile = System.IO.File.ReadAllBytes((string)saveto);

在saveto中,我引用了memorystream?是的-这应该有效-因为它应该写入流,因此不应该关心它是memorystream还是filestreammddnt workMemoryStream myMemStream=new memorystream();Object savetoFile=myMemStream;wordDoc.SaveAs(ref-savetoFile,ref-fileFormat,ref-oMissing,ref-oMissing,ref-oMissing,ref-oMissing,ref-oMissing,ref-oMissing,ref-oMissing,ref-oMissing,ref-omission,ref-omission,ref-oMissing,ref-oMissing,ref-oMissing,ref-oMissing,ref-oMissing;