Spread WPF 18
Features / File Operations / Save Files
In This Topic
    Save Files
    In This Topic

    Spread for WPF allows you to save/export the content of a workbook or worksheet in various formats, such as excel, pdf, xml, and html using the SaveAs method of the IWorkbook interface. To specify the file format, when saving, use the fields of the FileFormat enumeration.

    Supported Formats

    Save as Excel File (.xlsx)

    You can save the content of spread to the default Excel format using the OpenXMLWorkbook field of the FileFormat enumeration.

    Refer to the following example code to save data to a .xlsx file.

    Copy Code
    // Save the workbook in Excel format.
    spreadSheet1.Workbook.SaveAs("Book1.xlsx", GrapeCity.Spreadsheet.IO.FileFormat.OpenXMLWorkbook);
    // Save a worksheet in Excel format.
    spreadSheet1.Workbook.Worksheets[0].SaveAs("Sheet1.xlsx", GrapeCity.Spreadsheet.IO.FileFormat.OpenXMLWorkbook);
    
    Copy Code
    ' Save the workbook in Excel format.
    spreadSheet1.Workbook.SaveAs("Book1.xlsx", GrapeCity.Spreadsheet.IO.FileFormat.OpenXMLWorkbook)
    ' Save a worksheet in Excel format.
    spreadSheet1.Workbook.Worksheets(0).SaveAs("Sheet1.xlsx", GrapeCity.Spreadsheet.IO.FileFormat.OpenXMLWorkbook)
    

    Save as Macro-Enabled Workbook (.xlsm)

    You can also save data as a macro-enabled workbook, which is preferred if your file contains macros. Use the OpenXMLWorkbookMacroEnabled field of the FileFormat enumeration to save data directly as .xlsm files.

    Refer to the following example code to save to a .xlsm file.

    Copy Code
    // Save the workbook in macro-enabled format.
    spreadSheet1.Workbook.SaveAs("Book1.xlsm", GrapeCity.Spreadsheet.IO.FileFormat.OpenXMLWorkbookMacroEnabled);
    // Save a worksheet in macro-enabled format.
    spreadSheet1.Workbook.Worksheets[0].SaveAs("Sheet1.xlsm", GrapeCity.Spreadsheet.IO.FileFormat.OpenXMLWorkbookMacroEnabled);
    
    Copy Code
    ' Save the workbook in macro-enabled format.
    spreadSheet1.Workbook.SaveAs("Book1.xlsm", GrapeCity.Spreadsheet.IO.FileFormat.OpenXMLWorkbookMacroEnabled)
    ' Save a worksheet in macro-enabled format.
    spreadSheet1.Workbook.Worksheets(0).SaveAs("Sheet1.xlsm", GrapeCity.Spreadsheet.IO.FileFormat.OpenXMLWorkbookMacroEnabled)
    

    Save as Comma-Separated Value (.csv)

    You can export data to the Comma-Separated Value (.csv) files. A CSV file is a plain text file that uses a comma to separate the values where each row of data starts from a new line. To save data as a .csv file, use the CSV field of the FileFormat enumeration.

    Note: Worksheet headers cannot be exported to CSV files.

    Refer to the following example code to save data to a .csv file.

    Copy Code
    // Save the workbook to CSV format.
    spreadSheet1.Workbook.SaveAs("Book1.csv", GrapeCity.Spreadsheet.IO.FileFormat.CSV);
    // Save a worksheet to CSV format.
    spreadSheet1.Workbook.Worksheets[0].SaveAs("Sheet1.csv", GrapeCity.Spreadsheet.IO.FileFormat.CSV);
    
    Copy Code
    ' Save the workbook to CSV format.
    spreadSheet1.Workbook.SaveAs("Book1.csv", GrapeCity.Spreadsheet.IO.FileFormat.CSV)
    ' Save a worksheet to CSV format.
    spreadSheet1.Workbook.Worksheets(0).SaveAs("Sheet1.csv", GrapeCity.Spreadsheet.IO.FileFormat.CSV)
    

    Save as Portable Document File (.pdf)

    You can export a workbook/worksheet to a Portable Document File (PDF). This type of format is good for sharing non-editable versions of your data. To save as a .pdf file, use the PDF field of the FileFormat enumeration.

    Refer to the following example code to save data to a .pdf file.

    Copy Code
    // Save the workbook in pdf format.
    spreadSheet1.Workbook.SaveAs("Book1.pdf", GrapeCity.Spreadsheet.IO.FileFormat.PDF);
    // Save a worksheet in pdf format.
    spreadSheet1.Workbook.Worksheets[0].SaveAs("Sheet1.pdf", GrapeCity.Spreadsheet.IO.FileFormat.PDF);
    
    Copy Code
    ' Save the workbook in pdf format.
    spreadSheet1.Workbook.SaveAs("Book1.pdf", GrapeCity.Spreadsheet.IO.FileFormat.PDF)
    ' Save a worksheet in pdf format.
    spreadSheet1.Workbook.Worksheets(0).SaveAs("Sheet1.pdf", GrapeCity.Spreadsheet.IO.FileFormat.PDF)
    

    Save as Hypertext Markup Language (.html)

    Hypertext Markup Language (HTML) files can be accessed with a web browser. You can export a workbook or worksheet to an HTML file. To save as a .html file, use the Html field of the FileFormat enumeration.

    Refer to the following example code that saves the data from a workbook to a .html file.

    Copy Code
    // Save the workbook in HTML format.
    spreadSheet1.Workbook.SaveAs("Book1.html", GrapeCity.Spreadsheet.IO.FileFormat.Html);
    // Save a worksheet in HTML format.
    spreadSheet1.Workbook.Worksheets[0].SaveAs("Sheet1.html", GrapeCity.Spreadsheet.IO.FileFormat.Html);
    
    Copy Code
    ' Save the workbook in HTML format.
    spreadSheet1.Workbook.SaveAs("Book1.html", GrapeCity.Spreadsheet.IO.FileFormat.Html)
    ' Save a worksheet in HTML format.
    spreadSheet1.Workbook.Worksheets(0).SaveAs("Sheet1.html", GrapeCity.Spreadsheet.IO.FileFormat.Html)
    

    Export Options

    Additionally, while saving a workbook, you can also specify various options for exporting data by setting the following values of the ExportOptions enumeration in the Options property of the ExportContext class.

    Value Description
    ColumnHeader Indicates that the column header will be exported as top rows of the worksheet.
    DataOnly Indicates that only cell values are exported.
    Default The default export options.
    Exchangeable Spread data will be saved as extension of OpenXML file format.
    ExternalReference Indicates that the saved file will be used for external references to the workbook.
    IncludeAutoMergedCells Indicates that the automatically merged cells will be exported.
    NoFormulas Indicates that cell formulas aren't exported.
    None No option is available.
    PreferTextOnExport The cell text will be exported if there is an explicit cell type.
    RowHeader Indicates that the row header will be exported as left columns of the worksheet.

    In the following example code, ExportOptions.ColumnHeader is specified to include the column headers in the exported Excel file.

    Copy Code
    ExportContext exportContext = new ExportContext();
    exportContext.Options = ExportOptions.ColumnHeader;
    spreadSheet1.Workbook.SaveAs("NewTestExcel.xlsx", GrapeCity.Spreadsheet.IO.FileFormat.OpenXMLWorkbook, null, context: exportContext);
    
    Copy Code
    Dim exportContext As ExportContext = New ExportContext()
    exportContext.Options = ExportOptions.ColumnHeader
    spreadSheet1.Workbook.SaveAs("NewTestExcel.xlsx", GrapeCity.Spreadsheet.IO.FileFormat.OpenXMLWorkbook, Nothing, context:=exportContext)