Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
wei tang
How to output excel in control panel portlet
December 28, 2012 1:44 PM
Answer

wei tang

Rank: Junior Member

Posts: 36

Join Date: December 3, 2012

Recent Posts

I use jxl.jar to export excel. The excel file encoding is messy code when I use Firefox and Chrom. It work fine in IE9. WHY?

CODE:

OutputStream outputStream = null;
WritableWorkbook book = null;
ByteArrayOutputStream bos = null;
try
{
System.out.println("00000000000000000000000000000000000");
PortletContext pc = renderRequest.getPortletSession().getPortletContext();

/*
* HttpServletResponse httpResponse =
* PortalUtil.getHttpServletResponse(renderResponse);
* httpResponse.setContentType("application/vnd.ms-excel");
* httpResponse.flushBuffer();
*/
HttpServletResponse response = PortalUtil.getHttpServletResponse(renderResponse);
response.reset();
response.setHeader("Content-disposition", "attachment; filename=" + new String("Book1".getBytes("UTF-8"), "UTF-8") + ".xls");
response.setContentType("application/vnd.ms-excel");
renderResponse.addProperty(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + new String("Book1".getBytes("UTF-8"), "UTF-8") + ".xls");

//outputStream = renderResponse.getPortletOutputStream();//doesn't work
outputStream = response.getOutputStream();//doesn't work

System.out.println("1111111111111111111111111111111111111");
System.out.println(pc.getRealPath("")+"test.xls");

WorkbookSettings settings = new WorkbookSettings();
settings.setEncoding("utf-8");
book = Workbook.createWorkbook(outputStream,settings);//unreadable code, include page html element
//book = Workbook.createWorkbook(new File(pc.getRealPath("")+"test.xls"));// it's work fine

WritableSheet sheet = book.createSheet(" 第一页 ", 0);


Label label = new Label(0, 0, " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ");


sheet.addCell(label);


jxl.write.Number number = new jxl.write.Number(1, 0, 555.12541);
sheet.addCell(number);

System.out.println("22222222222222222222222222222222222222222");


book.write();
book.close();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (bos != null)
bos.close();
}
Rewati Raman
RE: How to output excel in control panel portlet
December 28, 2012 9:12 PM
Answer

Rewati Raman

Rank: Junior Member

Posts: 97

Join Date: February 24, 2012

Recent Posts

Hi wei tang,

I had also generated xls file but i had used poi.jar and my code is working fine in all browser...

here is my code

 1
 2HSSFWorkbook workbook = new HSSFWorkbook(); 
 3/*******************workbook  filling statements***************/
 4resourceResponse.setContentType("application/vnd.ms-excel");               
 5resourceResponse.addProperty(HttpHeaders.CACHE_CONTROL, "max-age=3600, must-revalidate");
 6resourceResponse.addProperty(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=Report.xls");
 7OutputStream out = resourceResponse.getPortletOutputStream();
 8workbook.write(out); 
 9out.flush();
10out.close();   


Regards,
Rewati Raman
Hitoshi Ozawa
RE: How to output excel in control panel portlet
December 29, 2012 1:08 AM
Answer

Hitoshi Ozawa

Rank: Liferay Legend

Posts: 7949

Join Date: March 23, 2010

Recent Posts

Probably because IE uses different method to determine encoding.
wei tang
RE: How to output excel in control panel portlet
December 29, 2012 2:22 AM
Answer

wei tang

Rank: Junior Member

Posts: 36

Join Date: December 3, 2012

Recent Posts

Rewati Raman:
Hi wei tang,

I had also generated xls file but i had used poi.jar and my code is working fine in all browser...

here is my code

 1
 2HSSFWorkbook workbook = new HSSFWorkbook(); 
 3/*******************workbook  filling statements***************/
 4resourceResponse.setContentType("application/vnd.ms-excel");               
 5resourceResponse.addProperty(HttpHeaders.CACHE_CONTROL, "max-age=3600, must-revalidate");
 6resourceResponse.addProperty(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=Report.xls");
 7OutputStream out = resourceResponse.getPortletOutputStream();
 8workbook.write(out); 
 9out.flush();
10out.close();   


Regards,
Rewati Raman



great! use poi is work fine, thx!