• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");
 
Ranch Hand
Posts: 360
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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())
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sivasundaram Umapathy
Ranch Hand
Posts: 360
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IE is not smart bala rather behaves in a confusing way across versions
 
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


 
He's giving us the slip! Quick! Grab this tiny ad!
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic