Foren
Export data to excel file
Raj K, geändert vor 13 Jahren.
Export data to excel file
Regular Member Beiträge: 214 Beitrittsdatum: 19.06.09 Neueste Beiträge
I am trying to export my data in a excel file and open a popup dialog for the client browser.
The issue is after successful creation of the xls file at the given path, while it tries to open the popup dialog - it displays some junk data in the portlet itself instead of opening a popup.
I am using jxl to create a excel file.
Is there any special setting to do in a portlet to open the save popup dialog?
The issue is after successful creation of the xls file at the given path, while it tries to open the popup dialog - it displays some junk data in the portlet itself instead of opening a popup.
I am using jxl to create a excel file.
Is there any special setting to do in a portlet to open the save popup dialog?
[color=#63806A]// creates the excel on harddrive[/color]
CreateExcel createExcel = new CreateExcel();
createExcel.write(rb, list);
// reset the response
response.reset();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=sampleName.xls");
response.setHeader("Expires", "0");
// Get streams
fileInputStream = new FileInputStream("fileLocation");
servletOutputStream = response.getOutputStream();
// Init byte count and array
int bytesRead = 0;
byte byteArray[] = new byte[4096];
// Read in bytes through file stream, and write out through servlet stream
while((bytesRead = fileInputStream.read(byteArray)) != -1) {
servletOutputStream.write(byteArray, 0, bytesRead);
Gustavo Fernández Gómez, geändert vor 13 Jahren.
RE: Export data to excel file
Regular Member Beiträge: 191 Beitrittsdatum: 26.10.07 Neueste Beiträge
what kind of url are you creating?. I think it should be a ResourceUrl type.
http://developers.sun.com/portalserver/reference/techart/jsr286/jsr286_2.html
http://developers.sun.com/portalserver/reference/techart/jsr286/jsr286_2.html
Tina Agrawal, geändert vor 13 Jahren.
RE: Export data to excel file
Expert Beiträge: 297 Beitrittsdatum: 03.01.08 Neueste Beiträge
Hi,
You would need to overwrite the serveResource method.
I am attaching the sample code for that -
You would need to overwrite the serveResource method.
I am attaching the sample code for that -
public void serveResource(ResourceRequest req, ResourceResponse res)
throws PortletException, IOException {
InputStream in = null;
OutputStream out = null;
try {
.....
String contentType = MimeTypesUtil.getContentType(path);
res.setContentType(contentType);
res.addProperty(HttpHeaders.CACHE_CONTROL, "max-age=3600, must-revalidate");
out = res.getPortletOutputStream();
String shortFileName = FileUtil.getShortFileName(path);
res.addProperty(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + shortFileName + "\"");
if (in == null) {
out.close();
}
else {
byte[] buffer = new byte[contentLength];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.flush();
}
}
} catch (PortalException e) {
logger.error("Portal Exception " + e);
} catch (SystemException e) {
logger.error("SystemException " + e);
} catch (Exception e) {
logger.error("Exception " + e);
}
finally{
in.close();
out.close();
}
}
Raj K, geändert vor 13 Jahren.
RE: Export data to excel file
Regular Member Beiträge: 214 Beitrittsdatum: 19.06.09 Neueste Beiträge
I resolved it by calling a servlet.