Stellen Sie den HTML-Dateipfad des exportierten Arbeitsblatts über die IFilePathProvider-Schnittstelle bereit
Mögliche Nutzungsszenarien
Angenommen, Sie haben eine Excel-Datei mit mehreren Blättern und möchten jedes Blatt in eine einzelne HTML-Datei exportieren. Wenn eines Ihrer Blätter Links zu anderen Blättern enthält, werden diese Links im exportierten HTML unterbrochen. Um dieses Problem zu lösen, bietet Aspose.CellsIFilePathProviderSchnittstelle, die Sie implementieren können, um die defekten Links zu reparieren.
Geben Sie den Dateipfad des exportierten Arbeitsblatts HTML über die IFilePathProvider-Schnittstelle an
Bitte laden Sie die herunterExcel-Beispieldateiwird im folgenden Code und seinen exportierten HTML-Dateien verwendet. Alle diese Dateien befinden sich im Temp-Verzeichnis. Sie sollten es auf Laufwerk C: extrahieren. Dann wird es zum C:\Temp-Verzeichnis. Dann öffnen Sie die Datei Sheet1.html im Browser und klicken auf die beiden darin enthaltenen Links. Diese Links verweisen auf diese beiden exportierten HTML-Arbeitsblätter, die sich im Verzeichnis C:\Temp\OtherSheets befinden.
file:///C:/Temp/OtherSheets/Sheet2.html#RANGE!A1
file:///C:/Temp/OtherSheets/Sheet3.html#RANGE!A1
Der folgende Screenshot zeigt, wie die C:\Temp\Sheet1.html und ihre Links aussehen
Der folgende Screenshot zeigt die Quelle HTML. Wie Sie sehen können, verweisen die Links jetzt auf das Verzeichnis C:\Temp\OtherSheets. Dies wurde mit Hilfe von erreichtIFilePathProviderSchnittstelle.
Beispielcode
Bitte beachten Sie, dass das Verzeichnis C:\Temp nur zu Illustrationszwecken dient. Sie können ein beliebiges Verzeichnis Ihrer Wahl und Ihres Ortes verwendenExcel-Beispieldateidarin und führen Sie den bereitgestellten Beispielcode aus. Es erstellt dann das Unterverzeichnis OtherSheets in Ihrem Verzeichnis und exportiert das zweite und dritte Arbeitsblatt HTML darin. Bitte ändern Sie die dirPath-Variable im bereitgestellten Code und verweisen Sie sie vor der Ausführung auf das Verzeichnis Ihrer Wahl.
Bitte beachten Sie, dass das Kommentieren dieser Zeilen im Code die Links in Sheet1.html unterbricht und Sheet2.html oder Sheet3.html nicht geöffnet werden, wenn auf ihre Links in Sheet1.html geklickt wird
// 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(); |
Hier ist der vollständige Beispielcode, den Sie mit dem bereitgestellten ausführen könnenExcel-Beispieldatei.
// 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 ""; | |
} | |
} |