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

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Adding a string value to the cell
worksheet.Cells["A1"].PutValue("Hello World");
// Adding a double value to the cell
worksheet.Cells["A2"].PutValue(20.5);
// Adding an integer value to the cell
worksheet.Cells["A3"].PutValue(15);
// Adding a boolean value to the cell
worksheet.Cells["A4"].PutValue(true);
// Adding a date/time value to the cell
worksheet.Cells["A5"].PutValue(DateTime.Now);
// Setting the display format of the date
Style style = worksheet.Cells["A5"].GetStyle();
style.Number = 15;
worksheet.Cells["A5"].SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "output.out.xls");

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

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

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

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

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

  • StringValue: يعيد قيمة السلسلة للخلية.
  • DoubleValue: يعيد قيمة مزدوجة للخلية.
  • BoolValue: يعيد قيمة Boolean للخلية.
  • DateTimeValue: يعيد قيمة تاريخ / وقت للخلية.
  • FloatValue: يعيد قيمة عائمة للخلية.
  • IntValue: يعيد قيمة العدد الصحيح للخلية.

عندما لا يتم ملؤها، تثير الخلايا مع DoubleValue أو FloatValue استثناء.

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

أنواع قيم الخلية الوصف
IsBool يحدد أن قيمة الخلية هي بولية.
IsDateTime يحدد أن قيمة الخلية هي تاريخ/وقت.
IsNull تمثل خلية فارغة.
IsNumeric يحدد أن قيمة الخلية هي رقمية.
IsString يحدد أن قيمة الخلية هي نصية.
IsUnknown يحدد أن قيمة الخلية غير معروفة.

يمكنك أيضًا استخدام أنواع قيم الخلية المحددة مسبقًا أعلاه للمقارنة مع نوع البيانات الحاضرة في كل خلية.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Opening an existing workbook
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing first worksheet
Worksheet worksheet = workbook.Worksheets[0];
foreach (Cell cell1 in worksheet.Cells)
{
// Variables to store values of different data types
string stringValue;
double doubleValue;
bool boolValue;
DateTime dateTimeValue;
// Passing the type of the data contained in the cell for evaluation
switch (cell1.Type)
{
// Evaluating the data type of the cell data for string value
case CellValueType.IsString:
stringValue = cell1.StringValue;
Console.WriteLine("String Value: " + stringValue);
break;
// Evaluating the data type of the cell data for double value
case CellValueType.IsNumeric:
doubleValue = cell1.DoubleValue;
Console.WriteLine("Double Value: " + doubleValue);
break;
// Evaluating the data type of the cell data for boolean value
case CellValueType.IsBool:
boolValue = cell1.BoolValue;
Console.WriteLine("Bool Value: " + boolValue);
break;
// Evaluating the data type of the cell data for date/time value
case CellValueType.IsDateTime:
dateTimeValue = cell1.DateTimeValue;
Console.WriteLine("DateTime Value: " + dateTimeValue);
break;
// Evaluating the unknown data type of the cell data
case CellValueType.IsUnknown:
stringValue = cell1.StringValue;
Console.WriteLine("Unknown Value: " + stringValue);
break;
// Terminating the type checking of type of the cell data is null
case CellValueType.IsNull:
break;
}
}

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