Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.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
C# 将HTML文本转换为Word文档而不在本地保存_C#_.net_Ms Word - Fatal编程技术网

C# 将HTML文本转换为Word文档而不在本地保存

C# 将HTML文本转换为Word文档而不在本地保存,c#,.net,ms-word,C#,.net,Ms Word,我需要保存包含HTML内容的word文档。我有一个字符串变量中的html内容。由于安全原因,我无权在本地保存文件。我必须将文档作为字节流传递到另一台服务器。是否可以执行此操作。 我所看到的解决方案是将文档保存在本地并将其转换为字节流。 < P>如果您是一个选项,可以考虑使用文档转换SDK,该文档转换SDK可以在不保存本地任何内容的情况下执行转换。例如,我熟悉LEADTOOLS文档转换器类(因为我为供应商工作),它能够从HTML流转换为文档流 这方面的代码如下所示: MemoryStream ou

我需要保存包含HTML内容的word文档。我有一个字符串变量中的html内容。由于安全原因,我无权在本地保存文件。我必须将文档作为字节流传递到另一台服务器。是否可以执行此操作。
我所看到的解决方案是将文档保存在本地并将其转换为字节流。

< P>如果您是一个选项,可以考虑使用文档转换SDK,该文档转换SDK可以在不保存本地任何内容的情况下执行转换。例如,我熟悉LEADTOOLS文档转换器类(因为我为供应商工作),它能够从HTML流转换为文档流

这方面的代码如下所示:

MemoryStream outputDocStream = new MemoryStream();

// Example of the HTML Byte Array input
string HTMLString = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> <html xmlns=\"http://www.w3.org/1999/xhtml\" > <head> <title>Test HTML</title> <style type=\"text/css\"> .style1 { font-size: xx-large; } </style> </head> <body> <p> Hello <span class=\"style1\">World!</span></p> </body> </html>";
byte[] bytes = Encoding.UTF8.GetBytes(HTMLString);

// Extract string and write to memory stream
MemoryStream inputHTMLStream = new MemoryStream(Encoding.UTF8.GetBytes(HTMLString));

// Convert to Doc and place the output in DOC Word in the output memory stream
var options = new LoadDocumentOptions();
using (var document = DocumentFactory.LoadFromStream(inputHTMLStream, options))
{
   using (DocumentConverter documentConverter = new DocumentConverter())
   {
      var jobData = new DocumentConverterJobData();
      jobData.Document = document;
      jobData.DocumentFormat = DocumentFormat.Doc;
      jobData.OutputDocumentStream = outputDocStream;
      jobData.JobName = "conversion job";
      var job = documentConverter.Jobs.CreateJob(jobData);
      documentConverter.Jobs.RunJob(job);
   }
}
MemoryStream outputDocStream=new MemoryStream();
//HTML字节数组输入示例
string HTMLString=“Test HTML.style1{font size:xx large;}你好,世界!

”; byte[]bytes=Encoding.UTF8.GetBytes(HTMLString); //提取字符串并写入内存流 MemoryStream inputtmlStream=新的MemoryStream(Encoding.UTF8.GetBytes(HTMLString)); //转换为Doc并将Doc Word中的输出放在输出内存流中 var options=new LoadDocumentOptions(); 使用(var document=DocumentFactory.LoadFromStream(inputtmlstream,选项)) { 使用(DocumentConverter DocumentConverter=new DocumentConverter()) { var jobData=new DocumentConverterJobData(); jobData.Document=文档; jobData.DocumentFormat=DocumentFormat.Doc; jobData.OutputDocumentStream=outputDocStream; jobData.JobName=“转换作业”; var job=documentConverter.Jobs.CreateJob(jobData); documentConverter.Jobs.RunJob(作业); } }

输出内存流将包含生成的文档内容。

您可以利用
NetworkStream
类从字符串变量读取HTML内容,并将该流对象发送到其他服务器。