通过IFilePathProvider接口提供导出工作表HTML文件路径
Contents
[
Hide
]
可能的使用场景
假设,您有一个包含多个工作表的 Excel 文件,并且您想要将每个工作表导出到单独的 HTML 文件。如果您的任何工作表有指向其他工作表的链接,那么这些链接将在导出的 HTML 中断开。为了解决这个问题,Aspose.Cells 提供IFilePathProvider您可以实现该接口来修复损坏的链接。
通过IFilePathProvider接口提供导出工作表HTML文件路径
请下载示例 excel 文件在以下代码及其导出的 HTML 文件中使用。所有这些文件都在温度目录。你应该提取它C:驾驶。然后就会变成C:\温度目录。然后你会打开Sheet1.html在浏览器中打开文件,然后单击其中的两个链接。这些链接指的是这两个导出的 HTML 工作表,它们位于C:\Temp\OtherSheets目录。
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:\温度目录仅用于说明目的。您可以使用您选择和放置的任何目录示例 excel 文件在那里并执行提供的示例代码。然后它将创建其他床单您的目录中的子目录,并在其中导出第二个和第三个工作表 HTML。请更改目录路径在提供的代码中添加变量,并在执行前将其引用到您选择的目录。
示例代码仅在您设置 Aspose.Cells 许可证时才有效。如果您尝试在未设置许可证的情况下运行代码,它将进入无限循环。因此,我们添加了一个检查以在未设置许可证时打印消息并停止执行。您可以购买许可证或向 Aspose.Purchase 团队申请 30 天的临时许可证。
请参阅在代码中注释这些行将破坏链接Sheet1.html和Sheet2.html要么Sheet3.html当他们的链接在里面被点击时不会打开Sheet1.html
//If you will comment this line, then hyperlinks will be broken
options.setFilePathProvider(new FilePathProvider());
这是您可以使用提供的执行的完整示例代码示例 excel 文件.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(OpeningFilesThroughPath.class); | |
// Opening from path. Creating an Workbook object with an Excel file path | |
Workbook workbook1 = new Workbook(dataDir + "Book1.xlsx"); | |
// Print message | |
System.out.println("Workbook opened using path successfully."); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
public class 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) { | |
String dataDir = Utils.getDataDir(FilePathProvider.class); | |
if ("Sheet2".equals(sheetName)) { | |
return dataDir + "Sheet2.html"; | |
} else if ("Sheet3".equals(sheetName)) { | |
return dataDir + "Sheet3.html"; | |
} | |
return ""; | |
} | |
} |