【问题标题】:How to read docx file from a URL using .NET如何使用 .NET 从 URL 读取 docx 文件
【发布时间】:2019-02-28 15:57:49
【问题描述】:

我想在 .NET core 2.2 框架中使用 Web HTTP 请求读取 word 文件的内容。

我尝试了以下代码:

// Create a new WebClient instance.
using (WebClient myWebClient = new WebClient())
{
    // Download the Web resource and save it into a data buffer.
    byte[] myDataBuffer = myWebClient.DownloadData(body.SourceUrl);

    // Display the downloaded data.
    string download = Encoding.ASCII.GetString(myDataBuffer);
}

输出:

无法从 URL 读取 .docx 文件的内容。如何在没有任何付费库或使用 HTTP Web 请求的情况下读取 docx 文件。

【问题讨论】:

    标签: c# asp.net-core openxml-sdk


    【解决方案1】:

    您可以使用OpenXml处理word文档:https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2010/cc535598(v=office.14)

    这可能就是你要找的东西:

    // Create a new WebClient instance.
    using (WebClient myWebClient = new WebClient())
    {
        // Download the Web resource and save it into a data buffer.
        byte[] bytes = myWebClient.DownloadData(body.SourceUrl);
        MemoryStream memoryStream = new MemoryStream(bytes);
    
        // Open a WordprocessingDocument for read-only access based on a stream.
        using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(memoryStream, false))
        {
            MainDocumentPart mainPart = wordDocument.MainDocumentPart;
            content = mainPart.Document.Body.InnerText;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多