File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes application/vnd.ms-excel is not working for content type? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "application/vnd.ms-excel is not working for content type?" Watch "application/vnd.ms-excel is not working for content type?" New topic
Author

application/vnd.ms-excel is not working for content type?

yamini nadella
Ranch Hand

Joined: Apr 13, 2004
Posts: 257
Hi
In below script I used ("application/vnd.ms-excel") and ("application/msword"). But none is working. I mean it is showing in as plain text. It is not showing in Word or excel.
Do I need to do some parameter setup or any thing.

Thanks,
Yamini.


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ApplesAndOranges extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//response.setContentType("application/vnd.ms-excel");
response.setContentType("application/msword");
PrintWriter out = response.getWriter();
out.println("\traja1\trani\tsekhar");
out.println("\t10\t20\t30\t=SUM(B2:C2)");
}
}

output is - raja1 rani sekhar 10 20 30 =SUM(B2:C2)
Dave Brooks
Greenhorn

Joined: May 16, 2004
Posts: 3
You need to set the response header and specify the Content-Disposition.

Try this to get an Excel file created from your servlet.

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=myfile.xls");

Or for Word use:

response.setContentType("application/vnd.ms-word");
response.setHeader("Content-Disposition", "attachment; filename=myfile.doc");
Sivasundaram Umapathy
Ranch Hand

Joined: Aug 10, 2002
Posts: 360
Adding to dave's words, as word and excel files are binary files, please use binary stream(ServletResponse.getOutputStream()) rather than text stream(ServletResponse.getWriter())


Siva
Co-Author - SCMAD Exam Guide - ISBN:9780070077881
Author - Java certification success, Part 4: SCEA
Michael Zalewski
Ranch Hand

Joined: Apr 23, 2002
Posts: 168
Actually, I don't think it would be appropriate for yamini to use getOutputStream().

He (she?) is attempting to write a text file (tab delimited). Excel can open this file as a spreadsheet.

But you cannot put formulas into a tab delimited file. Or at least, if you do they don't come out as formulas in the spread sheet. The spreadsheet will show the forula '=SUM(B2:C2)' in a cell as though it were a string -- which is the only thing you can put into a tab delimited file for Excel.
yamini nadella
Ranch Hand

Joined: Apr 13, 2004
Posts: 257
Thanks, There is some progress. But execution of URL is opening seperate MS word window. I am expecting to open the word window as part of internet explorer.

The current code causing seperate window is

response.setContentType("application/msword");
response.setHeader("Content-Disposition", "attachment; filename=myfile.doc");
OutputStream out = response.getOutputStream();
out.write(new String("\traja\trani\tsekhar\n\r").getBytes());
out.write(new String("\t10\t20\t30\t=SUM(B2:C2)").getBytes());

(2)Another problem with setting header is

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=myfile.doc");

In above even I set content type as excel, if I set header as .doc then it is opening data in MS Word. It is contradictory.
----
below 3 and 4 codes causing output to open in MS Word. is it mean setContentType is not considered if Content-Disposition value is present.
(3)
//response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=myfile.doc");

(4)
response.setContentType("text/html");
response.setHeader("Content-Disposition", "attachment; filename=myfile.doc");
Balamurugan Muthiah
Greenhorn

Joined: Mar 14, 2002
Posts: 17
Hi Yamini

Internet Explorer is smart enough to work without providing content type. It uses the file extension and/or contents of the file to determine the content type. Also, this behaviour is inconsistent with the different versions of Internet Explorer and Windows Operating System. Netscape does expect you to provide content type. So it is a good practise to use response.setContentType always.

will open in the document in the same browser window.


Regards<br />Bala<br />SCJP 1.2, SCWCD 1.4
Sivasundaram Umapathy
Ranch Hand

Joined: Aug 10, 2002
Posts: 360
IE is not smart bala rather behaves in a confusing way across versions
mohan sengodan
Greenhorn

Joined: May 19, 2006
Posts: 1
I used the code as follows in jsp. still i can't get the excel o/p

<%!
RequestContext objRecCon = null;
HierListGroup objHLG = null;
public static Logger logger=Logger.getLogger("TEAMPAGES");
%>

<%

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition","attachment; filename=" + "projectdata.xls" );
response.setHeader("Cache-Control", "max-age=0");

try
{

objRecCon = (RequestContext) request.getAttribute(IWidgetsKeys.REQUEST_CONTEXT);

}
catch(Exception ex)
{
ex.printStackTrace();
}

Logger logger=Logger.getLogger("TEAMPAGES");
String strOutput = "";
try{

int lintTeamID = Integer.parseInt(request.getParameter("teamid"));
TeamTypeInfo info = new TeamTypeInfo();
String xmlStr = info.getTeamTypeInfo(lintTeamID,2,"true","true");
logger.debug("\n\n"+ xmlStr + "In: TPTeamInfo.jsp \n\n");
HashMap hmKey = new HashMap();

hmKey.put("Function",TPConstants.FUNCTION_TYPE);
hmKey.put("PhoneCommunication",TPConstants.PHONE_TYPE);
hmKey.put("Responsibilities",TPConstants.RESPONSIBILITY_TYPE);
hmKey.put("Addresses",TPConstants.ADDRESS_TYPE);
hmKey.put("PeoplePageURL",TPConstants.PEOPLE_PAGE_INFO_URL);



TPTransformer trans = new TPTransformer();
String xslFileName = "TPTeamInfo_excel.xsl";
strOutput = trans.doTransformation(xmlStr,xslFileName, hmKey);

//byte[] outPut = strOutput.getBytes();
//response.getOutputStream().write(outPut,0,outPut.length);

out.print(strOutput);

/*
if(strAction!=null && strAction.equalsIgnoreCase("DownloadExcel")){

try{
out.flush();
}

catch(IOException ioex)
{
logger.debug("Exception is : " + ioex.getMessage());
}

// Disable any further buffering inside the ResponseOutputStream

byte[] outPut = strOutput.getBytes();
response.getOutputStream().write(outPut,0,outPut.length);
}else{
out.print(strOutput);
}
*/

}catch(Exception e){
logger.debug(getClass().getName()+ " Exception is : " + e.getMessage());
}

%>
can any one help me to do the things
Ramji Yadav
Greenhorn

Joined: Jun 23, 2011
Posts: 1

Hi Mohan,

your above setHeader() is ok for the static header In case to give Dynamic Header means file name should be dynamic

Then file name must not contain space because it can not take file name with space character...


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: application/vnd.ms-excel is not working for content type?
 
Similar Threads
using jsp to generate excel file
response.setContentType not working in case of Excel
Table display as Excel file
Displaying the query results in Excel
Excel fails load to web