大家好。我是 Spring 的新人,也是这里的新人。
我有问题。我有使用 Apache POI 创建 xls 文档的类(class):
public class PagetoExcelConverter extends AbstractExcelView{
List<FormDate> attributesList = null;
//Create massive of constants for making table header
private final String[] HEADER_NAMES_MASSIVE = {"HEADER1", "HEADER2", "HEADER3"};
@SuppressWarnings("unchecked")
@Override
protected void buildExcelDocument(Map<String, Object> model,
HSSFWorkbook workbook, HttpServletRequest request,
HttpServletResponse response) throws Exception {
//Creating new instance of ArrayList for add model attributes to
attributesList = new ArrayList<FormDate>();
//Adding model attributes to ArrayList
attributesList.addAll((List<FormDate>)model.get("findAttributes"));
//Creating sheet inside of book with given name
Sheet sheet = workbook.createSheet("Result");
sheet.autoSizeColumn(0);
//Making first row as a header
Row headerRow = sheet.createRow(0);
for(int i=0; i<HEADER_NAMES_MASSIVE.length; i++) {
Cell headCell = headerRow.createCell(i);
headCell.setCellValue(HEADER_NAMES_MASSIVE[i]);
headCell.setCellStyle(headCellstyle);
}
int rowNumber=1;
for(int i=0; i<attributesList.size(); i++) {
Row dataRow = sheet.createRow(rowNumber);
Cell dataCell;
int cellNumber=0;
dataCell = dataRow.createCell(cellNumber);
dataCell.setCellValue(attributesList.get(i).getFormDescriptionList().get(i).getInstitutions().getNameOfInstitution());
cellNumber++;
dataCell = dataRow.createCell(cellNumber);
dataCell.setCellValue(attributesList.get(i).getFormDescriptionList().get(i).getInstitutionType().getTypeOfInstitution());
cellNumber++;
dataCell = dataRow.createCell(cellNumber);
dataCell.setCellValue(attributesList.get(i).getParticularDate().toString());
cellNumber++;
rowNumber++;
FileOutputStream fos = new FileOutputStream("/home/vadim/Desktop/mybook.xls");
workbook.write(fos);
}
attributesList = null;
}
}
在我的 servlet 上下文中,我有:
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/classes directory. Goes first -->
<beans:bean id="xlsviewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<beans:property name="order" value="1" />
<beans:property name="basename" value="views"/>
</beans:bean>
在我的 Controller 类中,我有方法:
@RequestMapping(value="/result", method=RequestMethod.POST, params="asexcel")
public String resultXLS(@RequestParam String particularDate,
@RequestParam String institutionName,
@RequestParam String institutionType, Model model) throws Exception {
if((!particularDate.equals("")) && !institutionName.equals("") && institutionType.equals("")) {
model.addAttribute("findAttributes", educationWebService.fetchByDateAndNameService(dateConvertation(particularDate), institutionName));
} else if((!particularDate.equals("")) && (institutionName.equals("")) && (!institutionType.equals(""))) {
model.addAttribute("findAttributes", educationWebService.fetchByDateAndTypeService(dateConvertation(particularDate), institutionType));
} else if((!particularDate.equals("")) && institutionName.equals("") && institutionType.equals("")) {
model.addAttribute("findAttributes", educationWebService.fetchByDateService(dateConvertation(particularDate)));
} else {
throw new Exception("Exception occurs because it's not correspond to any case in controller");
}
return "xlspage";
}
问题是它不保存从模型数据中获取的新创建的文件。相反,它保存一些完全不同的文件,看起来像 TEXT/HTML
而不是 xls。当我打开此文件时,尝试打开浏览器并引导我访问网址。当我添加到我的 PagetoExcelConverter
类时:
FileOutputStream fos = new FileOutputStream("/home/vadim/Desktop/mybook.xls");
workbook.write(fos);
它正确保存了所有内容,我的意思是它使用我不需要的 TXT/HTML
保存文件,并在我指向的位置保存 xls。我需要从浏览器中为用户弹出一个小窗口,以便用户有机会保存在特定位置。你能帮我一下吗?
添加了对 buildExelDocument() 的调用:
#This view property triggered from org.springframework.web.servlet.view.ResourceBundleViewResolver for xls converting
#Here is xlspage is name of the jsp page, is tied in with (class) with do converting model to xls
xlspage.(class)=edu.demidov.service.PagetoExcelConverter
最佳答案
有一些关于将 POI 与 Spring MVC 结合使用的教程。
Mark Serrano 在此演示了一个简单的用法 - http://krams915.blogspot.in/2011/02/spring-3-apache-poi-hibernate-creating.html .
我的博客中使用 BO 和 DAO 演示了如何使用 Spring MVC 生成 excel 模板并将填充到 excel 文件中的数据导入到您自己的数据库中的稍微复杂的版本 - http://avik-ganguly.blogspot.in/2013/06/import-bulk-data-tutorial-apache-poi.html .
希望这有帮助。
关于java - Spring MVC 和 Apache POI 创建 xls 文档。无法保存模型中新创建的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17898153/