无需任何格式即可将 Excel 数据导出到 DataTable
Contents
[
Hide
]
有时用户希望将excel数据不带任何格式地导出到数据表中。例如,如果某个单元格的值为 0.012345,并且格式为显示两位小数,则当用户将 excel 数据导出到数据表时,将导出为 0.01 而不是 0.012345。针对这个问题,Aspose.Cells提供了ExportTableOptions.FormatStrategy可以取这三个值之一的属性
- CellValueFormatStrategy.CellStyle
- CellValueFormatStrategy.DisplayStyle
- CellValueFormatStrategy.None
如果你将它设置为CellValueFormatStrategy.None然后它将导出数据而不进行任何格式化。
示例代码
下面的示例解释了使用ExportTableOptions.FormatStrategy属性导出带有和不带有任何格式的 Excel 数据。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()); |
控制台输出
以下是上述示例代码的控制台调试输出
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