Fornire il percorso del file HTML del foglio di lavoro esportato tramite l interfaccia IFilePathProvider

Possibili Scenari di Utilizzo

Supponiamo che tu abbia un file di Excel con più fogli e vuoi esportare ogni foglio in un file HTML individuale. Se alcuni dei tuoi fogli hanno collegamenti ad altri fogli, allora quei collegamenti saranno interrotti nell’HTML esportato. Per affrontare questo problema, Aspose.Cells fornisce l’interfaccia IFilePathProvider che è possibile implementare per correggere i collegamenti interrotti.

Fornire il percorso del file HTML del foglio di lavoro esportato tramite l’interfaccia IFilePathProvider

Si prega di scaricare il file di Excel di esempio utilizzato nel codice seguente e i relativi file HTML esportati. Tutti questi file sono all’interno della directory Temp. Dovresti estrarli sull’unità C:. Quindi diventerà la directory C:\Temp. Quindi aprirai il file Sheet1.html nel browser e cliccherai sui due collegamenti al suo interno. Questi collegamenti si riferiscono ai due fogli di lavoro HTML esportati che si trovano nella directory C:\Temp\OtherSheets.

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

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

Lo screenshot seguente mostra come appaiono il C:\Temp\Sheet1.html e i suoi collegamenti

todo:image_alt_text

Lo screenshot seguente mostra il sorgente HTML. Come si può vedere, i collegamenti ora si riferiscono alla directory C:\Temp\OtherSheets. Questo è stato ottenuto utilizzando l’interfaccia IFilePathProvider.

todo:image_alt_text

Codice di Esempio

Si noti che la directory C:\Temp è solo a scopo illustrativo. È possibile utilizzare qualsiasi directory a scelta personale e inserire il file di Excel di esempio all’interno e eseguire il codice di esempio fornito. Creerà quindi la sotto-directory OtherSheets nella directory selezionata ed esporterà i fogli di lavoro HTML del secondo e del terzo foglio di lavoro al suo interno. Si prega di modificare la variabile dirPath all’interno del codice fornito e farvi riferimento alla directory a 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 verranno cliccati i relativi collegamenti all’interno di Sheet1.html

Di seguito è riportato il codice di esempio completo che è possibile eseguire con il file di Excel di esempio fornito.

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