管理Excel文件的数据
如何向单元格添加数据
Aspose.Cells for Node.js via C++提供一个类,Workbook,用于表示一个Microsoft Excel文件。Workbook类包含一个Worksheets集合,可以访问文件中的每个工作表。工作表由Worksheet类表示。Worksheet类提供一个Cells集合,Cells集合中的每个项目代表Cell类的对象。
Aspose.Cells允许开发者通过调用Cell类的putValue方法向工作表中的单元格添加数据。Aspose.Cells提供了putValue方法的重载版本,允许开发者向单元格添加不同类型的数据。使用这些重载版本的putValue方法,可以向单元格添加布尔值、字符串、双精度、整数或日期/时间等值。
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Create directory if it is not already present. | |
const fs = require("fs"); | |
if (!fs.existsSync(dataDir)) { | |
fs.mkdirSync(dataDir); | |
} | |
// Instantiating a Workbook object | |
let workbook = new AsposeCells.Workbook(); | |
// Obtaining the reference of the first worksheet | |
let worksheet = workbook.getWorksheets().get(0); | |
// Adding a string value to the cell | |
worksheet.getCells().get("A1").putValue("Hello World"); | |
// Adding a double value to the cell | |
worksheet.getCells().get("A2").putValue(20.5); | |
// Adding an integer value to the cell | |
worksheet.getCells().get("A3").putValue(15); | |
// Adding a boolean value to the cell | |
worksheet.getCells().get("A4").putValue(true); | |
// Adding a date/time value to the cell | |
worksheet.getCells().get("A5").putValue(new Date()); | |
// Setting the display format of the date | |
let style = worksheet.getCells().get("A5").getStyle(); | |
style.setNumber(15); | |
worksheet.getCells().get("A5").setStyle(style); | |
// Saving the Excel file | |
workbook.save(path.join(dataDir, "output.out.xls")); |
如何提高效率
如果你使用putValue方法将大量数据放入工作表,应该先按行、再按列向单元格添加值。这种方法极大地提高了应用程序的效率。
如何从单元格中检索数据
Aspose.Cells for Node.js via C++提供一个类,Workbook,用于表示一个Microsoft Excel文件。Workbook类包含一个Worksheets集合,可以访问文件中的工作表。工作表由Worksheet类表示。Worksheet类提供一个Cells集合,Cells集合中的每个项目代表Cell类的对象。
Cell类提供了多个属性,允许开发者根据数据类型从单元格中检索值。这些属性包括:
- getStringValue():返回单元格的字符串值。
- getDoubleValue():返回单元格的双精度值。
- getBoolValue():返回单元格的布尔值。
- getDateTimeValue():返回单元格的日期/时间值。
- getFloatValue():返回单元格的浮点值。
- getIntValue():返回单元格的整数值。
当字段未填充时,getDoubleValue()或getFloatValue()的单元格会抛出异常。
还可以通过使用Cell类的getType()方法检查单元格中包含的数据类型。实际上,Cell类的getType()方法基于以下列出的一组预定义值的CellValueType枚举:
单元格值类型 | 描述 |
---|---|
IsBool | 指定单元格值为布尔类型。 |
IsDateTime | 指定单元格值为日期/时间类型。 |
IsNull | 表示空白单元格。 |
IsNumeric | 指定单元格值为数值类型。 |
IsString | 指定单元格值为字符串类型。 |
IsUnknown | 指定单元格值为未知类型。 |
您还可以使用上述预定义的单元格值类型与每个单元格中存在的数据类型进行比较。
const AsposeCells = require("aspose.cells.node"); | |
const path = require("path"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Opening an existing workbook | |
const workbook = new AsposeCells.Workbook("book1.xls"); | |
// Accessing first worksheet | |
const worksheet = workbook.getWorksheets().get(0); | |
var cells = worksheet.getCells(); | |
var maxRow = cells.getMaxRow(); | |
var maxColumn = cells.getMaxColumn(); | |
for (let i = 0; i <= maxRow; i++) { | |
for (let j = 0; j <= maxColumn; j++) | |
{ | |
const cell1 = cells.get(i, j); | |
// Variables to store values of different data types | |
let stringValue; | |
let doubleValue; | |
let boolValue; | |
let dateTimeValue; | |
// Passing the type of the data contained in the cell for evaluation | |
switch (cell1.getType()) { | |
// Evaluating the data type of the cell data for string value | |
case AsposeCells.CellValueType.IsString: | |
stringValue = cell1.getStringValue(); | |
console.log("String Value: " + stringValue); | |
break; | |
// Evaluating the data type of the cell data for double value | |
case AsposeCells.CellValueType.IsNumeric: | |
doubleValue = cell1.getDoubleValue(); | |
console.log("Double Value: " + doubleValue); | |
break; | |
// Evaluating the data type of the cell data for boolean value | |
case AsposeCells.CellValueType.IsBool: | |
boolValue = cell1.getBoolValue(); | |
console.log("Bool Value: " + boolValue); | |
break; | |
// Evaluating the data type of the cell data for date/time value | |
case AsposeCells.CellValueType.IsDateTime: | |
dateTimeValue = cell1.getDateTimeValue(); | |
console.log("DateTime Value: " + dateTimeValue); | |
break; | |
// Evaluating the unknown data type of the cell data | |
case AsposeCells.CellValueType.IsUnknown: | |
stringValue = cell1.getStringValue(); | |
console.log("Unknown Value: " + stringValue); | |
break; | |
// Terminating the type checking of type of the cell data is null | |
case AsposeCells.CellValueType.IsNull: | |
break; | |
} | |
} | |
} |