Charger du HTML dans Excel avec StreamProvider
Lors du chargement de fichiers HTML contenant des ressources externes, nous rencontrons souvent les deux problèmes suivants :
- Lorsque le flux HTML est chargé, les images et les ressources externes référencées par le fichier HTML ne peuvent pas être obtenues via des chemins relatifs.
- Les chemins des ressources externes référencés dans les fichiers HTML doivent être mappés
Cet article explique comment implémenter l’interface IStreamProvider pour définir la propriété HtmlLoadOptions.StreamProvider. En implémentant cette interface, vous pourrez charger des ressources externes lors du chargement de flux HTML ou ces ressources externes seront relatives.
Il s’agit du code principal montrant l’utilisation de la propriété HtmlLoadOptions.StreamProvider
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"); | |
} |