burkerking
burkerking

Reputation: 3

Is there a way to format an Aspose.Cells generated excel sheet without calling Workbook.Save()?

My customer has a use case for exporting search results to a spreadsheet. I would like to return a formatted spreadsheet to them, but the only way I can get the formatting changes to "stick" is by calling

workbook.Save(memoryStream, SaveFormat.Xlsx);

The problem with calling the method above, is that a spreadsheet will actually be saved to my local project folder, which is not desired behavior. How can I return the spreadsheet without calling workbook.Save()?

    public byte[] ExportSpreadsheet(List<Result> results)
    {
        var workbook = MakeWorkbook(results);

        var memoryStream = new MemoryStream();
        workbook.Save(memoryStream, SaveFormat.Xlsx); // this saves the spreadsheet in the project
        memoryStream.Seek(0, SeekOrigin.Begin);

        var byteArray = memoryStream.ToArray();
        return byteArray;
    }

    private Workbook MakeWorkbook(List<Result> results)
    {
        var workbook = new Workbook();
        AddDataToWorkbook(workbook);
        ApplyFormattingAfterData(workbook);
        return workbook;
    }

Upvotes: 0

Views: 913

Answers (1)

Amjad Sahi
Amjad Sahi

Reputation: 1931

workbook.Save(memoryStream, SaveFormat.Xlsx);

You are doing ok. This line will save the workbook to stream and not on physical filepath. It won't save to your project's folder or path.

PS. I am working as Support developer/ Evangelist at Aspose.

Upvotes: 1

Related Questions