我在将模型创建的 xls 书保存到硬盘时遇到问题。我正在使用弹簧AbstractExcelView
和
protected void buildExcelDocument(Map<String, Object> model,
HSSFWorkbook workbook, HttpServletRequest request,
HttpServletResponse response) throws Exception
问题是我无法让一个窗口弹出到用户屏幕,用户选择保存文件的位置并单击保存。
//This class makes convertation from Spring MVC model to excel document
public class PagetoExcelConverter extends AbstractExcelView{
List<FormDate> attributesList = null;
//Create massive of constants for making table header
private final String[] HEADER_NAMES_MASSIVE = {"Name", "Type", "Date", "Description"};
@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);
//Creating head for table by iterating over HEADER_NAMES_MASSIVE
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;
//Add data to table by iterating over and attributes
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());
dataCell.setCellStyle(dataCellstyle);
cellNumber++;
dataCell.setCellValue(attributesList.get(i).getParticularDate());
cellNumber++;
dataCell = dataRow.createCell(cellNumber);
dataCell.setCellValue(attributesList.get(i).getFormDescriptionList().get(i).getDaySchedule());
dataCell.setCellStyle(dataCellstyle);
cellNumber++;
dataCell = dataRow.createCell(cellNumber);
dataCell.setCellValue(attributesList.get(i).getFormDescriptionList());
cellNumber++;
}
attributesList = null;
}
}
所以,如果我把这段代码:
FileOutputStream fos = new FileOutputStream("/home/vadim/Desktop/mybook.xls");
workbook.write(fos);
它保存一个文件,我指向它保存。但我想为用户弹出一个小窗口。
我在 webapp/WEB-INF/classes 中的 view.properties:
#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
我在 servlet-context 中的 viewResolvers 声明:
<!-- 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>
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean id="jspviewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>