إدارة بيانات ملفات إكسل

كيفية إضافة بيانات إلى الخلايا

يوفر Aspose.Cells for Node.js via C++ فئة، Workbook، تمثل ملف Microsoft Excel. تحتوي فئة Workbook على مجموعة Worksheets تتيح الوصول إلى كل ورقة عمل في ملف Excel. تمثل ورقة العمل بواسطة فئة Worksheet. توفر فئة Worksheet مجموعة Cells. كل عنصر في مجموعة Cells يمثل كائنًا من فئة Cell.

يسمح Aspose.Cells للمطورين بإضافة البيانات إلى خلايا ورقة العمل من خلال استدعاء طريقة putValue من فئة Cell. توفر Aspose.Cells نسخ تحميل متراكبة من طريقة putValue التي تتيح للمطورين إضافة أنواع مختلفة من البيانات إلى الخلايا. باستخدام هذه النسخ التحميلية من طريقة putValue، من الممكن إضافة قيمة Boolean، سلسلة، Double، عدد صحيح، أو تاريخ/وقت، وغيرها إلى الخلية.

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 العديد من الخصائص التي تسمح للمطورين باسترجاع القيم من الخلايا وفقًا لأنواع بياناتها. تشمل هذه الخصائص:

عندما لا يتم ملء حقل، فإن الخلايا التي تحتوي على getDoubleValue() أو getFloatValue() ترمي استثناءً.

يمكن أيضًا فحص نوع البيانات المحتواة في الخلية باستخدام طريقة getType() من فئة Cell. في الواقع، تعتمد طريقة getType() من فئة Cell على تعداد 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;
}
}
}

مواضيع متقدمة