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.

Sample document
TrendlineAnalysis_Template.xlsx



using DevExpress.Spreadsheet;

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

    sourceWorkbook.LoadDocument(inputStream);

    var worksheetsToSplit = new List<Worksheet>();
    if(splitVisibleOnly)
        worksheetsToSplit = sourceWorkbook.Worksheets.Where(worksheet => worksheet.Visible).ToList();
    else
        worksheetsToSplit = sourceWorkbook.Worksheets.Where(worksheet => worksheet.VisibilityType != WorksheetVisibilityType.VeryHidden).ToList();

    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);

        targetWorksheet.VisibilityType = WorksheetVisibilityType.Visible;

        var outputStream = new MemoryStream();

        targetWorkbook.SaveDocument(outputStream, outputFormat);

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