我正在尝试使用AbstractExcelView来提供我在服务器上创建的XLS文档。有没有办法可以将它与已经构建的工作簿一起使用?我尝试了以下但是它不起作用:
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);
}
}
答案 0 :(得分:4)
您不需要AbstractExcelView
来提供现有文档。
WEB-INF
下,您可以使用request.getRequestDispatcher(fileName).forward(request, response)
转发请求。response.getOutputStream()
但是,不要忘记设置正确的内容类型:response.setContentType("application/vnd.ms-excel");
答案 1 :(得分:3)
public class ValidationErrorsView extends AbstractExcelView {
public ValidationErrorsView() {
super();
setUrl("/WEB-INF/templates/1.1");
}
@Override
protected void buildExcelDocument(Map model, HSSFWorkbook workbook,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
}
}
“1.1”是现有的excel文件名。
工作簿 - 从1.1.xls文件创建。