原創(chuàng)|行業(yè)資訊|編輯:胡濤|2022-04-07 16:38:07.603|閱讀 229 次
概述:本篇文章主要介紹了如何使用Spire.Doc 在 C#、VB.NET 中從 URL 下載 Word 文檔,歡迎查閱!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
如今,我們面臨著從 URL 下載文件的更大機會,因為更多的文件是通過互聯網以電子方式傳遞的。在本文中,我將介紹如何在 C#、VB.NET 中使用 Spire.Doc 以編程方式從 URL 下載 Word 文檔。
Spire.Doc 不提供直接從 URL 下載 Word 文件的方法。但是,您可以將文件從 URL 下載到 MemoryStream 中,然后使用 Spire.Doc for .NET MemoryStream 加載文檔并作為新的 Word 文檔保存到本地文件夾。
代碼片段:
第 1 步:初始化一個新的 Word 文檔。
Document doc = new Document();
第 2 步:初始化 WebClient 類的新實例。
WebClient webClient = new WebClient();
第 3 步:調用WebClient.DownloadData(string address)方法從 URL 加載數據。將數據保存到 MemoryStream,然后調用Document.LoadFromStream()方法從 MemoryStream 加載 Word 文檔。
using (MemoryStream ms = new MemoryStream(webClient.DownloadData("http://www.e-iceblue.com/images/test.docx"))) { doc.LoadFromStream(ms,FileFormat.Docx); }
第 4 步:保存文件。
doc.SaveToFile("result.docx",FileFormat.Docx);
運行程序,目標文件將被下載并保存為 Bin 文件夾中的新 Word 文件。
完整代碼:
[C#]
using Spire.Doc; using System.IO; using System.Net; namespace DownloadfromURL { class Program { static void Main(string[] args) { Document doc = new Document(); WebClient webClient = new WebClient(); using (MemoryStream ms = new MemoryStream(webClient.DownloadData("http://www.e-iceblue.com/images/test.docx"))) { doc.LoadFromStream(ms, FileFormat.Docx); } doc.SaveToFile("result.docx", FileFormat.Docx); } } }
[VB.NET]
Imports Spire.Doc Imports System.IO Imports System.Net Namespace DownloadfromURL Class Program Private Shared Sub Main(args As String()) Dim doc As New Document() Dim webClient As New WebClient() Using ms As New MemoryStream(webClient.DownloadData("http://www.e-iceblue.com/images/test.docx")) doc.LoadFromStream(ms, FileFormat.Docx) End Using doc.SaveToFile("result.docx", FileFormat.Docx) End Sub End Class End Namespace
歡迎下載|體驗更多E-iceblue產品
如需獲取更多產品相關信息請咨詢
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn