stephaneeybert
asked on
A servlet to output a .xls file
Dear all,
I have created a servlet that outputs an .xls spreadsheet on the fly.
When I call it on my Linux box, I have Open Office that is fired up.
But Open Office displays a filter popup window before opening the spreadsheet.
This popup window is a filter selector and asks me to choose which type of format to use.
I chose Open Office, or MS Excel, but whichever, I got the same message:
"Read error: unknown or unsupported Excel file format"
And I can view the file.
I must miss something when creating the spreadsheet.
Below is the servlet source code.
It's a demo dummy servlet.
I have created a servlet that outputs an .xls spreadsheet on the fly.
When I call it on my Linux box, I have Open Office that is fired up.
But Open Office displays a filter popup window before opening the spreadsheet.
This popup window is a filter selector and asks me to choose which type of format to use.
I chose Open Office, or MS Excel, but whichever, I got the same message:
"Read error: unknown or unsupported Excel file format"
And I can view the file.
I must miss something when creating the spreadsheet.
Below is the servlet source code.
It's a demo dummy servlet.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
can try this.........
may be some modification reruired according to your code............
.....................
response.setContentType("a pplication /vnd.ms-ex cel");
response.setHeader("Cache- Control", "public");
response.setHeader("Pragma ", "public");
response.setHeader("Expire s", "0");
response.setHeader("Conten t-disposit ion","atta chment; filename=\""+filename+"\"" );//this is optional line
HSSFWorkbook workBook = ............//your code
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
ServletOutputStream outStr = response.getOutputStream() ;
workBook.write(byteArrayOu t);
byte[] bytesOut = byteArrayOut.toByteArray() ;
response.setContentLength( bytesOut.l ength);
outStr.write(bytesOut);
outStr.flush();
may be some modification reruired according to your code............
.....................
response.setContentType("a
response.setHeader("Cache-
response.setHeader("Pragma
response.setHeader("Expire
response.setHeader("Conten
HSSFWorkbook workBook = ............//your code
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
ServletOutputStream outStr = response.getOutputStream()
workBook.write(byteArrayOu
byte[] bytesOut = byteArrayOut.toByteArray()
response.setContentLength(
outStr.write(bytesOut);
outStr.flush();
ASKER
Thanks for your postings!
I have found out about the problem...
I replaced the following line:
FileOutputStream fileOut = new FileOutputStream("workbook .xls");
with
OutputStream out = response.getOutputStream() ;
And it worked!
I also added the mime type in the web.xml file and the applicaton/vnd-ms.excel content type, although these were not the stumbloing block, as it seemed.
Since I made use of mrcoffee365 suggestions I feel he can have the points.
Thanks to you too chakra-march-04 for your suggestion.
Cheers!
I have found out about the problem...
I replaced the following line:
FileOutputStream fileOut = new FileOutputStream("workbook
with
OutputStream out = response.getOutputStream()
And it worked!
I also added the mime type in the web.xml file and the applicaton/vnd-ms.excel content type, although these were not the stumbloing block, as it seemed.
Since I made use of mrcoffee365 suggestions I feel he can have the points.
Thanks to you too chakra-march-04 for your suggestion.
Cheers!
Congratulations.
Glad we could help.
Glad we could help.
ASKER
import java.io.BufferedOutputStre
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.portlet.PortletExcep
import javax.portlet.RenderReques
import javax.portlet.RenderRespon
import javax.servlet.ServletExcep
import javax.servlet.http.HttpSer
import javax.servlet.http.HttpSer
import javax.servlet.http.HttpSer
import org.apache.poi.hssf.usermo
import org.apache.poi.hssf.usermo
import org.apache.poi.hssf.usermo
import org.apache.poi.hssf.usermo
import org.jfree.chart.JFreeChart
import org.springframework.beans.
import org.springframework.beans.
import org.springframework.core.i
import com.ericsson.pii.domain.Do
import com.ericsson.pii.domain.Do
import com.ericsson.pii.dto.Crite
import com.ericsson.pii.service.D
import com.ericsson.pii.service.D
import com.ericsson.pii.service.I
import com.ericsson.pii.service.D
import com.ericsson.pii.util.Sess
import com.ericsson.pii.util.Spri
public class DocumentListExcelGenerator
private static final long serialVersionUID = 1L;
BeanFactory beanFactory = null;
private DocumentListService documentListService;
protected void processRequest(HttpServlet
HttpServletResponse response) throws ServletException,
java.io.IOException {
request.getSession(false);
response.setContentType("a
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Document List");
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow((short)0);
// Create a cell and put a value in it.
HSSFCell cell = row.createCell((short)0);
cell.setCellValue(1);
// Or do it on one line.
row.createCell((short)1).s
row.createCell((short)2).s
row.createCell((short)3).s
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook
wb.write(fileOut);
fileOut.close();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
java.io.IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
java.io.IOException {
processRequest(request, response);
}
public String getServletInfo() {
return "Short description";
}
private Object getBean(String beanName) {
if (null == beanFactory) {
ClassPathResource classPathResource = new ClassPathResource("applica
beanFactory = new XmlBeanFactory(classPathRe
}
Object result = beanFactory.getBean(beanNa
return result;
}
}