Kako izvesti veliku Excel datoteku u CSV u C#

U ovoj temi ćemo odgovoriti na pitanje kako izvesti veliku Excel datoteku u CSV u C#. Koraci za programsko pretvaranje Excel datoteke u CSV format u C# aplikacijama navedeni u nastavku zajedno s jednostavnim kodom pružit će vam potrebno rješenje.

Glavni problem s kojim se programeri suočavaju prilikom obrade velikih Excel datoteka poput XLSX ili XLS je upravljanje memorijom. Ovaj se problem može jednostavno riješiti postavljanjem MemorySetting property klase LoadOptions na MemoryPreference. To će pomoći u učinkovitom upravljanju memorijom. Zadana vrijednost ovog svojstva je Normal, što bi se trebalo koristiti u slučaju Excel datoteka uobičajene veličine.

Koraci za izvoz velike Excel datoteke u CSV u C#

  1. Instalirajte paket Aspose.Cells for .NET s NuGet.org
  2. Dodaj Using direktivu za imenski prostor Aspose.Cells
  3. Postavite Aspose licencu koristeći SetLicense metodu
  4. Postavite svojstvo MemorySetting na opciju MemoryPreference
  5. Napravite instancu Workbook Class i proslijedite objekt LoadOptions kreiran u prethodnom koraku
  6. Na kraju, spremite izvezenu izlaznu CSV datoteku

C# kod za spremanje velike Excel datoteke kao CSV format

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);
}
}
}

Gornji kod sprema samo prvi list u Excel datoteci kao CSV. Međutim, ako imate više listova u velikoj excel datoteci, tada možete koristiti sljedeći isječak koda. Imajte na umu da u ovom slučaju ponovno moramo koristiti isto svojstvo MemorySetting za pravilno i učinkovito upravljanje memorijom.

Izvezite više Excel listova u zasebne CSV datoteke

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);
}
}
}
}

U gornjem kodu upotrijebili smo C# konzolnu aplikaciju, ali možete upotrijebiti isti kod za izvoz Excel datoteke u CSV u ASP.NET ili pretvoriti Excel file format u CSV u Windows aplikacijama s .NET Frameworkom. Ovo ne zahtijeva Excel datoteku na sustavu ili poslužitelju na kojem se izvodi vaš kôd.

 Hrvatski