通过IFilePathProvider接口提供导出的工作表html文件路径

可能的使用场景

假设您有一个包含多个工作表的excel文件,并且您想要将每个工作表导出为单独的HTML文件。如果您的任何工作表具有指向其他工作表的链接,则这些链接将在导出的HTML中断开。为解决此问题,Aspose.Cells提供了IFilePathProvider接口,您可以实现该接口以修复这些中断的链接。

通过IFilePathProvider接口提供导出的工作表HTML文件路径

请下载以下代码中使用的示例excel文件及其导出的HTML文件。所有这些文件都在Temp目录中。您应该将其提取到C:驱动器中。然后它将变为C:\Temp目录。然后您将在浏览器中打开Sheet1.html文件,并单击其中的两个链接。这些链接指向C:\Temp\OtherSheets目录中的这两个导出的HTML工作表。

 file:///C:/Temp/OtherSheets/Sheet2.html#RANGE!A1

file:///C:/Temp/OtherSheets/Sheet3.html#RANGE!A1

以下截图显示了C:\Temp\Sheet1.html及其链接的外观

todo:image_alt_text

以下截图显示了HTML源代码。如您所见,现在链接指向C:\Temp\OtherSheets目录。通过使用IFilePathProvider接口实现了这一点。

todo:image_alt_text

示例代码

请注意C:\Temp目录仅用于举例。您可以使用任何选择的目录,并将sample excel文件放入其中,并执行提供的示例代码。然后,它将在您的目录中创建OtherSheets子目录,并在其中导出第二和第三个工作表的HTML。在执行之前,请更改提供的代码中的dirPath变量并将其指向您选择的目录。

请注意,注释掉代码中的这些行将会导致Sheet1.html中的链接断开,Sheet2.html或Sheet3.html将无法在其中单击链接打开

以下是您可以通过提供的示例excel文件 执行的完整样本代码。

// 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 "";
}
}