翻譯|使用教程|編輯:李顯亮|2021-07-28 10:56:47.440|閱讀 384 次
概述:在文中,將介紹如何在 C# 中將大型 Excel 文件導出為CSV的問題。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
在本主題中,我們將介紹如何在 C# 中將大型 Excel 文件導出為CSV的問題。下面給出的在 C# 應用程序中以編程方式將 Excel 文件轉換為 CSV 格式的步驟以及簡單易行的代碼將為您提供所需的解決方案。
開發人員在處理像XLSX或XLS這樣的大型 Excel 文件時面臨的主要問題是內存管理。通過將LoadOptions 類的MemorySetting 屬性設置為MemoryPreference,可以輕松解決此問題。這將有助于有效地管理內存。此屬性的默認值是 Normal,它應該用于常規大小的 Excel 文件。
Aspose.Cells for .NET是Excel電子表格編程API,可加快電子表格的管理和處理任務,支持構建能夠生成,修改,轉換,呈現和打印電子表格的跨平臺應用程序。同時不依賴于Microsoft Excel或任何Microsoft Office Interop組件。你可以下載《Aspose.Cells for .NET開發者指南》獲取更多幫助。
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 屬性來正確有效地管理內存。
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# 控制臺應用程序,但您可以使用相同的代碼在 ASP.NET 中將 Excel 文件導出為 CSV,或者在使用 .NET Framework 的 Windows 應用程序中將 Excel 文件轉換為 CSV。這不需要運行代碼的系統或服務器上的 Excel 文件。
如果您發現任何其他關于在 C# 中將大型 Excel 文件導出為 CSV的問題,請在Aspose免費支持論壇中提問哦 。本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn