モデルから作成した xls ブックをハード ドライブに保存する際に問題があります。私はSpringsを使用して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
サーブレット コンテキストでの私の 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>