Provide exported worksheet html file path via IFilePathProvider interface
Possible Usage Scenarios
Suppose, you have an excel file with multiples sheets and you want to export each sheet to individual HTML file. If any of your sheets have links to other sheets, then those links will be broken in the exported HTML. To deal with this problem, Aspose.Cells provides IFilePathProvider interface which you can implement to fix the broken links.
Provide exported worksheet HTML file path via IFilePathProvider interface
Please download the sample excel file used in the following code and its exported HTML files. All these files are inside the Temp directory. You should extract it on C: drive. Then it will become C:\Temp directory. Then you will open the Sheet1.html file in the browser and click the two links inside it. These links refer to these two exported HTML worksheets which are inside the C:\Temp\OtherSheets directory.
file:///C:/Temp/OtherSheets/Sheet2.html#RANGE!A1
file:///C:/Temp/OtherSheets/Sheet3.html#RANGE!A1
The following screenshot shows how the C:\Temp\Sheet1.html and its links look like
The following screenshot shows the HTML source. As you can see that the links are now referring to C:\Temp\OtherSheets directory. This was achieved using the IFilePathProvider interface.
Sample Code
Please note C:\Temp directory is just for illustration purpose. You can use any directory of your choice and place sample excel file inside there and execute the provided sample code. It will then create OtherSheets sub-directory inside your directory and export second and third worksheets HTML inside it. Please change the dirPath variable inside the provided code and refer it to the directory of your choice before execution.
Please see commenting these lines inside the code will break the links in Sheet1.html and Sheet2.html or Sheet3.html will not open up when their links will be clicked inside the Sheet1.html
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// If you will comment this line, then hyperlinks will be broken | |
options.FilePathProvider = new FilePathProvider(); |
Here is the complete sample code which you can be executed with the provided sample excel file.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
public class ExportedWorkSheetViaIFilePathProvider | |
{ | |
// This is the directory path which contains the sample.xlsx file | |
static string dirPath = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
static void Main(string[] args) | |
{ | |
// because Aspose.Cells will always make the warning worksheet as active sheet in Evaluation mode. | |
SetLicense(); | |
// Check if license is set, otherwise do not proceed | |
Workbook wb = new Workbook(); | |
if (wb.IsLicensed == false) | |
{ | |
Console.WriteLine("You must set the license to execute this code successfully."); | |
Console.ReadKey(); | |
} | |
else | |
{ | |
// Test IFilePathProvider interface | |
TestFilePathProvider(); | |
} | |
} | |
static void SetLicense() | |
{ | |
string licPath = @"Aspose.Cells.lic"; | |
Aspose.Cells.License lic = new Aspose.Cells.License(); | |
lic.SetLicense(licPath); | |
Console.WriteLine(CellsHelper.GetVersion()); | |
System.Diagnostics.Debug.WriteLine(CellsHelper.GetVersion()); | |
Environment.CurrentDirectory = dirPath; | |
} | |
static void TestFilePathProvider() | |
{ | |
// Create subdirectory for second and third worksheets | |
Directory.CreateDirectory(dirPath + "OtherSheets"); | |
// Load sample workbook from your directory | |
Workbook wb = new Workbook(dirPath + "Sample.xlsx"); | |
// Save worksheets to separate html files | |
// Because of IFilePathProvider, hyperlinks will not be broken. | |
for (int i = 0; i < wb.Worksheets.Count; i++) | |
{ | |
// Set the active worksheet to current value of variable i | |
wb.Worksheets.ActiveSheetIndex = i; | |
// Creat html save option | |
HtmlSaveOptions options = new HtmlSaveOptions(); | |
options.ExportActiveWorksheetOnly = true; | |
// If you will comment this line, then hyperlinks will be broken | |
options.FilePathProvider = new FilePathProvider(); | |
// Sheet actual index which starts from 1 not from 0 | |
int sheetIndex = i + 1; | |
string filePath = ""; | |
// Save first sheet to same directory and second and third worksheets to subdirectory | |
if (i == 0) | |
{ | |
filePath = dirPath + "Sheet1.html"; | |
} | |
else | |
{ | |
filePath = dirPath + "OtherSheets\\Sheet" + sheetIndex + "_out.html"; | |
} | |
// Save the worksheet to html file | |
wb.Save(filePath, options); | |
} | |
} | |
} | |
// Implementation of IFilePathProvider interface | |
public class FilePathProvider : IFilePathProvider | |
{ | |
// Constructor | |
public FilePathProvider() | |
{ | |
} | |
// Gets the full path of the file by worksheet name when exporting worksheet to html separately. | |
// So the references among the worksheets could be exported correctly. | |
public string GetFullName(string sheetName) | |
{ | |
if ("Sheet2".Equals(sheetName)) | |
{ | |
return @"file:///" + "OtherSheets\\Sheet2.html"; | |
} | |
else if ("Sheet3".Equals(sheetName)) | |
{ | |
return @"file:///" + "OtherSheets\\Sheet3.html"; | |
} | |
return ""; | |
} | |
} |