我想生成一个excel
文件:
...
<a href="<c:url value='/exportExcel' />">Export</a>
...
// inside controller :
@RequestMapping(value = "/exportExcel", method = RequestMethod.GET)
public ModelAndView exportExcel(HttpServletRequest request) {
return new ModelAndView("excelExport");
}
servlet-context.xml :
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<beans:bean id="viewResolver1" class="org.springframework.web.servlet.view.XmlViewResolver">
<beans:property name="order" value="1"/>
<beans:property name="location" value="/WEB-INF/views.xml"/>
</beans:bean>
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean id="viewResolver2"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="order" value="2"/>
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<beans:property name="maxUploadSize" value="300000000"/>
</beans:bean>
<context:component-scan base-package="com.ambre.pta" />
</beans:beans>
views.xml :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="excelExport" class="com.ambre.pta.view.ExcelExportView" />
</beans>
ExcelExportView类:
public class ExcelExportView extends AbstractXlsxView {
@Autowired
private ExportationDAO exportationDao;
@Override
protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setHeader("Content-Disposition", "attachment; filename=\"export.xlsx\"");
for(Object[] param : exportationDao.getAllParams()) {
Sheet sheet = workbook.createSheet(String.valueOf(param[1]));
Row header = sheet.createRow(0);
List<DetailsParamExport> entetes = exportationDao.getAllDetailsParams(Integer.parseInt(String.valueOf(param[0])));
int enteteIdx = 0;
for (DetailsParamExport detail : entetes) {
header.createCell(enteteIdx).setCellValue(detail.getEntete());
enteteIdx++;
}
List<Object[]> rows = exportationDao.getSqlResults(String.valueOf(param[3]));
int rowIdx = 1;
for (Object[] row : rows) {
Row excelRow = sheet.createRow(rowIdx++);
for (int p=0; p<enteteIdx; p++) {
excelRow.createCell(p).setCellValue("cccccccc");
}
}
}
}
}
在运行时单击“导出”链接,然后我得到空白页面,并且不会出现下载框。那有什么不对?
答案 0 :(得分:1)
你正在注入一个Spring bean
enum DAY
{
sunday = 0,
monday,
tuesday,
wednesday, ****/* wednesday is associated with 3 as Sunday is 0*/****
thursday,
friday
} workday;
在Spring类的非托管内部@Autowired
private ExportationDAO exportationDao;
。所以当你调用ExcelExportView
时,你可能会得到一个空指针异常。你检查了你的日志吗?
您可以将exportationDao.getAllParams()
数组从控制器传递到params
,并且不要在view
exportationDao
您的控制器:
ExcelExportView
您的ExcelExportView:
@Autowired
private ExportationDAO exportationDao;
@RequestMapping(value = "/exportExcel", method = RequestMethod.GET)
public ModelAndView exportExcel(HttpServletRequest request) {
ModelAndView mv = new ModelAndView("excelExport");
modelAndView.addObject("params", exportationDao.getAllParams());
return mv;
}