Hi There,
We are using Aspose.Cells (version 2024.5.0) in our .net 7.0 service. We are trying to export an excel filr to html using the following code:
private static HtmlSaveOptions _htmlSaveOptions =>
new()
{
ExportImagesAsBase64 = true,
ExportPrintAreaOnly = true,
ExportHiddenWorksheet = false,
ExcludeUnusedStyles = true,
ExportActiveWorksheetOnly = true,
Encoding = Encoding.UTF8
};
public static void ExportHtml(string inputPath, string htmlOutputPath, string nameRange, string fontFolder = null)
{
if (!string.IsNullOrWhiteSpace(fontFolder) && Directory.Exists(fontFolder))
{
try
{
FontConfigs.SetFontFolder(fontFolder, true);
}
catch (Exception ex)
{
//Log error
}
}
Workbook wbc = null;
using (FileStream ms = new FileStream(inputPath, FileMode.Open))
{
wbc = new Workbook(ms, new LoadOptions(LoadFormat.Xlsx));
}
var worksheet = wbc.Worksheets[0];
var range = wbc.GetRangeByNamedRange(nameRange);
worksheet.PageSetup.PrintArea = range.Address;
wbc.Worksheets.ActiveSheetIndex = worksheet.Index;
using var html = new MemoryStream();
wbc.Save(html, _htmlSaveOptions);
File.WriteAllBytes(htmlOutputPath, html.ToArray());
}
We are using fontFolder since we are running this code in our k8 environment (linux).
The output html we get has bad formatting. (‘output.html’ in zip attached).
When we remove FontConfigs.SetFontFolder(fontFolder, true);
and run the code, the output html works as expected. (‘output-no-fonts.html’).
The ‘input.xlsx’ is the original excel we are trying to export to html.
nameRange is “DR_PUBv2__1698772048”.
fontFolder is attached in zip as well as a zipped folder.
My question is: What are we missing in our fontFolder that we need this export html to work as expected?
Link to download zip file: https://file.io/70FiQ1bchBPk
Please advise,
Shlomi
@shlomi.z,
Thanks for the details and code segment.
I tried to click on the link to download the zip file but I got error page. See the screenshot attached.
sc_shot1.png (18.4 KB)
Please upload the resource files in zipped archive to some file sharing service and share the correct download link, so we could download the zipped archive seamlessly.
I am trying to download the zipped archive from your pasted link but it seems it is taking long time. Is there a better and faster way to get zipped archive? I guess you may share the zipped archive here. If you find issue with file size (limitations) in the forum, you may create multiple zip files for your resource and font files and then attach those zip archives here.
Inputs + outputs:
inputs-outputs.zip (92.3 KB)
Fonts:
Fonts1.zip (8.1 MB)
Fonts2.zip (2.0 MB)
Fonts3.zip (5.0 MB)
@shlomi.z,
Thanks for the template XLSX file and fonts.
After an initial test, I was able to reproduce the issue as you mentioned by using the following sample code (and by specifying your fonts) with your template XLSX file. I found the HTML result has some formatting and other issues. But if I don’t set fonts folders in code, it works as expected.
HtmlSaveOptions _htmlSaveOptions = new HtmlSaveOptions()
{
ExportImagesAsBase64 = true,
ExportPrintAreaOnly = true,
ExportHiddenWorksheet = false,
ExcludeUnusedStyles = true,
ExportActiveWorksheetOnly = true
};
//I created a folder "MyFontFolder" and pasted all three font folders into it.
FontConfigs.SetFontFolder("e:\\test2\\MyFontFolder", true);//comment out this line will give good results.
Workbook wbc = null;
using (FileStream ms = new FileStream("e:\\test2\\Input.xlsx", FileMode.Open))
{
wbc = new Workbook(ms, new LoadOptions(LoadFormat.Xlsx));
}
var worksheet = wbc.Worksheets["Actual Plus Forecast"];
var range = wbc.Worksheets.GetRangeByName("DR_PUBv2__1698772048");
worksheet.PageSetup.PrintArea = range.Address;
wbc.Worksheets.ActiveSheetIndex = worksheet.Index;
using var html = new MemoryStream();
wbc.Save(html, _htmlSaveOptions);
File.WriteAllBytes("e:\\test2\\outputHTML1.html", html.ToArray());
Please find attached the output HTML file.
outputHTML1.zip (8.9 KB)
We require thorough evaluation of the issue. We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): CELLSNET-55793
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
@shlomi.z,
We evaluated your issue further.
I now installed all the fonts (previously I did not install the fonts in the three folders and just set the font folder(s) in code) in the three folders you provided in the zipped archives. Now, it works fine. So, please make sure that you have installed all required fonts in your system. Please note, if the fonts are not installed in the system, the browser will not be able to find the fonts, which may cause differences in displaying into the browser.
Can you please evaluate which fonts are missing from the 3 folders so I’ll know what to add?
@shlomi.z,
It is hard to tell which specific fonts were missing. I simply browsed each fonts folder one by one, I selected all the fonts to install all of them. In short, you may install all the fonts in those three folders and then let us know if you still notice the issue?
Like I mentioned earlier I cannot install fonts in the environment, since it is k8 environment. I have no choice but to use FontConfigs.SetFontFolder(...)
but it seems that the fonts I have (all located in those 3 folders) are not sufficient. So what I am asking from you, is what am I missing?
@shlomi.z
1, Please manually unzip the xlsx file and check xl/style1.xml to find all fonts.
2, Check fonts with Aspose.Cells as the following :
Workbook workbook = new Workbook(dir + "input.xlsx");
foreach(Aspose.Cells.Font f in workbook.GetFonts())
{
Console.WriteLine(f.Name);
}