当前位置: 代码迷 >> Java Web开发 >> ireport导出excel乱码解决方案
  详细解决方案

ireport导出excel乱码解决方案

热度:1186   发布时间:2013-02-25 21:14:04.0
ireport导出excel乱码
报表里的内容都是英文的,因为是加拿大的项目,现在我不理解我导出excel为什么乱码那,大家帮忙看看
Java code
public String branchExcelReport(List list) {        JasperPrint jasperPrintArr=null;        ArrayList jasperPrintList = new ArrayList();        HttpServletResponse resp=null;        JasperReport report;        Map map=new HashMap();        String realPath = "";        resp = (HttpServletResponse)ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);        realPath =ServletActionContext.getServletContext().getRealPath(                "/report/template/Branch.jasper");         JRDataSource dataSource = new JRBeanCollectionDataSource(list);        try {            report = (JasperReport)JRLoader.loadObject(realPath);                jasperPrintArr = JasperFillManager.fillReport (report,  map, dataSource);          jasperPrintList.add(jasperPrintArr);                //设定输出格式        FileBufferedOutputStream fbos = new FileBufferedOutputStream();         // 使用JRPdfExproter导出器导出pdf          JRPdfExporter exporter = new JRPdfExporter();          // 设置JasperPrintList          exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);          exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fbos);           exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);         exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);         exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);        exporter.exportReport();               resp.reset();               resp.setContentType("application/vnd.ms-excel;charset=UTF-8");               resp.setCharacterEncoding("UTF-8");            resp.setContentLength(fbos.size());             resp.setHeader("Content-Disposition", "attachment; filename=BRANCH.xls");            ServletOutputStream ouputStream = resp.getOutputStream();                         try            {                 fbos.writeData(ouputStream);                 fbos.dispose();                 ouputStream.flush();             }             finally             {                 if (ouputStream != null)                 {                     try                    {                         fbos.close();                         ouputStream.close();                     }                     catch (IOException ex)                     {                         ex.printStackTrace();                    }                 }             }     } catch (Exception e) {        // TODO Auto-generated catch block        e.printStackTrace();    }        return null;}


------解决方案--------------------------------------------------------
ireport的字符格式是不是不是utf-8的?
------解决方案--------------------------------------------------------
resp.setContentType("application/vnd.ms-excel;charset=UTF-8");
resp.setCharacterEncoding("UTF-8");
UTF-8是简体中文,设置成ISO-8859-1试试
------解决方案--------------------------------------------------------
乱码有两种是情况
首先确定编码格式是否正确.
1.为参数没全部传入
2.另外没有让流结束造成的。
如果是Linux下看报表还得在容器启动时加参数。
------解决方案--------------------------------------------------------

导出excel不用那么麻烦配置,直接在action中写一个方法就可以实现这个功能
代码:
public String action_executeExcel(){
try{

List<JWJX_JXdajbxx> list = teachDossierService.findAll();
getDmWjlxdmAll();
getDmBgqxAll();

//设置导出头部
HttpServletResponse response = ServletActionContext.getResponse();
response.reset();
response.setContentType("application/vnd.ms-excel");
String fileName = java.net.URLEncoder.encode("教学档案.xls", "UTF-8");
  相关解决方案