管理 Excel 文件的数据。

添加数据到 Cells

Aspose.Cells提供了一个类,工作簿,代表一个 Microsoft Excel 文件。这工作簿类包含一个工作表允许访问 Excel 文件中每个工作表的集合。工作表由工作表班级。这工作表类提供了Cells收藏。中的每一项Cells集合代表一个对象Cell班级。

Aspose.Cells 允许开发人员通过调用Cell班级'认沽价值方法。 Aspose.Cells 提供重载版本的认沽价值允许开发人员向单元格添加不同类型数据的方法。使用这些重载版本的认沽价值方法,可以向单元格添加布尔值、字符串、双精度值、整数或日期/时间等值。

// 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);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Adding a string value to the cell
worksheet.Cells["A1"].PutValue("Hello World");
// Adding a double value to the cell
worksheet.Cells["A2"].PutValue(20.5);
// Adding an integer value to the cell
worksheet.Cells["A3"].PutValue(15);
// Adding a boolean value to the cell
worksheet.Cells["A4"].PutValue(true);
// Adding a date/time value to the cell
worksheet.Cells["A5"].PutValue(DateTime.Now);
// Setting the display format of the date
Style style = worksheet.Cells["A5"].GetStyle();
style.Number = 15;
worksheet.Cells["A5"].SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "output.out.xls");

提高效率

如果你使用认沽价值将大量数据放入工作表的方法,您应该向单元格添加值,首先按行,然后按列。这种方法极大地提高了应用程序的效率。

从 Cells 检索数据

Aspose.Cells提供了一个类,工作簿表示 Microsoft Excel 文件。这工作簿类包含一个工作表允许访问文件中工作表的集合。工作表由工作表班级。这工作表类提供了Cells收藏。中的每一项Cells集合代表一个对象Cell班级。

Cell类提供了几个属性,允许开发人员根据单元格的数据类型从单元格中检索值。这些属性包括:

当字段未填充时,单元格双值要么浮动值抛出异常。

单元格中包含的数据类型也可以使用Cell班级'类型财产。事实上,Cell班级'类型财产是基于单元值类型下面列出了预定义值的枚举:

Cell 值类型 描述
布尔值 指定单元格值为布尔值。
是日期时间 指定单元格值为日期/时间。
一片空白 代表一个空白单元格。
是数字 指定单元格值为数字。
字符串 指定单元格值是一个字符串。
未知 指定单元格值未知。

您还可以使用上述预定义的单元格值类型与每个单元格中存在的数据类型进行比较。

// 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 an existing workbook
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing first worksheet
Worksheet worksheet = workbook.Worksheets[0];
foreach (Cell cell1 in worksheet.Cells)
{
// Variables to store values of different data types
string stringValue;
double doubleValue;
bool boolValue;
DateTime dateTimeValue;
// Passing the type of the data contained in the cell for evaluation
switch (cell1.Type)
{
// Evaluating the data type of the cell data for string value
case CellValueType.IsString:
stringValue = cell1.StringValue;
Console.WriteLine("String Value: " + stringValue);
break;
// Evaluating the data type of the cell data for double value
case CellValueType.IsNumeric:
doubleValue = cell1.DoubleValue;
Console.WriteLine("Double Value: " + doubleValue);
break;
// Evaluating the data type of the cell data for boolean value
case CellValueType.IsBool:
boolValue = cell1.BoolValue;
Console.WriteLine("Bool Value: " + boolValue);
break;
// Evaluating the data type of the cell data for date/time value
case CellValueType.IsDateTime:
dateTimeValue = cell1.DateTimeValue;
Console.WriteLine("DateTime Value: " + dateTimeValue);
break;
// Evaluating the unknown data type of the cell data
case CellValueType.IsUnknown:
stringValue = cell1.StringValue;
Console.WriteLine("Unknown Value: " + stringValue);
break;
// Terminating the type checking of type of the cell data is null
case CellValueType.IsNull:
break;
}
}

推进主题