كيفية تصدير ملف Excel كبير إلى CSV في C#

في هذا الموضوع ، سنجيب عن السؤال حول كيفية تصدير ملف Excel كبير الحجم إلى CSV في C#. ستوفر لك خطوات تحويل ملف Excel إلى تنسيق CSV برمجيًا في تطبيقات C# الواردة أدناه جنبًا إلى جنب مع التعليمات البرمجية البسيطة والسهلة الحل المطلوب.

المشكلة الرئيسية التي يواجهها المطورون عند معالجة ملف Excel كبير مثل XLSX أو XLS هي إدارة الذاكرة. يمكن حل هذه المشكلة بسهولة عن طريق تعيين MemorySetting property من فئة LoadOptions إلى MemoryPreference. سيساعد هذا في إدارة الذاكرة بكفاءة. القيمة الافتراضية لهذه الخاصية هي Normal والتي يجب استخدامها في حالة وجود ملفات Excel ذات حجم عادي.

خطوات لتصدير ملف Excel كبير إلى CSV في C#

  1. قم بتثبيت حزمة Aspose.Cells for .NET من NuGet.org
  2. أضف باستخدام التوجيه لـ Aspose.Cells مساحة الاسم
  3. تعيين ترخيص Aspose باستخدام طريقة SetLicense
  4. اضبط خاصية MemorySetting على خيار MemoryPreference
  5. قم بإنشاء مثيل لـ Workbook Class وقم بتمرير كائن LoadOptions الذي تم إنشاؤه في الخطوة السابقة
  6. أخيرًا ، احفظ ملف الإخراج CSV الذي تم تصديره

كود C# لحفظ ملف Excel كبير بتنسيق CSV

using System;
//Add reference to Aspose.Cells for .NET API
//Use following namespaces to Export excel file to CSV
using Aspose.Cells;
namespace ExportLargeExcelFiletoCSV
{
class Program
{
static void Main(string[] args)
{
//Set Aspose license before exporting Excel file to CSV file format
//using Aspose.Cells for .NET
Aspose.Cells.License AsposeCellsLicense = new Aspose.Cells.License();
AsposeCellsLicense.SetLicense(@"c:\asposelicense\license.lic");
//For optimized memory usage for large excel file use
//MemoryPreference MemorySetting option
LoadOptions OptionsLoadingLargeExcelFile = new LoadOptions();
OptionsLoadingLargeExcelFile.MemorySetting = MemorySetting.MemoryPreference;
//Create an instance of Workbook class to load input large excel file
//Also pass the MemoryPreference load options to the constructor
Workbook ExportExcelToCSVWorkBook = new Workbook("Large_Excel_To_Export.xlsx", OptionsLoadingLargeExcelFile);
//Save the exported output file as CSV format
ExportExcelToCSVWorkBook.Save("Exported_Output_CSV.csv", SaveFormat.Csv);
}
}
}

يحفظ الكود أعلاه الورقة الأولى فقط في ملف Excel كملف CSV. ومع ذلك ، إذا كان لديك أوراق متعددة في ملف Excel الكبير ، فيمكنك استخدام مقتطف الشفرة التالي. يرجى ملاحظة ، في هذه الحالة ، نحتاج مرة أخرى إلى استخدام خاصية MemorySetting نفسها لإدارة الذاكرة بشكل صحيح وفعال.

تصدير عدة أوراق Excel لفصل ملفات CSV

using System;
//Add reference to Aspose.Cells for .NET API
//Use following namespaces to Export excel file to CSV
using Aspose.Cells;
namespace ExportLargeExcelFiletoCSV
{
class Program
{
static void Main(string[] args)
{
//Set Aspose license before exporting Excel file to CSV file format
//using Aspose.Cells for .NET
Aspose.Cells.License AsposeCellsLicense = new Aspose.Cells.License();
AsposeCellsLicense.SetLicense(@"c:\asposelicense\license.lic");
//For optimized memory usage for large excel file use
//MemoryPreference MemorySetting option
LoadOptions OptionsLoadingLargeExcelFile = new LoadOptions();
OptionsLoadingLargeExcelFile.MemorySetting = MemorySetting.MemoryPreference;
//Create an instance of Workbook class to load input large excel file
//Also pass the MemoryPreference load options to the constructor
Workbook ExportExcelToCSVWorkBook = new Workbook("Large_Excel_To_Export.xlsx", OptionsLoadingLargeExcelFile);
//To save multiple sheets in a workbook use following code
for (int SheetIndex = 0; SheetIndex < ExportExcelToCSVWorkBook.Worksheets.Count; SheetIndex++)
{
ExportExcelToCSVWorkBook.Worksheets.ActiveSheetIndex = SheetIndex;
ExportExcelToCSVWorkBook.Save("Exported_CSV_" + SheetIndex + ".csv", SaveFormat.Csv);
}
}
}
}

في الكود أعلاه ، استخدمنا تطبيق C# Console ، ولكن يمكنك استخدام نفس الرمز لتصدير ملف Excel إلى CSV في ASP.NET أو تحويل Excel file format إلى CSV في تطبيقات Windows باستخدام .NET Framework. هذا لا يتطلب ملف Excel على النظام أو الخادم حيث يتم تشغيل التعليمات البرمجية الخاصة بك.

 عربي