添加和检索数据

添加数据到 Cells

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

Aspose.Cells 允许开发人员通过调用Cell班级'设定值财产。通过使用设定值属性,可以向单元格添加布尔值、字符串、双精度、整数或日期/时间等值。

// 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 根据单元格的数据类型从单元格中获取适当的值。

从 Cells 检索数据

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

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

此外,还可以使用类型的财产Cell班级。事实上,Cell班级'类型财产是基于单元值类型下面列出了预定义值的枚举:

Cell 值类型 描述
IS_BOOL 指定单元格值为布尔值。
是_日期_时间 指定单元格值为日期/时间。
IS_ERROR 表示单元格包含错误值
一片空白 代表一个空白单元格。
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;
}