يوضح المستند كيفية إنشاء ملف Excel في C#. توضح الخطوات التالية ومقتطف الشفرة كيفية إنشاء ملف Excel في C# بدون استخدام MS Office interop.
خطوات إنشاء ملف Excel في C#
- قم بتنزيل أو تثبيت حزمة Aspose.Cells for .NET من NuGet.org
- استيراد مساحة الاسم Aspose.Cells إلى مشروع VS.NET
- حدد كائن الترخيص وقم بتعيينه باستخدام طريقة SetLicense
- إنشاء Workbook فارغ
- أدخل بيانات التقرير في خلايا أول Worksheet
- احفظ ملف Excel XLSX بالبيانات
على مر السنين ، من المعروف أن تنسيقات ملفات MS Excel تُستخدم لتحليل البيانات وإعداد تقارير البيانات. تصف الخطوات المذكورة أعلاه إجراء الكتابة إلى ملف Excel باستخدام C#. نكتب كود C# لإنشاء ملف Excel جديد من البداية. سيحتوي ملف Excel على نموذج تقرير العملاء ؛ يمكنك إنشاء التقرير المطلوب عن طريق ملء الخلايا ذات الصلة بالبيانات في ورقة العمل.
نموذج التعليمات البرمجية لإنشاء ملف Excel في C#
//Add reference to the namespace of Aspose.Cells for .NET | |
using Aspose.Cells; | |
namespace GenerateExcelFile | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//At the start, set the license before using other APIs | |
Aspose.Cells.License Aspose_Cells_license = new Aspose.Cells.License(); | |
Aspose_Cells_license.SetLicense("Aspose.Cells.lic"); | |
//Create an Excel workbook from the scratch | |
Workbook ExcelFileWorkbook = new Workbook(); | |
//Get the first worksheet (0 indexed position) in the workbook, the default worksheet | |
Worksheet ExcelFileSheet = ExcelFileWorkbook.Worksheets[0]; | |
//Get the cells collection in the default worksheet | |
Cells SheetCells = ExcelFileSheet.Cells; | |
//Insert data into the cells of the sheet | |
SheetCells["A1"].PutValue("Customers Report"); | |
SheetCells["A2"].PutValue("C_ID"); | |
SheetCells["B2"].PutValue("C_Name"); | |
SheetCells["A3"].PutValue("C001"); | |
SheetCells["B3"].PutValue("Customer1"); | |
SheetCells["A4"].PutValue("C002"); | |
SheetCells["B4"].PutValue("Customer2"); | |
SheetCells["A5"].PutValue("C003"); | |
SheetCells["B5"].PutValue("Customer3"); | |
SheetCells["A6"].PutValue("C004"); | |
SheetCells["B6"].PutValue("Customer4"); | |
//Save to Excel file (XLSX) | |
ExcelFileWorkbook.Save("ExcelFile.xlsx"); | |
} | |
} | |
} |
في المثال أعلاه ، ستتعلم كيفية إنشاء ملف Excel ديناميكيًا في C#. باستخدام C# .NET ، يمكنك بسهولة إنشاء ملف Excel. في مقتطف الشفرة ، نقوم بإدخال البيانات في بضع خلايا من ورقة العمل الأولى في المصنف لعمل تقرير بسيط. في النهاية ، نقوم بحفظ ملف Excel XLSX على القرص. بالإضافة إلى ذلك ، قد ترغب في كيفية تصدير ملف Excel كبير إلى CSV في C#.