使用 StreamProvider 加载 HTML 到 Excel
Contents
[
Hide
]
在加载包含外部资源的html文件时,我们经常会遇到以下两个问题:
- 当加载 HTML 流时,无法通过相对路径获取 HTML 文件引用的图像和外部资源。
- html文件中引用的外部资源路径需要映射
本文介绍了如何实现IStreamProvider接口来设置HtmlLoadOptions.StreamProvider属性。通过实现此接口,您将能够在加载Html流期间加载外部资源,或者这些外部资源是相对的。
下面是显示使用HtmlLoadOptions.StreamProvider属性的主要代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class HtmlAttachedStreamProvider : Aspose.Cells.IStreamProvider | |
{ | |
internal static bool IsHRef(string picPath) | |
{ | |
//This handles http://,https:// file:// and probably ftp://. | |
if (picPath.StartsWith("http://") | |
|| picPath.StartsWith("https://") | |
|| picPath.StartsWith("file://") | |
|| picPath.StartsWith("ftp://")) | |
{ | |
return true; | |
} | |
return false; | |
} | |
internal static Stream GetStreamFromHref(string href) | |
{ | |
try | |
{ | |
WebRequest request = WebRequest.Create(href); | |
WebResponse response = request.GetResponse(); | |
// FIX: Do not specify capacity since the length of the response is sometimes not known. | |
MemoryStream dstStream = new MemoryStream(); | |
using (Stream responseStream = response.GetResponseStream()) | |
{ | |
byte[] buf = new byte[4096]; | |
while (true) | |
{ | |
int bytesRead = responseStream.Read(buf, 0, buf.Length); | |
// Read returns 0 when reached end of stream. Checking for negative too to make it conceptually close to Java. | |
if (bytesRead <= 0) | |
break; | |
else | |
dstStream.Write(buf, 0, bytesRead); | |
} | |
} | |
dstStream.Position = 0; | |
return dstStream; | |
} | |
catch | |
{ | |
//ignored | |
} | |
return null; | |
} | |
public void InitStream(StreamProviderOptions options) | |
{ | |
string absolutePath = null; | |
switch (options.DefaultPath) | |
{ | |
case "/Files/Image1.png": | |
absolutePath = @"D:/filetemp/G1.png"; | |
break; | |
case "/Files/Image2.png": | |
absolutePath = @"D:/filetemp/E1.png"; | |
break; | |
case "https://www.aspose.com/templates/aspose/img/products/cells/aspose_cells-for-net.svg": | |
absolutePath = @"D:/filetemp/F1.png"; | |
break; | |
default: | |
break; | |
} | |
if(absolutePath == null) | |
{ | |
if(IsHRef(options.DefaultPath)) | |
{ | |
options.Stream = GetStreamFromHref(options.DefaultPath); | |
} | |
else if(File.Exists(options.DefaultPath)) | |
{ | |
options.Stream = File.OpenRead(options.DefaultPath); | |
} | |
return; | |
} | |
options.Stream = File.OpenRead(absolutePath); | |
} | |
public void CloseStream(StreamProviderOptions options) | |
{ | |
if(options.Stream != null) | |
{ | |
options.Stream.Close(); | |
} | |
} | |
} | |
static void Main(string[] args) | |
{ | |
HtmlAttachedStreamProvider attachedStreamProvider = new HtmlAttachedStreamProvider(); | |
HtmlLoadOptions options = new HtmlLoadOptions(); | |
options.StreamProvider = attachedStreamProvider; | |
var workbook = new Workbook(@"html1.html", options); | |
workbook.Save("dest.xlsx"); | |
} |