Word控件Spire.Doc 【文档操作】教程(七):在 C#、VB.NET 中从 URL 下载 Word 文档

如今,我们面临着从 URL 下载文件的更大机会,因为更多的文件是通过互联网以电子方式传递的。在本文中,我将介绍如何在 C#、VB.NET 中使用 Spire.Doc 以编程方式从 URL 下载 Word 文档。

Spire.Doc 不提供直接从 URL 下载 Word 文件的方法。但是,您可以将文件从 URL 下载到 MemoryStream 中,然后使用 Spire.Doc for .NET MemoryStream 加载文档并作为新的 Word 文档保存到本地文件夹。

Spire.Doc for.NET 最新下载

欢迎下载|体验更多E-iceblue产品 技术交流Q群(767755948)  
代码片段:

第 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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值