XLS writing/save format

Hi,


How can i write/save the XLS file object, into httpresponse OutputStream ?

If possible share the example with me.

Note: I can able to write/save the same, by specifying the file path !


Advance thanks,

Rrgards
Gsam
gsam.v1@gmail.com

Hi,

Thanks for your posting and using Aspose.Cells for .NET.

Please download and use the latest version:
Aspose.Cells
for .NET v7.2.2.1



Please use the following code to save your file in XLS or in XLSX format in Response stream.

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 Shakeel,

Thanks for your promoted response.

But i need it on JAVA ! not in .NET and all.

regards,
Gsam

Hi,

Please see the following code.

It will give you idea how to use the Java for saving to response stream. I have highlighted the main things in Red color.

Java
/*
* Copyright © 2001-2011 Aspose Pty Ltd. All Rights Reserved.
*/

package com.aspose.cells.demos;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aspose.cells.Workbook;

public abstract class DemoBaseServlet extends HttpServlet
{
protected String resultFile;
protected String resultFormatSuffix;

protected abstract Workbook createReport(HttpServletRequest request) throws Exception;

protected void sendReport(HttpServletRequest request, HttpServletResponse response, Workbook wb) throws Exception
{
wb.save(response.getOutputStream(), wb.getFileFormat());
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,
ServletException
{
try
{
Workbook wb = createReport(request);
setResponseHeader(response, resultFile, resultFormatSuffix == null ? resultFile : resultFormatSuffix);
sendReport(request, response, wb);
}
catch(Exception e)
{
writeError(e, response);
return;
}
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}

protected void setResponseHeader(HttpServletResponse response, String fileName, String formatSuffix)
{
String contentType = “application/vnd.ms-excel”;
formatSuffix = formatSuffix.toLowerCase();
if(formatSuffix.endsWith(".xlsx")
|| formatSuffix.endsWith(".xlsb")
|| formatSuffix.endsWith(".xlsm")
|| formatSuffix.endsWith(".xltm")
|| formatSuffix.endsWith(".xltx"))
{
contentType = “application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”;
}
response.setContentType(contentType);
response.addHeader(“content-disposition”, “inline;filename=” + fileName);
}


private void writeError(Throwable e, HttpServletResponse response) throws IOException
{
response.setContentType(“text/html”);
PrintWriter writer = new PrintWriter(response.getOutputStream());

writer.println("");
writer.println("");
writer.println(“sorry”);
writer.println("");
writer.println("");
writer.println(“

Sorry, the demo programme failed for some reason.

”);
e.printStackTrace(writer);
writer.println("");
writer.println("");
writer.close();
}
}

Thank You So much Shakeel,

I forgot to add contentType in response.

:slight_smile:

Regards,
Gsam V

Hi,

Thanks for your feedback.

We are glad that your issue is now resolved by adding the contentType in response.

Let us know if you know any other issue, we will be happy to help you further.