ในหัวข้อนี้ เราจะตอบคำถามเกี่ยวกับวิธีส่งออกไฟล์ Excel ขนาดใหญ่ไปยัง CSV ใน C# ขั้นตอนในการแปลงไฟล์ Excel เป็นรูปแบบ CSV โดยทางโปรแกรมในแอปพลิเคชัน C# ที่ระบุด้านล่างพร้อมกับรหัสที่ง่ายและสะดวกจะให้วิธีแก้ปัญหาที่จำเป็นแก่คุณ
ปัญหาหลักที่นักพัฒนาเผชิญเมื่อประมวลผลไฟล์ Excel ขนาดใหญ่ เช่น XLSX หรือ XLS คือการจัดการหน่วยความจำ ปัญหานี้สามารถแก้ไขได้ง่ายๆ โดยตั้งค่า MemorySetting property ของคลาส LoadOptions เป็น MemoryPreference สิ่งนี้จะช่วยจัดการหน่วยความจำได้อย่างมีประสิทธิภาพ ค่าเริ่มต้นของคุณสมบัตินี้คือ ปกติ ซึ่งควรใช้ในกรณีของไฟล์ Excel ขนาดปกติ
ขั้นตอนในการส่งออกไฟล์ Excel ขนาดใหญ่เป็น CSV ใน C#
- ติดตั้งแพ็คเกจ Aspose.Cells for .NET จาก NuGet.org
- เพิ่มการใช้คำสั่งสำหรับเนมสเปซ Aspose.Cells
- ตั้งค่าใบอนุญาต Aspose โดยใช้วิธี SetLicense
- ตั้งค่าคุณสมบัติ MemorySetting เป็นตัวเลือก MemoryPreference
- สร้างอินสแตนซ์ของ Workbook Class และส่งออบเจกต์ LoadOptions ที่สร้างในขั้นตอนก่อนหน้า
- สุดท้าย บันทึกไฟล์ 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# แต่คุณสามารถใช้โค้ดเดียวกันนี้เพื่อส่งออกไฟล์ Excel เป็น CSV ใน ASP.NET หรือแปลง Excel file format เป็น CSV ในแอปพลิเคชัน Windows ด้วย .NET Framework สิ่งนี้ไม่ต้องการไฟล์ Excel บนระบบหรือเซิร์ฟเวอร์ที่รหัสของคุณทำงานอยู่