通过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 及其链接的外观

待办事项:图片_替代_文本

以下屏幕截图显示了 HTML 源。如您所见,链接现在指向 C:\Temp\OtherSheets 目录。这是通过使用IFilePathProvider界面。

待办事项:图片_替代_文本

示例代码

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

请参阅在代码中注释这些行将破坏 Sheet1.html 和 Sheet2.html 中的链接,或者在 Sheet1.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 "";
}
}