Skip to main content
All docs
V23.2

Create a Document

The Spreadsheet control always contains a document. If you do not open a document, the Spreadsheet generates an empty document. You can also call the New() method to create an empty document on demand.

A newly created or generated document has an empty identifier. The control cannot save such a document and loses content when you open or create another document. Generate a unique string identifier for this document to save pending changes.

The example below demonstrates how to create a document after a user clicks a custom New Document button:

@model SpreadsheetDocumentContentFromBytes

<script>
    function OnClick() {
        var spreadsheetState = spreadsheet.getSpreadsheetState();
        $.ajax({
            type: 'POST',
            url: '@Url.Action("NewDocument")',
            data: { spreadsheetState: spreadsheetState},
            success: function (result) {
                $("#container").html(result);
            }
        });
    }
</script>
<input type="button" onclick="OnClick()" value="New document" />
<div class="text-center">
    <div id="container">
        @Html.Partial("SpreadsheetPartialView", @Model)
    </div>
</div>
public ActionResult NewDocument(SpreadsheetClientState spreadsheetState) {
    var spreadsheet = SpreadsheetRequestProcessor.GetSpreadsheetFromState(spreadsheetState);
    spreadsheet.New();
    // ...
    byte[] docBytes = spreadsheet.SaveCopy(DocumentFormat.Xlsx);
    Func<byte[]> contentAccessor = () => docBytes;
    var model = new SpreadsheetDocumentContent(Guid.NewGuid().ToString(), contentAccessor);
    return PartialView("SpreadsheetPartialView", model);
}

View Example: Spreadsheet for ASP.NET Core - How to use AJAX requests to update document content