从 .NET 中的工作表导出数据
Contents
[
Hide
]
概述
本文介绍如何使用 C# 将工作表数据导出到 DataTable。它涵盖以下主题
格式: Excel
格式: XLS
格式: XLSX
格式: ODS
C# 导出Excel数据
本文讨论开发者通过Aspose.Cells接触到的一些数据导出技巧。
从工作表导出数据
Aspose.Cells 不仅方便其用户从外部数据源将数据导入工作表,还允许他们将工作表数据导出到数据表.据我们所知数据表是ADO.NET的一部分,用于保存数据。一旦数据存储在一个数据表,可根据用户要求任意使用。开发人员还可以存储此数据(存储在数据表 如果他们愿意,可以直接访问数据库。因此,我们可以看到,如果将工作表数据导出到数据表.
使用 Aspose.Cells 将数据导出到 DataTable
开发人员可以轻松地将他们的工作表数据导出到数据表通过调用对象导出数据表要么ExportDataTableAsString的方法Cells班级。这两种方法用于不同的场景,下面将对此进行更详细的讨论。
包含强类型数据的列
我们知道电子表格将数据存储为一系列行和列。如果工作表列中的所有值都是强类型的(这意味着列中的所有值必须具有相同的数据类型),那么我们可以通过调用导出工作表内容导出数据表的方法Cells班级。导出数据表方法采用以下参数将工作表数据导出为数据表目的:
- 行号, 第一个单元格数据的行号将从中导出。
- 列号,数据将从中导出的第一个单元格的列号。
- 行数,要导出的行数。
- 列数,要导出的列数。
- 导出列名 一个布尔属性,指示是否应将工作表第一行中的数据导出为数据表或不。
步骤:将数据导出到 DataTable
- 脚步: C#中的Excel转DataTable
- 脚步: C#中Excel转DataTable
- 脚步: C#中导入Excel到DataTable
- 脚步:在 C# 中从 Excel 导出到 DataTable
代码步骤:
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 | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
string filePath = dataDir + "Book1.xlsx"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(filePath); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Exporting the contents of 7 rows and 2 columns starting from 1st cell to DataTable | |
DataTable dataTable = worksheet.Cells.ExportDataTable(0, 0, 11, 2, true); | |
foreach (DataRow r in dataTable.Rows) | |
{ | |
foreach (DataColumn c in dataTable.Columns) | |
{ | |
Double value = r.Field<Double>(c); | |
Console.Write(value + " "); | |
} | |
Console.WriteLine(); | |
} | |
包含非强类型数据的列
如果工作表列中的所有值都不是强类型的(这意味着列中的值可能具有不同的数据类型),那么我们可以通过调用导出工作表内容ExportDataTableAsString的方法Cells班级。ExportDataTableAsString方法采用与导出数据表将工作表数据导出为数据表目的。
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 | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
string filePath = dataDir + "Book1.xlsx"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(filePath); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Exporting the contents of 7 rows and 2 columns starting from 1st cell to DataTable | |
DataTable dataTable = worksheet.Cells.ExportDataTableAsString(0, 0, 11, 2, true); | |
foreach (DataRow r in dataTable.Rows) | |
{ | |
foreach (DataColumn c in dataTable.Columns) | |
{ | |
string value = r.Field<string>(c); | |
Console.Write(value + " "); | |
} | |
Console.WriteLine(); | |
} |
导出带有标志的范围以跳过列名
范围内的数据可以导出到数据表其中有一个标志可用于跳过导出数据中的标题行。以下代码将一系列数据导出到数据表有一个论点导出表选项其中包含导出列名旗帜。它被设置为真的如果标题信息存在,因此它不会包含在数据中并设置为错误的如果没有标题,所有行都将被视为数据。
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 | |
// The path to the documents directory. | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(sourceDir + "Names.xlsx"); | |
// Instantiating a WorkbookDesigner object | |
WorkbookDesigner designer = new WorkbookDesigner(workbook); | |
// Accessing the range having name "Names" | |
var range = designer.Workbook.Worksheets.GetRangeByName("Names"); | |
// Instantiating the ExportTableOptions object | |
ExportTableOptions options = new ExportTableOptions(); | |
// Setting the ExportColumnName flag to true shows that first line is header and not part of data | |
options.ExportColumnName = true; | |
// Exporting data with the selected information | |
var dataTable = range.ExportDataTable(options); |