简体   繁体   中英

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;
    }
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM