Free Support Forum - aspose.com

Help regarding HelloWordServlet

Hi there,

I was trying to excute HelloWorldServlet program. I created a small application which contains a call btn. On click of the btn HelloWorlServlet is called. But the execution of the servlet stops at


workbook.open(template_file_path);

throwing a error

java.io.IOException: It is not a valid excel file.
at com.aspose.cells.Workbook.o(Unknown Source)
at com.aspose.cells.Workbook.open(Unknown Source)
at coreservlets.HelloWorldServlet.CreateHelloWorld(HelloWorldServlet.jav
a:84)

Hi,

Thanks for considering Aspose.

Well, your template excel file does not seems to be a native excel file. Please make sure that it is a valid and native excel file. Aspose.Cells supports to load/open native excel files (Excel97-Excel2007)Could you post your template file. here.

Thank you.

Thanks a lot for replying.

I think the flow of the program HelloWorldServlet is as follows..

1) Specifying virtual path at which the excel will be created

2) Creating a workbook

3) Opening the workbook

4) Storing data into the excel file

Pls correct me if i am wrong.

If the above flow is correct then how native file format will help?

Thanks

Hi,

I just want to make sure if your template "HelloWorld.xls" is a valid / native excel file and is not of other format like... a web html file etc. (you may confirm it opening it into notepad). And also make sure that the path (you are specifying) to the template file is correct. Your procedure is correct. Could you post your template file here.

Thank you.

We are getting following error message along with other errors

ÐÏࡱá!þÿ þÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ ÓÍaÉÀ=Xx'7Þ!8X"1ÈÿArial1È Arial1ÈÿÌArial1ÈÿÌArial1ÈÿÌArialàõÿÀ àõÿôÀ àõÿôÀ àõÿôÀ àõÿôÀ àõÿôÀ àõÿôÀ àõÿôÀ àõÿôÀ àõÿôÀ àõÿôÀ àõÿôÀ àõÿôÀ àõÿôÀ àõÿôÀ àÀ àÀ “€ÿ`…Sheet1… Sheet2Œ®ü<4Evaluation only. Created with Aspose.Cells for Java.Was well as expose the user to other legal recourse for collection and punitive damages.RAny violation of this provision shall require a mandatory purchase of pay license KIt is strictly prohibited from using it in the production of any software. ÿ Ê ÓÍaÉÀ  dü©ñÒMbP?_*+€%ÿÁƒ„¡" d,,à?à?U>¶@¶@<D™5 þÿùOhòŸ…àÙ³?+‘«0°þÿœ.eÕÍÕ®ù,+—“0°a þÿÿÿþÿÿÿþÿÿÿRoot Entryÿÿÿÿÿÿÿÿ ÀFÀaWorkbookÿÿÿÿÿÿÿÿ¦SummaryInformation(ÿÿÿÿÿÿÿÿeHDocumentSummaryInformation8ÿÿÿÿÿÿÿÿÿÿÿÿHþÿÿÿþÿÿÿþÿÿÿýÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ


java.io.IOException: It is not a valid excel file. at com.aspose.cells.Workbook.o(Unknown Source) at com.aspose.cells.Workbook.open(Unknown Source) at coreservlets.HelloWorldServlet.CreateHelloWorld(HelloWorldServlet.java:85) at coreservlets.HelloWorldServlet.doGet(HelloWorldServlet.java:39) at javax.servlet.http.HttpServlet.service(HttpServlet.java:689) at javax.servlet.http.HttpServlet.service(HttpServlet.java:802) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869) at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664) at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527) at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80) at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684) at java.lang.Thread.run(Thread.java:595)

Hi,

Please post your template file here as an attachment, we will check and test it soon.

Sure..

package coreservlets;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.SQLException;
import com.aspose.cells.*;
import com.aspose.cells.Chart;
import org.apache.log4j.*;
import org.apache.log4j.Logger;

public class HelloWorldServlet extends HttpServlet
{
//ServletContext sc = getServletContext();
ServletContext sc;
private static final String TEMPLATE_FILE_PATH_PART = "/WEB-INF/Designer/Workbooks/HelloWorld.xls";
Logger logger;

public void init(ServletConfig config) throws ServletException
{
super.init(config);
sc = config.getServletContext();
logger = Logger.getLogger("HelloWorldServlet.class");
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
OutputStream out = response.getOutputStream();
RequestDispatcher rd = sc.getRequestDispatcher("/errorpage.jsp");
try
{
ServletContext sc = getServletContext();
String template_file_path = sc.getRealPath(TEMPLATE_FILE_PATH_PART);

System.out.println("***** template_file_path *****"+template_file_path);

setResponseHeader(response, "HelloWorld.xls");
CreateHelloWorld(out, template_file_path);
System.out.println("Calling CreateHelloWorld()");

rd = sc.getRequestDispatcher("/ExcelForm.jsp");
}
catch (Exception ex)
{
ex.printStackTrace();

// say sorry
response.setContentType("text/html");
PrintWriter writer = new PrintWriter(out);

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

Sorry, the demo programme failed for some reason.

");
ex.printStackTrace(writer);
writer.println("");
writer.println("");
writer.close();
}
}

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

private void setResponseHeader(HttpServletResponse response, String resultXLS)
{
System.out.println("setResponseHeader Start");
response.setContentType("application/vnd.ms-excel");
response.addHeader("content-disposition", "inline; filename=" + resultXLS);
System.out.println("setResponseHeader End");
}

private void CreateHelloWorld(OutputStream out, String template_file_path) throws IOException
{
Workbook workbook = new Workbook();
System.out.println("Crating workbook");
System.out.println("template_file_path :"+template_file_path);

workbook.open(template_file_path);
System.out.println("opening workbook");

CreateStaticReport(workbook);
System.out.println("after creating workbook");

//Saving the Excel file
workbook.save(out);
}


private void CreateStaticReport(Workbook workbook)
{
Worksheets worksheets = workbook.getWorksheets();
Worksheet worksheet = worksheets.getSheet(0);
Cells cells = worksheet.getCells();
Cell cell;

//Put a string value into the cell using its name
cell = cells.getCell("A1");
cell.setValue("Cell Value");

//put a string value into the cell using its name
cell = cells.getCell("A2");
cell.setValue("Hello World");

//Put an boolean value into the cell using its name
cell = cells.getCell("A3");
cell.setValue(true);

//Put an int value into the cell using its name
cell = cells.getCell("A4");
cell.setValue(100);

//Put an double value into the cell using its name
cell = cells.getCell("A5");
cell.setValue(2856.5);

//Put an object value into the cell using its name
Object obj = "Aspose";
cell = cells.getCell("A6");
cell.setValue(obj);

//Put an datetime value into the cell
Calendar calendar = Calendar.getInstance();
cell = cells.getCell("A7");
cell.setValue(calendar);
Style style = cell.getStyle();
style.setNumber(14);
cell.setStyle(style);

//Put a string value into the cell using its row and column
cell = cells.getCell(0,1);
cell.setValue("Cell Value Type");
int temp ;
for (int i = 1; i < 7; i ++)
{
temp = cells.getCell(i, 0).getValueType();
cell = cells.getCell(i, 1);
switch (temp)
{
case CellValueType.NULL:
cell.setValue("Null");
break;
case CellValueType.BOOLEAN:
cell.setValue("Boolean");
break;
case CellValueType.DATETIME:
cell.setValue("Calendar");
break;
case CellValueType.STRING:
cell.setValue("String");
break;
case CellValueType.INT:
cell.setValue("Integer");
break;
case CellValueType.DOUBLE:
cell.setValue("Double");
break;
case CellValueType.RICH_TEXT_STRING:
cell.setValue("String");
break;
}
}
}

}

Hi,

I have tested your servlet, it worked fine, the attachments are my test template file and the result file saved at client.

For your issue, I think you can try following steps:

1. check your template excel file first to make sure it is an excel file coincides with its file extension, that is, xls.

2. If the excel file is ok, then please check the file path on which invoked Workbook.open() method, make sure the template excel file exists in this path and is available to your web server.

If the problem still persists, please post your excel template file here as attachment and we will check it.

Hi,

Here is my template file at server side.