Herhangi bir Biçimlendirmeye Gerek Duymadan Excel Verilerini DataTable a Aktar
Bazı durumlarda kullanıcılar, herhangi bir biçimlendirmeye gerek duymaksızın excel verilerini bir veri tablosuna aktarmak isteyebilir. Örneğin, bir hücrede değer 0.012345 olarak girilmiş ve bu değer iki ondalık basamaklı olarak biçimlendirilmişse, kullanıcı excel verilerini bir veri tablosuna aktardığında 0.012345 yerine 0.01 olarak aktarılacaktır. Bu sorunla başa çıkmak için Aspose.Cells, bu üç değerden birini alabilen ExportTableOptions.FormatStrategy özelliğini sağlamıştır
- CellValueFormatStrategy.CellStyle
- CellValueFormatStrategy.DisplayStyle
- CellValueFormatStrategy.None
Eğer CellValueFormatStrategy.None olarak ayarlarsanız, o zaman verileri herhangi bir biçimlendirmeye gerek duymadan aktaracaktır
Örnek Kod
Aşağıdaki örnek, excel verilerini ExportTableOptions.FormatStrategy özelliğini kullanarak biçimlendirmesiz olarak bir veri tablosuna aktarmanın nasıl yapılacağını açıklar
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Create workbook | |
Workbook workbook = new Workbook(); | |
// Access first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Access cell A1 | |
Cell cell = worksheet.Cells["A1"]; | |
// Put value inside the cell | |
cell.PutValue(0.012345); | |
// Format the cell that it should display 0.01 instead of 0.012345 | |
Style style = cell.GetStyle(); | |
style.Number = 2; | |
cell.SetStyle(style); | |
// Display the cell values as it displays in excel | |
Console.WriteLine("Cell String Value: " + cell.StringValue); | |
// Display the cell value without any format | |
Console.WriteLine("Cell String Value without Format: " + cell.GetStringValue(CellValueFormatStrategy.None)); | |
// Export Data Table Options with FormatStrategy as CellStyle | |
ExportTableOptions opts = new ExportTableOptions(); | |
opts.ExportAsString = true; | |
opts.FormatStrategy = CellValueFormatStrategy.CellStyle; | |
// Export Data Table | |
DataTable dt = worksheet.Cells.ExportDataTable(0, 0, 1, 1, opts); | |
// Display the value of very first cell | |
Console.WriteLine("Export Data Table with Format Strategy as Cell Style: " + dt.Rows[0][0].ToString()); | |
// Export Data Table Options with FormatStrategy as None | |
opts.FormatStrategy = CellValueFormatStrategy.None; | |
dt = worksheet.Cells.ExportDataTable(0, 0, 1, 1, opts); | |
// Display the value of very first cell | |
Console.WriteLine("Export Data Table with Format Strategy as None: " + dt.Rows[0][0].ToString()); |
Konsol Çıktısı
Yukarıdaki örnek kodun konsol hata ayıklama çıktısı aşağıda yer almaktadır
Cell String Value: 0.01
Cell String Value without Format: 0.012345
Export Data Table with Format Strategy as Cell Style: 0.01
Export Data Table with Format Strategy as None: 0.012345