We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Unable to send aspose report using web api

Following code works when saving to disk but when sending as filestreamresult it shows 500 internal error on client side. My api method is as below:

public FileResult GetFile()
{
//open aspose workbook designer
WorkbookDesigner wd = new WorkbookDesigner();
//open template file workbook
wd.Workbook = new Workbook(TemplateProps.XLTemplateFullPath);
//bind dataset to workbook as datasource
wd.SetDataSource(this.DataSource);
//process aspose markers
wd.Process(true);
//wd.Workbook.Save(@“I:” + FileName);
//return stream for workbook
var stream = wd.Workbook.SaveToStream();
var result = new FileStreamResult(stream, “application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”)
return result;
}

@nikhilpinto,

Workbook.SaveToStream --> will save the workbook in XLS. So, if you want to save to XLSX or more advanced MS Excel formats, you need to use the overloaded method, e.g Workbook.Save(stream, SaveFormat.Xlsx) for the purpose. See the following lines of code that you may try:
e.g
Sample code:

MemoryStream stream = new MemoryStream();
wd.Workbook.Save(stream, SaveFormat.Xlsx);

Hope, this helps a bit.

Still unable to download the report on client end.
Is the content type correct?
new FileStreamResult(stream, “application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”)

@nikhilpinto,
Please give a try to the following sample code and share the feedback.

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:Button ID="btnSaveToXLS" runat="server" Text="Save to XLS" OnClick="btnSaveToXLS_Click" /> <br /><br />
        <asp:Button ID="btnSaveToXLSX" runat="server" Text="Save to XLSX" OnClick="btnSaveToXLSX_Click" />

    </div>
    </form>
</body>
</html>

Default.aspx.cs

sing 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
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSaveToXLS_Click(object sender, EventArgs e)
    {
        //Create empty workbook.
        Workbook workbook = new Workbook();

        // Access first worksheet
        Worksheet worksheet = workbook.Worksheets[0];

        // Put some value in cell C4
        Cell cell = worksheet.Cells["C4"];
        cell.PutValue("This is XLS format generated by Aspose.Cells API.");

        // Save file and send to client browser using selected format
        workbook.Save(HttpContext.Current.Response, "outputSaveToXLS.xls", ContentDisposition.Attachment, new XlsSaveOptions(SaveFormat.Excel97To2003));

        HttpContext.Current.Response.End();
    }

    protected void btnSaveToXLSX_Click(object sender, EventArgs e)
    {
        //Create empty workbook.
        Workbook workbook = new Workbook();

        // Access first worksheet
        Worksheet worksheet = workbook.Worksheets[0];

        // Put some value in cell C4
        Cell cell = worksheet.Cells["C4"];
        cell.PutValue("This is XLSX format generated by Aspose.Cells API.");

        // Save file and send to client browser using selected format
        workbook.Save(HttpContext.Current.Response, "outputSaveToXLSX.xlsx", ContentDisposition.Attachment, new OoxmlSaveOptions(SaveFormat.Xlsx));

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

@nikhilpinto,

Please note, the 500 Internal Server Error is a very general HTTP status code that means something has gone wrong on your web site’s server, so you should debug your own code on your end and fix it. The issue/error may not be related to Aspose.Cells APIs or nothing to do with Aspose.Cells.

Please try using the code segment shared by Ahsan Iqbal. If you want to use only HttpResponse and relevant IO APIs, you may try as following:
e.g
Sample code:

/*Send workbook to response*/
            OoxmlSaveOptions xSaveOptions = new OoxmlSaveOptions(SaveFormat.Xlsx);

            MemoryStream tempStream = new MemoryStream();
           wd.Workbook.Save(tempStream, xSaveOptions);

            //set the position.
            tempStream.Position = 0;

            this.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            this.Current.Response.AddHeader("content-disposition", "attachment; filename=out1.xlsx" );
            Response.BinaryWrite(tempStream.ToArray());
           Response.End();

Hope, this helps a bit.