我尝试创建文档(excel / word),然后将其发送到浏览器进行下载。
该文件是在eclipse目录中创建的,但我没有收到保存/下载文件的对话框。相反,我有像#34; ������RootEntry���������Workbook������������������������������������A����\phpB�a=� = H:#8X @" 1Arial1Arial1Arial1Arial" $"#,## 0 _);(& #34; $"#,## 0)" $"#,## 0 _); [红色] 0); (*" - " ); (@ )5)0 _(" $" *#,## 0 _); ("����� ��������������`�bSheet0���一些文字�D��Xd����MbP? * +��%������ " d ,,�?�?U���">�@��"
这是(excel)的代码:
public void exportResults_EXCEL(ActionRequest actionRequest, ActionResponse actionResponse)throws IOException,PortletException{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
cell.setCellValue("Some text");
// write it as an excel attachment
ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
wb.write(outByteStream);
ByteArrayInputStream excelStream = new ByteArrayInputStream(outByteStream.toByteArray());
byte [] outArray = outByteStream.toByteArray();
HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);
HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=something.xls");
response.setContentLength(outArray.length);
ServletResponseUtil.sendFile(request, response, "something.xls" ,outArray, ContentTypes.APPLICATION_VND_MS_EXCEL);
FileOutputStream output = new FileOutputStream(new File("something.xls"));
ServletOutputStream out = response.getOutputStream();
wb.write(output);
output.flush();
output.close();
}
P.S!我使用eclipse,liferay,poi
答案 0 :(得分:2)
对于任何挣扎于此的人,它与liferay有关,因为我没有在actionURL中指定windows状态,因为我正在使用弹出窗口。
<portlet:actionURL var="exportURL" name="exportEXCEL" windowState="<%= LiferayWindowState.POP_UP.toString() %>">
并在java中将文件发送到浏览器下载:
ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
wb.write(outByteStream);
byte [] outArray = outByteStream.toByteArray();
HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);
HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse);
ServletResponseUtil.sendFile(request, response, "result.xls" ,outArray, ContentTypes.APPLICATION_VND_MS_EXCEL);