管理Excel文件的数据
如何向单元格添加数据
Aspose.Cells提供了一个表示Microsoft Excel文件的 Workbook 类。 Workbook 类包含一个 Worksheets 集合,允许访问Excel文件中的每个工作表。工作表由 Worksheet 类表示。 Worksheet 类提供一个 Cells 集合。 Cells 集合中的每个项目都表示 Cell 类的对象。
Aspose.Cells允许开发人员通过调用 Cell 类的 PutValue 方法向工作表中的单元格添加数据。Aspose.Cells提供 PutValue 方法的重载版本,使开发人员可以向单元格添加不同类型的数据。使用 PutValue 方法的这些重载版本,可以将布尔值、字符串、双精度、整数或日期/时间等值添加到单元格。
// 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"); |
如何提高效率
如果您使用PutValue方法向工作表添加大量数据,应先按行然后按列添加值到单元格。这种方法极大地提高了应用程序的效率。
如何从单元格中检索数据
Aspose.Cells提供一个代表Microsoft Excel文件的类Workbook。Workbook类包含一个Worksheets集合,允许访问文件中的工作表。工作表由Worksheet类表示。Worksheet类提供Cells集合。Cells集合中的每个项代表Cell类的对象。
Cell类提供了几个属性,允许开发人员根据其数据类型从单元格中检索值。这些属性包括:
- StringValue:返回单元格的字符串值。
- DoubleValue:返回单元格的双精度值。
- BoolValue:返回单元格的布尔值。
- DateTimeValue:返回单元格的日期/时间值。
- FloatValue:返回单元格的浮点值。
- IntValue:返回单元格的整数值。
当字段未填写时,具有DoubleValue 或FloatValue 的单元格会引发异常。
还可以通过使用Cell 类的Type 属性来检查单元格中包含的数据类型。 实际上,Cell 类的Type 属性基于CellValueType 枚举,其预定义值如下:
单元格值类型 | 描述 |
---|---|
IsBool | 指定单元格值为布尔类型。 |
IsDateTime | 指定单元格值为日期/时间类型。 |
IsNull | 表示空白单元格。 |
IsNumeric | 指定单元格值为数值类型。 |
IsString | 指定单元格值为字符串类型。 |
IsUnknown | 指定单元格值为未知类型。 |
您还可以使用上述预定义的单元格值类型与每个单元格中存在的数据类型进行比较。
// 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; | |
} | |
} |