Farklı Biçimlerdeki Dosyaları Açma
Aspose.Cells’i kullanarak farklı formatlardaki dosyaları açabilirsiniz.Aspose.Cells gibi bir dizi dosya formatını açabilir.
Desteklenen tüm dosya biçimlerini bilmeniz gerekiyorsa, lütfen aşağıdaki sayfalara bakın: Desteklenen Dosya Biçimleri
Farklı Biçimlerdeki Dosyaları Açma
Aspose.Cells, geliştiricilerin SpreadsheetML, Virgülle ayrılmış değerler (CSV), Sekmeyle Ayrılmış veya Sekmeyle ayrılmış değerler (TSV), ODS dosyaları gibi farklı biçimlerdeki elektronik tablo dosyalarını açmasına olanak tanır. Bu tür dosyaları açmak için geliştiriciler, farklı Microsoft Excel sürümlerindeki dosyaları açarken kullandıkları metodolojinin aynısını kullanabilirler.
SpreadsheetML Dosyalarını Açma
SpreadsheetML dosyaları, biçimlendirme, formüller vb. hakkında tüm bilgileri içeren elektronik tabloların XML temsilleridir. Microsoft Excel XP’den beri, Microsoft Excel’e elektronik tablolarınızı SpreadsheetML dosyalarına aktaran bir XML dışa aktarma seçeneği eklenmiştir.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Opening SpreadsheetML Files | |
// Instantiate LoadOptions specified by the LoadFormat. | |
LoadOptions loadOptions3 = new LoadOptions(LoadFormat.SpreadsheetML); | |
// Create a Workbook object and opening the file from its path | |
Workbook wbSpreadSheetML = new Workbook(dataDir + "Book3.xml", loadOptions3); | |
Console.WriteLine("SpreadSheetML file opened successfully!"); |
HTML Dosyalarını Açma
Aspose.Cells, HTML dosyasını Workbook nesnesine açmanıza izin verir. HTML dosyası Microsoft Excel odaklı olmalıdır yani MS-Excel açabilmelidir.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
string filePath = dataDir + "Book1.html"; | |
// Instantiate LoadOptions specified by the LoadFormat. | |
HtmlLoadOptions loadOptions = new HtmlLoadOptions(LoadFormat.Html); | |
// Create a Workbook object and opening the file from its path | |
Workbook wb = new Workbook(filePath, loadOptions); | |
// Save the MHT file | |
wb.Save(filePath + "output.xlsx"); |
CSV Dosyalarını Açma
Virgülle Ayrılmış Değerler (CSV) dosyaları, değerlerin virgülle ayrıldığı kayıtları içerir. Veriler, her sütunun virgül karakteriyle ayrıldığı ve çift tırnak karakteriyle alıntılandığı bir tablo olarak saklanır. Bir alan değeri bir çift tırnak karakteri içeriyorsa, bir çift çift tırnak karakteri ile çıkış yapılır. Elektronik tablo verilerini CSV’e aktarmak için Microsoft Excel’i de kullanabilirsiniz.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiate LoadOptions specified by the LoadFormat. | |
LoadOptions loadOptions4 = new LoadOptions(LoadFormat.Csv); | |
// Create a Workbook object and opening the file from its path | |
Workbook wbCSV = new Workbook(dataDir + "Book_CSV.csv", loadOptions4); | |
Console.WriteLine("CSV file opened successfully!"); |
CSV dosyalarının açılması ve geçersiz karakterlerin değiştirilmesi
Excel’de özel karakterler içeren CSV dosyası açıldığında karakterler otomatik olarak değiştirilir. Aynısı, aşağıda verilen kod örneğinde gösterilen Aspose.Cells API tarafından yapılır.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
var filename = sourceDir + "[20180220142533][ASPOSE_CELLS_TEST].csv"; | |
//Load CSV file | |
var workbook = new Workbook(filename, new TxtLoadOptions() { Separator = ';', LoadFilter = new LoadFilter(LoadDataFilterOptions.CellData), CheckExcelRestriction = false, ConvertNumericData = false, ConvertDateTimeData = false }); | |
Console.WriteLine(workbook.Worksheets[0].Name); // (20180220142533)(ASPOSE_CELLS_T | |
Console.WriteLine(workbook.Worksheets[0].Name.Length); // 31 | |
Console.WriteLine("CSV file opened successfully!"); |
Tercih edilen ayrıştırıcıyı kullanma
Bu, CSV dosyalarını açmak için varsayılan ayrıştırıcı ayarlarını kullanmak için her zaman gerekli değildir. Bazen CSV dosyasının içe aktarılması beklenen çıktıyı oluşturmaz, örneğin tarih biçimi beklendiği gibi değildir veya boş alanlar farklı şekilde işlenir. Bu amaç içinTxtLoadOptions.PreferredParsersgereksinime göre farklı veri türlerini ayrıştırmak için kendi tercih edilen ayrıştırıcıyı sağlamak için kullanılabilir. Aşağıdaki örnek kod, tercih edilen ayrıştırıcının kullanımını gösterir.
Bu özelliği test etmek için örnek kaynak dosya ve çıktı dosyaları aşağıdaki bağlantılardan indirilebilir.
outputsamplePreferredParser.xlsx
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
class TextParser : ICustomParser | |
{ | |
public object ParseObject(string value) | |
{ | |
return value; | |
} | |
public string GetFormat() | |
{ | |
return ""; | |
} | |
} | |
class DateParser : ICustomParser | |
{ | |
public object ParseObject(string value) | |
{ | |
DateTime myDate = DateTime.ParseExact(value, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture); | |
return myDate; | |
} | |
public string GetFormat() | |
{ | |
return "dd/MM/yyyy"; | |
} | |
} | |
public static void Main() | |
{ | |
// Initialize Text File's LoadFormat | |
LoadFormat oLoadFormat = LoadFormat.Csv; | |
// Initialize Text File's Load options | |
TxtLoadOptions oTxtLoadOptions = new TxtLoadOptions(oLoadFormat); | |
// Specify the separatot character | |
oTxtLoadOptions.Separator = Convert.ToChar(","); | |
// Specify the encoding scheme | |
oTxtLoadOptions.Encoding = System.Text.Encoding.UTF8; | |
// Set the flag to true for converting datetime data | |
oTxtLoadOptions.ConvertDateTimeData = true; | |
// Set the preferred parsers | |
oTxtLoadOptions.PreferredParsers = new ICustomParser[] { new TextParser(), new DateParser() }; | |
// Initialize the workbook object by passing CSV file and text load options | |
Workbook oExcelWorkBook = new Aspose.Cells.Workbook(sourceDir + "samplePreferredParser.csv", oTxtLoadOptions); | |
// Get the first cell | |
Cell oCell = oExcelWorkBook.Worksheets[0].Cells["A1"]; | |
// Display type of value | |
Console.WriteLine("A1: " + oCell.Type.ToString() + " - " + oCell.DisplayStringValue); | |
// Get the second cell | |
oCell = oExcelWorkBook.Worksheets[0].Cells["B1"]; | |
// Display type of value | |
Console.WriteLine("B1: " + oCell.Type.ToString() + " - " + oCell.DisplayStringValue); | |
// Save the workbook to disc | |
oExcelWorkBook.Save(outputDir + "outputsamplePreferredParser.xlsx"); | |
Console.WriteLine("OpeningCSVFilesWithPreferredParser executed successfully.\r\n"); | |
} |
Metin Dosyalarını Özel Ayırıcıyla Açma
Metin dosyaları, elektronik tablo verilerini biçimlendirmeden tutmak için kullanılır. Dosya, bazı özelleştirilmiş sınırlayıcılara sahip olabilen bir tür düz metin dosyasıdır.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
string filePath = dataDir + "Book11.csv"; | |
// Instantiate Text File's LoadOptions | |
TxtLoadOptions txtLoadOptions = new TxtLoadOptions(); | |
// Specify the separator | |
txtLoadOptions.Separator = Convert.ToChar(","); | |
// Specify the encoding type | |
txtLoadOptions.Encoding = System.Text.Encoding.UTF8; | |
// Create a Workbook object and opening the file from its path | |
Workbook wb = new Workbook(filePath, txtLoadOptions); | |
// Save file | |
wb.Save(dataDir+ "output.txt"); |
Sekmeyle Ayrılmış Dosyaları Açma
Sekmeyle ayrılmış (Metin) dosyası, elektronik tablo verilerini içerir, ancak herhangi bir biçimlendirme içermez. Veriler, tablolarda ve elektronik tablolarda olduğu gibi satırlar ve sütunlar halinde düzenlenir. Temel olarak, sekmeyle ayrılmış bir dosya, her sütun arasında bir sekme bulunan özel bir tür düz metin dosyasıdır.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Opening Tab Delimited Files | |
// Instantiate LoadOptions specified by the LoadFormat. | |
LoadOptions loadOptions5 = new LoadOptions(LoadFormat.TabDelimited); | |
// Create a Workbook object and opening the file from its path | |
Workbook wbTabDelimited = new Workbook(dataDir + "Book1TabDelimited.txt", loadOptions5); | |
Console.WriteLine("Tab delimited file opened successfully!"); |
Sekmeyle Ayrılmış Değerler (TSV) Dosyalarını Açma
Sekmeyle ayrılmış değerler (TSV) dosyası, herhangi bir biçimlendirme olmadan elektronik tablo verileri içerir. Tablolarda ve elektronik tablolarda olduğu gibi verilerin satırlar ve sütunlar halinde düzenlendiği Sekmeyle Ayrılmış dosya ile aynıdır.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
// Instantiate LoadOptions specified by the LoadFormat. | |
LoadOptions loadOptions = new LoadOptions(LoadFormat.Tsv); | |
// Create a Workbook object and opening the file from its path | |
Workbook workbook = new Workbook(sourceDir + "SampleTSVFile.tsv", loadOptions); | |
// Using the Sheet 1 in Workbook | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Accessing a cell using its name | |
Cell cell = worksheet.Cells["C3"]; | |
Console.WriteLine("Cell Name: " + cell.Name + " Value: " + cell.StringValue); | |
SXC Dosyalarını Açma
StarOffice Calc, Microsoft Excel’e benzer ve formülleri, çizelgeleri, işlevleri ve makroları destekler. Bu yazılımla oluşturulan elektronik tablolar SXC uzantısıyla kaydedilir. SXC dosyası, OpenOffice.org Calc elektronik tablo dosyaları için de kullanılır. Aspose.Cells, aşağıdaki kod örneğinde gösterildiği gibi SXC dosyalarını okuyabilir.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
// Instantiate LoadOptions specified by the LoadFormat. | |
LoadOptions loadOptions = new LoadOptions(LoadFormat.Sxc); | |
// Create a Workbook object and opening the file from its path | |
Workbook workbook = new Workbook(sourceDir + "SampleSXC.sxc", loadOptions); | |
// Using the Sheet 1 in Workbook | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Accessing a cell using its name | |
Cell cell = worksheet.Cells["C3"]; | |
Console.WriteLine("Cell Name: " + cell.Name + " Value: " + cell.StringValue); | |
FODS Dosyalarını Açma
FODS dosyası, herhangi bir sıkıştırma olmadan OpenDocument XML’de kaydedilmiş bir elektronik tablodur. Aspose.Cells, aşağıdaki kod örneğinde gösterildiği gibi FODS dosyalarını okuyabilir.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
// Instantiate LoadOptions specified by the LoadFormat. | |
LoadOptions loadOptions = new LoadOptions(LoadFormat.Fods); | |
// Create a Workbook object and opening the file from its path | |
Workbook workbook = new Workbook(sourceDir + "SampleFods.fods", loadOptions); | |
Console.WriteLine("FODS file opened successfully!"); | |