导出到Excel,PDF,CSV文件不支持阿拉伯语

时间:2011-09-28 12:55:31

标签: jsp tomcat localization export

我有tomcat6并在其中使用开放报告应用程序。 当我将任何报告导出为ex​​cel或pdf时,它不支持阿拉伯语。 请有人帮帮我吗? 你可以在下面的jsp页面代码中找到。


<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib uri="http://displaytag.sf.net" prefix="display" %>
<%@page import="org.efs.openreports.util.DisplayProperty"%>
<%@page import="org.efs.openreports.objects.Report"%>
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<s:include value="Banner.jsp" />
<s:if test="report == null || !report.isDisplayInline()">

<a class="back-link img-report-small" href="reportList.action"><s:text name="link.back.reports"/></a>
<a class="back-link img-group-small" href="reportGroup.action"><s:text name="link.back.groups"/></a>    

<br/>

<s:actionerror/>
    <div align="center">  
        <div class="important img-queryreport" id="instructions"><s:property value="report.name"/></div>  
    </div>
</s:if>

<div align="center">   

  <s:set name="results" value="results" scope="request" />  
  <s:set name="properties" value="properties" scope="request" />
  <s:set name="report" value="report" scope="request" />
  <% DisplayProperty[] displayProperties = (DisplayProperty[]) request.getAttribute("properties");
     request.setCharacterEncoding("UTF-8");
     Report report = (Report) request.getAttribute("report");%>

  <display:table name="results" class="displayTag" sort="list" export="true" pagesize="20" requestURI="queryReportResult.action?tab=PRODUCTIVITY" excludedParams="org.apache.struts.taglib.html.TOKEN">  
    <% for (int i=0; i < displayProperties.length; i++) { %>
      <display:column property="<%=displayProperties[i].getName()%>" title="<%=displayProperties[i].getDisplayName()%>" sortable="true" headerClass="sortable" />
    <% } %>
        <display:setProperty name="export.pdf" value="true"/> 
    <display:setProperty name="export.xml.filename" value="<%=report.getName() + ".xml"%>"/>
    <display:setProperty name="export.pdf.filename" value="<%=report.getName() + ".pdf"%>"/>
    <display:setProperty name="export.csv.filename" value="<%=report.getName() + ".csv"%>"/>
    <display:setProperty name="export.excel.filename" value="<%=report.getName() + ".xls"%>"/>
  </display:table>


  <s:if test="#session.user.scheduler">

    <s:text name="queryReport.scheduleReport"/>
    <a href="reportOptions.action?reportId=<%=report.getId()%>&submitSchedule=true&exportType=3">CSV</a> |
    <a href="reportOptions.action?reportId=<%=report.getId()%>&submitSchedule=true&exportType=1">Excel</a> |
    <a href="reportOptions.action?reportId=<%=report.getId()%>&submitSchedule=true&exportType=0">PDF</a>

  </s:if>  

</div>

<s:if test="report == null || !report.isDisplayInline()">

<s:include value="Footer.jsp" />

</s:if>

请快点帮我...

3 个答案:

答案 0 :(得分:1)

我在 JBoss 下使用从 Stripes MVC应用程序中挑选的 Displaytag 1.2 获得了类似的问题。

当数据库(MySQL)提供包含以UTF-8 编码的斯堪的纳维亚语言字符的字符串时,那些表格的CSV和Excel导出已损坏,即下载的文件不包含数据或小无用的损坏数据量。

我认为与数据相关的主要问题是芬兰字符编码,它们在ISO-8859-1中,但在UTF-8中没有(cfr。http://en.wikipedia.org/wiki/ISO/IEC_8859-1)。< / p>

对我有用的修正是:

  1. 将其添加到Stripes Action Bean:

    getContext().getResponse().setCharacterEncoding("UTF-8");
    getContext().getResponse().setContentType("application/vnd.ms-excel;charset=UTF-8");
    
  2. 将其添加到相应的JSP:

    <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
    
  3. 在此之后,我成功地从Displaytag表中下载CSV和Excel文件,该表包含用斯堪的纳维亚语言编码的UTF-8字符的数据。

答案 1 :(得分:0)

搜索后我找到了解决此案例的方法: 你应该添加
response.setContentType( “应用程序/ vnd.ms-Excel的;字符集= Cp1256”);
而不是:
request.setCharacterEncoding( “UTF-8”);
in&lt;%DisplayProperty .....&gt;线

获益

答案 2 :(得分:0)

我们正在使用DisplayTag 1.1.1,我们在Excel / CSV导出中遇到了特殊字符编码(UTF-8)的问题。经过一些搜索,我们能够通过在导出字符串之前添加BOM到TableTag.java类中的writeExport()来解决问题。

例如:

JspWriter lOut = pageContext.getOut();

...

lOut.write( '\ ufeff');

lOut.write(pExportString);

这很有效(正确显示特殊字符),除了在xls输出中,所有数据都显示在Excel 2010的一个单元格中,尽管CSV输出正确显示。 \ t - 添加BOM后,TAB分隔符无效。但是,如果我们用逗号(,)替换\ t,它在Excel 2010中按预期工作。