添加和检索数据

向单元格添加数据

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

Aspose.Cells允许开发人员通过调用Cell类的setValue属性向工作表的单元格添加数据。通过使用setValue属性,可以向单元格添加布尔值、字符串、双精度型、整型或日期/时间等值。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AddingDataToCells.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
com.aspose.cells.Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
com.aspose.cells.Cells cells = worksheet.getCells();
// Adding a string value to the cell
com.aspose.cells.Cell cell = cells.get("A1");
cell.setValue("Hello World");
// Adding a double value to the cell
cell = cells.get("A2");
cell.setValue(20.5);
// Adding an integer value to the cell
cell = cells.get("A3");
cell.setValue(15);
// Adding a boolean value to the cell
cell = cells.get("A4");
cell.setValue(true);
// Adding a date/time value to the cell
cell = cells.get("A5");
cell.setValue(java.util.Calendar.getInstance());
// Setting the display format of the date
com.aspose.cells.Style style = cell.getStyle();
style.setNumber(15);
cell.setStyle(style);
// Saving the Excel file
workbook.save(dataDir + "AddingDataToCells_out.xls");
// Print message
System.out.println("Data Added Successfully");

提高效率

在处理工作表时,用户可能会在单元格中添加不同类型的数据。这些数据项可以包括布尔值、整数、浮点数、文本或日期/时间值。您可以使用Aspose.Cells根据其数据类型从单元格中获取适当的值。

从单元格检索数据

Aspose.Cells提供了一个代表Excel文件的Workbook类。 Workbook类包含一个WorksheetCollection,允许访问Excel文件中的每个工作表。工作表由Worksheet类表示。 Worksheet类提供Cells集合。 Cells集合每个项表示Cell类的一个对象。

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

此外,还可以使用Cell类的Type属性来检查单元格中包含的数据类型。事实上,Cell类的Type属性基于CellValueType枚举,其预定义的值如下所示:

单元格值类型 描述
IS_BOOL 表示单元格值为布尔类型。
IS_DATE_TIME 表示单元格值为日期/时间类型。
IS_ERROR 表示单元格包含错误值。
IS_NULL 表示空白单元格。
IS_NUMERIC 表示单元格值为数值类型。
IS_STRING 表示单元格值为字符串类型。
IS_UNKNOWN 表示单元格值为未知类型。
您还可以使用上述预定义的单元格值类型与每个单元格中的数据类型进行比较。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(RetrievingDataFromCells.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the worksheet
com.aspose.cells.Worksheet worksheet = workbook.getWorksheets().get(0);
com.aspose.cells.Cells cells = worksheet.getCells();
// get cell from cells collection
com.aspose.cells.Cell cell = cells.get("A5");
switch (cell.getType()) {
case com.aspose.cells.CellValueType.IS_BOOL:
System.out.println("Boolean Value: " + cell.getValue());
break;
case com.aspose.cells.CellValueType.IS_DATE_TIME:
System.out.println("Date Value: " + cell.getValue());
break;
case com.aspose.cells.CellValueType.IS_NUMERIC:
System.out.println("Numeric Value: " + cell.getValue());
break;
case com.aspose.cells.CellValueType.IS_STRING:
System.out.println("String Value: " + cell.getValue());
break;
case com.aspose.cells.CellValueType.IS_NULL:
System.out.println("Null Value");
break;
}