Fornisci il percorso del file html del foglio di lavoro esportato tramite l'interfaccia IFilePathProvider
Possibili scenari di utilizzo
Supponiamo di avere un file excel con fogli multipli e di voler esportare ogni foglio in un singolo file HTML. Se uno dei tuoi fogli ha collegamenti ad altri fogli, tali collegamenti verranno interrotti nel HTML esportato. Per affrontare questo problema, Aspose.Cells fornisceIFilePathProviderinterfaccia che puoi implementare per correggere i collegamenti interrotti.
Fornire il percorso del file del foglio di lavoro esportato HTML tramite l’interfaccia IFilePathProvider
Si prega di scaricare ilfile excel di esempioutilizzato nel codice seguente e nei suoi file HTML esportati. Tutti questi file si trovano all’interno della directory Temp. Dovresti estrarlo su C: drive. Quindi diventerà la directory C:\Temp. Quindi aprirai il file Sheet1.html nel browser e fai clic sui due collegamenti al suo interno. Questi collegamenti fanno riferimento a questi due fogli di lavoro HTML esportati che si trovano all’interno della directory C:\Temp\OtherSheets.
file:///C:/Temp/OtherSheets/Sheet2.html#RANGE!A1
file:///C:/Temp/OtherSheets/Sheet3.html#RANGE!A1
Lo screenshot seguente mostra l’aspetto di C:\Temp\Sheet1.html e dei relativi collegamenti
Lo screenshot seguente mostra la fonte HTML. Come puoi vedere, i collegamenti ora fanno riferimento alla directory C:\Temp\OtherSheets. Ciò è stato ottenuto utilizzando ilIFilePathProviderinterfaccia.
Codice d’esempio
Si noti che la directory C:\Temp è solo a scopo illustrativo. Puoi utilizzare qualsiasi directory di tua scelta e posizionefile excel di esempiolì dentro ed eseguire il codice di esempio fornito. Creerà quindi la sottodirectory OtherSheets all’interno della directory ed esporterà il secondo e il terzo foglio di lavoro HTML al suo interno. Modificare la variabile dirPath all’interno del codice fornito e riferirla alla directory di propria scelta prima dell’esecuzione.
Si prega di notare che commentare queste righe all’interno del codice interromperà i collegamenti in Sheet1.html e Sheet2.html o Sheet3.html non si apriranno quando i loro collegamenti verranno cliccati all’interno di 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(); |
Ecco il codice di esempio completo che puoi eseguire con il file fornitofile excel di esempio.
// 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 ""; | |
} | |
} |