IFilePathProvider arabirimini kullanarak dışa aktarılan çalışma sayfasının HTML dosya yolunu sağlayın
Olası Kullanım Senaryoları
Örneğin, her bir sayfanın ayrı bir HTML dosyasına dışa aktarılmak istediği bir Excel dosyanız var. Herhangi bir sayfanızın diğer sayfalara bağlantıları varsa, o bağlantılar dışa aktarılan HTML’de bozulur. Bu sorunu çözmek için, Aspose.Cells IFilePathProvider arayüzünü sağlar, bu arayüzü uygulayarak bozulmuş bağlantıları düzeltebilirsiniz.
IFilePathProvider arayüzü aracılığıyla dışa aktarılan çalışma sayfası HTML dosya yolunu sağlayın
Aşağıdaki kodda kullanılan örnek excel dosyasını ve dışa aktarılan HTML dosyalarını indirin. Tüm bu dosyalar Temp dizini içindedir. Onu C: sürücüsüne çıkarmanız gerekmektedir. Daha sonra C:\Temp dizini haline gelecektir. Ardından tarayıcıda Sheet1.html dosyasını açacak ve içindeki iki bağlantıya tıklayacaksınız. Bu bağlantılar, C:\Temp\OtherSheets dizini içerisinde bulunan iki dışa aktarılmış HTML çalışma sayfasına işaret eder.
file:///C:/Temp/OtherSheets/Sheet2.html#RANGE!A1
file:///C:/Temp/OtherSheets/Sheet3.html#RANGE!A1
Aşağıdaki ekran görüntüsü, C:\Temp\Sheet1.html ve bağlantılarının nasıl göründüğünü göstermektedir
Aşağıdaki ekran görüntüsü, HTML kaynağını göstermektedir. Bağlantıların şimdi C:\Temp\OtherSheets dizinine işaret ettiğini görebilirsiniz. Bu, IFilePathProvider arayüzünü kullanarak başarılmıştır.
Örnek Kod
Lütfen unutmayın, C:\Temp dizini sadece gösterim amaçlıdır. Kendi seçtiğiniz herhangi bir dizini kullanabilir ve örnek excel dosyasını içeriye yerleştirebilir ve sağlanan örnek kodu çalıştırabilirsiniz. Ardından, kendi dizininiz içerisine OtherSheets alt dizinini oluşturacak ve ikinci ve üçüncü sayfa HTML’lerini içerisine dışa aktaracaktır. Lütfen kod içerisinde dirPath değişkenini değiştirerek ve çalıştırmadan önce kendi seçtiğiniz dizine yönlendirdiğinizden emin olun.
Lütfen bu satırların kod içerisinde yorum satırı olarak işaretlenmesi, Sheet1.html içindeki bağlantıları bozacaktır ve Sheet2.html veya Sheet3.html, içindeki bağlantılara tıklandığında açılmayacaktır.
// 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(); |
Aşağıdaki, örnek excel dosyası ile birlikte çalıştırabileceğiniz tam örnek kod bulunmaktadır.
// 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 ""; | |
} | |
} |