Aspose.Cells save workbook to stream

In Aspose 4.8, I was able to use Workbook.Save and write to a generic stream object, so I could use the same code to write to a file or the HttpResponse.OutputStream.


We’ve just upgraded to v7.3 and I find that this seems to be no longer an option. I have one method that writes to a file and another that requires the entire HttpResponse. Is there a way to just stream the saved workbook’s bytes to a stream and let the developer worry about where to send them?

PS: Your website seems very slow at the moment, and I can’t get the “Live Chat” to work at all.

Hi,

Thanks for using Aspose.Cells.

Please download and use the latest version:

Aspose.Cells for .NET 7.3.2



Please use this code to save your file in XLS or in XLSX format in Response stream. It should solve your problem.

C#


//Save file and send to client browser using selected format
if (yourFileFormat == “XLS”)
{
workbook.Save(HttpContext.Current.Response, “output.xls”, ContentDisposition.Attachment, new XlsSaveOptions(SaveFormat.Excel97To2003));

}
else
{
workbook.Save(HttpContext.Current.Response, “output.xlsx”, ContentDisposition.Attachment, new OoxmlSaveOptions(SaveFormat.Xlsx));
}

HttpContext.Current.Response.End();


VB.NET


'Save file and send to client browser using selected format

If yourFileFormat = “XLS” Then

workbook.Save(HttpContext.Current.Response, “output.xls”, ContentDisposition.Attachment, New XlsSaveOptions(SaveFormat.Excel97To2003))

Else

workbook.Save(HttpContext.Current.Response, “output.xlsx”, ContentDisposition.Attachment, New OoxmlSaveOptions(SaveFormat.Xlsx))

End If


HttpContext.Current.Response.End()

Hi,

In the latest versions, you may try some Workbook.Save overloads for saving file to streams or response object, SaveFormat can be set to Auto, e.g

public void Save(Stream,SaveFormat);

public void Save(Stream,SaveOptions);

public void Save(HttpResponse,string,ContentDisposition,SaveOptions);

For complete reference, see the document:

Thank you.

Hi Amjad,


Thanks for the quick reply :slight_smile:

When I use workbook.Save(Stream, SaveFormat), I get:

[NotSupportedException: Specified method is not supported.]
System.Web.HttpResponseStream.set_Position(Int64 value) +29
Aspose.Cells.Workbook.›Ÿ(Stream ) +23
”..Write() +347
”..ˆ(Workbook , Stream , FileFormatType , SaveOptions ) +165
Aspose.Cells.Workbook.Save(Stream stream, SaveOptions saveOptions) +2534
Aspose.Cells.Workbook.Save(Stream stream, SaveFormat saveFormat) +32

Hi,


Please try our latest version v7.3.2:
http://www.aspose.com/community/files/51/.net-components/aspose.cells-for-.net/entry413673.aspx


If you still find any issue, kindly provide us runnable sample code / sample application to reproduce the issue on our end. We will check it soon.

Thank you.

Thanks Amjad, version 7.3.2 works (7.3.0 did not).


Can I just ask one more question?

What is the difference between the FileFormatType enum and the SaveFormat enum? This is a minor difference, but previously Workbook.Save took a FileFormatType, now it takes a SaveFormat instead.

Thanks again for helping me sort this out. Love Aspose!

R.

Hi,


Well, FileFormatType enum is replaced with LoadFormat and SaveFormat in new versions.

We still have FileFormatType enum but it is only for Workbook constructor’s usage, e.g

Workbook workbook = new Workbook(FileFormatType.Xlsx);
If you need to create a workbook object with respect to XLSX (Excel 2007/2010) file format type, you may specify the FileFormatType here otherwise no big usage of this enum anymore. By default a workbook is created in Excel 2003 XLS file format type.

Thank you.

Thanks Again Amjad. If I initialise a workbook with just newWorkbook(), ie it is in Excel 2003 format, can I save it with SaveFormat.Xlsx? Presumably the affect of FileFormatType in the constructor is just to limit the features that can be used within the workbook?

Hi,


Yes, sure you may save the workbook to XLSX file format. When the workbook is saved in Xlsx file format (no matter you have initialized the workbook using default (blank) constructor, the workbook will be using all the features/standards of XLSX (Excel 2007/2010) file format surely.

e.g

Workbook wb = new Workbook();

// Your code goes here.


//Save the file to XLSX file format
wb.Save(“e:\test2\Book2.xlsx”, SaveFormat.Xlsx);


As I said earlier there might be a few cases when you don’t really want to save the workbook to XLSX file rather you need to create a new workbook (with respect to Excel 2007/2010), input/process or import/export some data to/from the worksheets, or utilize the features/results or other objects at runtime, here you will need to initialize the workbook with respect to XLSX file format type only.

Hope, you understand it now.

Thank you.


Thanks Amjad, all crystal clear now.


Bye!

R.

Hi,

It’s good to know that you now understand the difference between two enumerations.

Let us know if you face any other issue, we will be glad to help you.

Hi,

When I am trying above code i.e.

workbook.Save(HttpContext.Current.Response, “output.xls”, ContentDisposition.Attachment, New XlsSaveOptions(SaveFormat.Excel97To2003))

that actually, simply download excel file in downloads folder but i want to open/display that file in browser. i tried “ContentDisposition.Inline” as well but no difference.

Please suggest, how i can open excel file in browser ?

Thanks

Hi,

Thanks for your posting and considering Aspose.Cells.

The web browser cannot display excel files, so whether you specify inline or attachment, it will not display the excel file inside it. However, some browser can display pdf inline. So if you specify inline for pdf format, then some browser like FireFox will display the pdf inline and if you specify attachment, then it will display as attachment.

We have verified this scenario using the following sample code. You can also download the attached sample project and test it at your end.

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Aspose.Cells;

public partial class _Default : System.Web.UI.Page
{
string openMode = “Inline”;

protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
Workbook workbook = new Workbook();

Worksheet worksheet = workbook.Worksheets[0];

worksheet.Cells[“B5”].PutValue(“This is a sample text.”);

//Save file and send to client browser using selected format
if (openMode == “Inline”)
{
workbook.Save(HttpContext.Current.Response, “output.pdf”, ContentDisposition.Inline, new PdfSaveOptions());
}
else
{
workbook.Save(HttpContext.Current.Response, “output.pdf”, ContentDisposition.Attachment, new PdfSaveOptions());
}

HttpContext.Current.Response.End();
}
}