0

I have a Customer.jsp file on the server to send the content in an Excel file. I have kept the simple text "TestContent" to help me debug this.

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

TestContent

When I download the file on the client side open, Excel(version 2013 or 2016) opens the file, but I get the following message:

The file format and extension of Test.xls don't match. The file could be corrupted or unsafe. Unless you trust its source, don't open it. Do you want to open it anyway?

I have also tried to set the header

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` 

and file format as .xlsx, but it also doesn't help because the warning still appears.


To address the duplicate notice, in the linked answer the author says to not use plain HTML tables with an application/vnd.ms-excel content type. However, because I'm not using any html content, so how would that solution affect my situation (removing the warning)? I have padded all the request headers and content.

Also the target question asks

Is it possible under tomcat to just write a file as if it was a normal Java application [...] ?

Community
  • 1
  • 1
user3198603
  • 5,528
  • 13
  • 65
  • 125
  • You are having a excel file which is not a excel file. how are you creating the file? – seenukarthi Oct 24 '16 at 12:36
  • `You are having a excel file which is not a excel file` I did not get what do you mean here ? – user3198603 Oct 24 '16 at 12:40
  • That means the file extension is of an excel file but not the file itself. – seenukarthi Oct 24 '16 at 12:41
  • Are you saying that filename is `Customer.jsp` but its sent as Test.xls ? – user3198603 Oct 24 '16 at 12:44
  • You cannot sent the content `TestContent` as content because excel files has some format. You can use Apache Poi to create the excel file. – seenukarthi Oct 24 '16 at 12:46
  • Is your Excel file generated or is it a file at server side and you send it as is? If generated, you should first check, whether the problem lies in generationg the Excel file or sending the generated file. E.g. you could write a unit test for generating and ensure the correctness of the generated file. And can you show the JSP's part where you are actually sending the Excel document? And why do you use a JSP instead of a pure servlet? JSP technology is more in the way instead of helping here. – vanje Oct 24 '16 at 12:53
  • @BalusC this question is not duplicate by to the link you pointed. In your answer you said `Don't use plain HTML tables with an application/vnd.ms-excel content type..` but as i said in my post i am not using any html content then why warning is thrown here. I have padted all the request headers and content. Request you to reopen this question – user3198603 Oct 24 '16 at 14:30
  • AFAIK a string of "TestContent" is not a valid Excel file. It's throwing a warning because you're not sending an Excel file: just setting headers and giving it the correct extension doesn't mean it's a valid Excel file. Just like if I put random text into a file named "xxx.jpg" doesn't make it a valid JPEG. – Dave Newton Oct 24 '16 at 15:34
  • @DaveNewton even i do not send any content(remove `TestConent`), it same warning that appears ? – user3198603 Oct 25 '16 at 05:08
  • An empty file is also not likely to be a valid Excell file. I'm not sure what other words to use. – Dave Newton Oct 25 '16 at 10:43
  • So where are we at with this question? Resolved? – Drew Oct 29 '16 at 03:19
  • @Drew Not resolved yet – user3198603 Oct 30 '16 at 13:24

0 Answers0