إضافة واسترداد البيانات

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

توفر Aspose.Cells فئة Workbook التي تمثل ملف Microsoft Excel. تحتوي فئة Workbook على مجموعة Worksheets التي تسمح بالوصول إلى كل ورقة في ملف Excel. يتم تمثيل ورقة عمل بفئة Worksheet. توفر فئة Worksheet مجموعة Cells. يُمثل كل عنصر في مجموعة Cells كائن من فئة Cell.

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

Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input
U16String dirPath(u"");
//Path of output
U16String outPath(u"");
//Path of input excel file
U16String sampleData = dirPath + u"sampleData.xlsx";
//Path of output excel file
U16String outputData = outPath + u"outputData.xlsx";
//Read input excel file
Workbook workbook(sampleData);
//Accessing the second worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(1);
//Adding a string value to the cell
worksheet.GetCells().Get(u"A1").PutValue(u"Hello World");
//Adding a double value to the cell
worksheet.GetCells().Get(u"A2").PutValue(20.5);
//Adding an integer value to the cell
worksheet.GetCells().Get(u"A3").PutValue(15);
//Adding a boolean value to the cell
worksheet.GetCells().Get(u"A4").PutValue(true);
//Setting the display format of the date
Cell cell = worksheet.GetCells().Get(u"A5");
Style style = cell.GetStyle();
style.SetNumber(15);
cell.SetStyle(style);
//Save the workbook
workbook.Save(outputData);
Aspose::Cells::Cleanup();

تحسين الكفاءة

إذا كنت تستخدم طريقة PutValue لوضع كمية كبيرة من البيانات في ورقة عمل، يجب عليك إضافة القيم إلى الخلايا أولاً حسب الصفوف، ثم حسب الأعمدة. يحسن هذا النهج كفاءة تطبيقاتك بشكل كبير.

استرداد البيانات من الخلايا

توفر Aspose.Cells فئة Workbook التي تمثل ملف Microsoft Excel. تحتوي فئة Workbook على مجموعة Worksheets التي تسمح بالوصول إلى ورق العمل في الملف. تمثل ورقة عمل بفئة Worksheet. توفر فئة Worksheet مجموعة Cells. كل عنصر في مجموعة Cells يُمثل كائنًا من فئة Cell.

توفر فئة Cell عدة طرق تسمح للمطورين باسترجاع القيم من الخلايا وفقًا لأنواع بياناتها. تشمل هذه الطرق:

  • GetStringValue، تُرجع قيمة السلسلة للخلية.
  • GetDoubleValue، تُرجع القيمة العددية للخلية.
  • GetBoolValue، يعيد قيمة البيان البوليانية للخلية.
  • GetDateTimeValue، يعيد قيمة التاريخ/الوقت للخلية.
  • GetFloatValue، يعيد قيمة العدد العائم للخلية.
  • GetIntValue، يعيد قيمة العدد الصحيح للخلية.

عندما لا يتم ملؤها، تقوم الخلايا برمي استثناء عند استخدام GetDoubleValue أو GetFloatValue.

يمكن أيضًا التحقق من نوع البيانات الواردة في الخلية باستخدام فئة Cell وطريقة GetType. في الواقع، تعتمد طريقة GetType لفئة Cell على تعداد CellValueType التي قيمها المعرفة مسبقا كما هو مدرج أدناه:

أنواع قيم الخلية الوصف
CellValueType_IsBool يحدد أن قيمة الخلية هي منطقية.
CellValueType_IsDateTime يحدد أن قيمة الخلية هي تاريخ/وقت.
CellValueType_IsNull يمثل خلية فارغة.
CellValueType_IsNumeric يحدد أن قيمة الخلية هي رقمية.
CellValueType_IsString يحدد أن قيمة الخلية هي سلسلة.
CellValueType_IsUnknown يحدد أن قيمة الخلية مجهولة.
يمكنك أيضًا استخدام أنواع قيم الخلية المعرفة مسبقًا أعلاه للمقارنة مع نوع البيانات الموجودة في كل خلية.
Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input
U16String dirPath(u"");
//Path of output
U16String outPath(u"");
//Path of input excel file
U16String sampleData = dirPath + u"sampleData.xlsx";
//Read input excel file
Workbook workbook(sampleData);
//Accessing the third worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(2);
//Get cells from sheet
Cells cells = worksheet.GetCells();
//Variable declarations
U16String strVal;
Date dateVal;
double dblVal;
bool boolVal;
Enumerator<Cell> enCell = cells.GetEnumerator();
while (enCell.MoveNext())
{
Cell cell = enCell.GetCurrent();
switch (cell.GetType())
{
//Evaluating the data type of the cell data for string value
case CellValueType::IsString:
std::cout <<"Cell Value Type Is String." << std::endl;
strVal = cell.GetStringValue();
break;
//Evaluating the data type of the cell data for double value
case CellValueType::IsNumeric:
std::cout <<"Cell Value Type Is Numeric." << std::endl;
dblVal = cell.GetDoubleValue();
break;
//Evaluating the data type of the cell data for boolean value
case CellValueType::IsBool:
std::cout <<"Cell Value Type Is Bool." << std::endl;
boolVal = cell.GetBoolValue();
break;
//Evaluating the data type of the cell data for date/time value
case CellValueType::IsDateTime:
std::cout <<"Cell Value Type Is DateTime." << std::endl;
dateVal = cell.GetDateTimeValue();
break;
//Evaluating the unknown data type of the cell data
case CellValueType::IsUnknown:
cell.GetStringValue();
break;
default:
break;
}
}
Aspose::Cells::Cleanup();