views:

211

answers:

1

I am trying to use AbstractExcelView to serve an XLS document that I create on a server. Is there a way I can use this with an already constructed workbook? I tried the following but it doesn't work:

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;


public class ValidationErrorsView  extends AbstractExcelView {

@SuppressWarnings("unchecked")
@Override
protected void buildExcelDocument(Map arg0, HSSFWorkbook workbook,     
 HttpServletRequest arg2, HttpServletResponse arg3) 
    throws Exception {

    final String toprocess = "myfile.xls";
    final InputStream is = new FileInputStream(toprocess);
    workbook = new HSSFWorkbook (is);
    }

}
A: 

You don't need an AbstractExcelView to serve an existing document.

  • If the document is in the public folders of your webapp, users can download it as a static file.
  • If it's under WEB-INF, you can forward request to it using request.getRequestDispatcher(fileName).forward(request, response).
  • If it's an arbitrary file, you should open the file and copy its content into response.getOutputStream()

However, don't forget to set the right content type: response.setContentType("application/vnd.ms-excel");

axtavt
Because I am storing the file in a non-web accessible loction, I am just copying it to the outputstream and setting the appropriate content type. Thanks!
Casey