Split Workbooks

This demo uses the DevExpress Spreadsheet Document API to split a document into logical parts and save each part (worksheet) as a separate file in the required format.

You can split the predefined sample file or supply your own document. To do the latter, select Upload a File in the file selection drop-down menu. In the Split Settings panel, select the workbook split mode.

Use the Split and Save as... dropdown button to select the output format, split the workbook, and download the result.

Select a Document
TrendlineAnalysis_Template.xlsx



using DevExpress.Spreadsheet;

IReadOnlyList<Stream> SplitWorkbook(bool splitVisibleOnly, Stream inputStream, DocumentFormat outputFormat) {
    // Load document from stream
    using var sourceWorkbook = new Workbook();

    sourceWorkbook.LoadDocument(inputStream);

    // Split document worksheets to separate documents
    var worksheetsToSplit = new List<Worksheet>();
    if(splitVisibleOnly) {
        // Get visible worksheets only
        worksheetsToSplit = sourceWorkbook.Worksheets.Where(worksheet => worksheet.Visible).ToList();
    } else {
        // Get all worksheets except "Very hidden"
        worksheetsToSplit = sourceWorkbook.Worksheets.Where(worksheet => worksheet.VisibilityType != WorksheetVisibilityType.VeryHidden).ToList();
    }

    // Create separate documents from selected worksheets
    var documentStreams = new List<Stream>();
    foreach(var sourceWorksheet in worksheetsToSplit) {
        using var targetWorkbook = new Workbook();
        var targetWorksheet = targetWorkbook.Worksheets[0];
        targetWorksheet.Name = sourceWorksheet.Name;
        targetWorksheet.CopyFrom(sourceWorksheet);

        // Ensure the copied worksheet is visible in the output document
        targetWorksheet.VisibilityType = WorksheetVisibilityType.Visible;

        // Prepare output stream
        var outputStream = new MemoryStream();

        // Save the document
        targetWorkbook.SaveDocument(outputStream, outputFormat);

        outputStream.Position = 0;
        documentStreams.Add(outputStream);
    }
    return documentStreams.AsReadOnly();
}