Proporcione la ruta del archivo html de la hoja de trabajo exportada a través de la interfaz IFilePathProvider
Posibles escenarios de uso
Supongamos que tiene un archivo de Excel con varias hojas y desea exportar cada hoja a un archivo HTML individual. Si alguna de sus hojas tiene enlaces a otras hojas, esos enlaces se romperán en el HTML exportado. Para solucionar este problema, Aspose.Cells proporcionaIFilePathProviderinterfaz que puede implementar para reparar los enlaces rotos.
Proporcione la hoja de trabajo exportada HTML ruta del archivo a través de la interfaz IFilePathProvider
Por favor descarga elejemplo de archivo de Excelutilizado en el código siguiente y sus archivos HTML exportados. Todos estos archivos están dentro del directorio Temp. Debes extraerlo en la unidad C:. Entonces se convertirá en el directorio C:\Temp. Luego, abrirá el archivo Sheet1.html en el navegador y hará clic en los dos enlaces que contiene. Estos enlaces hacen referencia a estas dos hojas de trabajo HTML exportadas que se encuentran dentro del directorio C:\Temp\OtherSheets.
file:///C:/Temp/OtherSheets/Sheet2.html#RANGE!A1
file:///C:/Temp/OtherSheets/Sheet3.html#RANGE!A1
La siguiente captura de pantalla muestra cómo se ven C:\Temp\Sheet1.html y sus enlaces
La siguiente captura de pantalla muestra la fuente HTML. Como puede ver, los enlaces ahora se refieren al directorio C:\Temp\OtherSheets. Esto se logró utilizando elIFilePathProviderinterfaz.
Código de muestra
Tenga en cuenta que el directorio C:\Temp es solo para fines ilustrativos. Puede utilizar cualquier directorio de su elección y lugarejemplo de archivo de Exceldentro de allí y ejecute el código de muestra provisto. Luego creará el subdirectorio OtherSheets dentro de su directorio y exportará la segunda y tercera hoja de trabajo HTML dentro de él. Cambie la variable dirPath dentro del código provisto y consúltelo con el directorio de su elección antes de la ejecución.
Consulte si comenta estas líneas dentro del código, se romperán los enlaces en Sheet1.html y Sheet2.html o Sheet3.html no se abrirán cuando se haga clic en sus enlaces dentro de 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(); |
Aquí está el código de muestra completo que se puede ejecutar con el proporcionadoejemplo de archivo de 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 ""; | |
} | |
} |