Excelファイルのデータ管理
セルにデータを追加する方法
Aspose.Cells for Node.js via C++は、Microsoft Excelファイルを表すクラスWorkbookを提供します。Workbookクラスには、Excelファイル内の各ワークシートにアクセス可能なWorksheetsコレクションが含まれます。ワークシートはWorksheetクラスで表されます。WorksheetクラスはCellsコレクションを提供し、そのCellsコレクションの各アイテムはCellクラスのオブジェクトを表します。
Aspose.Cellsでは、CellクラスのputValueメソッドを呼び出すことで、ワークシートのセルにデータを追加可能です。Aspose.Cellsは、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")); |
効率を向上させる方法
大量のデータをシートに配置する場合は、まず行ごとに値を追加し、その後列に追加すると効率が大幅に向上します。
セルからデータを取得する方法
Aspose.Cells for Node.js via C++は、Microsoft Excelファイルを表すクラスWorkbookを提供します。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 | セルの値が不明であることを指定します。 |
上記の事前定義されたセル値タイプを使用して、各セルに存在するデータのTypeと比較することもできます。
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; | |
} | |
} | |
} |