Search code examples
export-to-csvspreadsheetgear

SpreadsheetGear - Save Specific Workbook Sheet to CSV


I am opening an existing Excel file using SpreadsheetGear, using the following code:

SpreadsheetGear.IWorkbook xlBook = SpreadsheetGear.Factory.GetWorkbook(fileName, System.Globalization.CultureInfo.CurrentCulture); xlBook.SaveAs(fileNameCSV, SpreadsheetGear.FileFormat.CSV);

This works, but the saved CSV file contains the wrong sheet.

Can anyone help with a code snippet on how to open an Excel file in SpreadsheetGear, then save only a SPECIFIC sheet to a CSV file.

Please note I am working with SpreadsheetGear and want a solution for that library. Thanks!


Solution

  • The IWorksheet interface includes a SaveAs(...) method for just this purpose:

    using SpreadsheetGear;
    using System.Globalization;
    ...
    
    IWorkbook xlBook = Factory.GetWorkbook(fileName, CultureInfo.CurrentCulture); 
    xlBook.Worksheets["My Sheet"].SaveAs(fileNameCSV, FileFormat.CSV);
    

    I'll also mention that there is also an IRange.SaveAs(...) method if you want to save just a particular range to CSV / UnicodeText (tab-delimited).