溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Birt支持自定義導(dǎo)出中文名稱

發(fā)布時間:2020-06-23 12:20:22 來源:網(wǎng)絡(luò) 閱讀:628 作者:genuinecx 欄目:軟件技術(shù)

BIRT 是以 Java 和 JavaEE 為基礎(chǔ)為 Web 應(yīng)用程序開發(fā)的基于 Eclipse 的開源報表系統(tǒng),雖然支持文件導(dǎo)出功能,但不支持導(dǎo)出時動態(tài)自定義文件名稱,尤其不支持中文。

Birt默認(rèn)提供了兩種導(dǎo)出文件時自定義名稱的方法,如下:

  • DefaultFilenameGenerator:報表文件名稱.后綴名
  • TimestampFilenameGenerator:報表文件名稱+yyyyMMdd-HHmmss.后綴名

Birt若實現(xiàn)自定義導(dǎo)出文件名稱,需要實現(xiàn)org.eclipse.birt.report.utility.filename包下的IFilenameGenerator接口,具體操作如下:

  1. 自定義實現(xiàn)IFilenameGenerator 接口
public class CustomExportGenerator implements IFilenameGenerator {
    @Override
    public String getFilename( String baseName, String fileExtension, String outputType, Map options) {
        return baseName  + "." + fileExtension;
    }
}

從上面的代碼看出,IFilenameGenerator 中的方法默認(rèn)沒有提供自定義文件名稱,這時候只能夠從Map options參數(shù)中獲取,當(dāng)然如果不嫌麻煩的話,也可以重寫getFilename接口。

Object obj = options.get("httpRequest");
    if(obj !=null && obj instanceof RequestFacade){
        RequestFacade request = (RequestFacade)obj;
        exportName = ParameterAccessor.getParameter(request,"__filename");
    }
  1. 配置Birt的導(dǎo)出文件生成類

修改web.xml

<context-param>
        <param-name>BIRT_FILENAME_GENERATOR_CLASS</param-name>
        <param-value>org.eclipse.birt.report.utility.filename.CustomExportGenerator</param-value>
    </context-param>
  1. 支持中文

文件名稱:org.eclipse.birt.report.presentation.aggregation.layout.FramesetFragment
方法: doPreService( HttpServletRequest request, HttpServletResponse response )
操作:

String filename = ParameterAccessor.getExportFilename( new BirtContext( request, response ), format, emitterId );
        String displayName = URLEncoder.encode(filename,"utf-8");
        displayName = displayName .replace("+","%20");
        response.setCharacterEncoding("utf-8");
        response.setHeader("contentType", "text/html; charset=utf-8");
        response.setHeader( "Content-Disposition", //$NON-NLS-1$
                ParameterAccessor.htmlEncode( openType )
                        + "; filename=\"" 
                        + ParameterAccessor.htmlEncode(displayName )
                        + "\"" ); 
  1. 修改Tomcat的編碼

&lt;Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/&gt;

  1. 訪問方式:
    frameset?report=test.rptdesign&format=docx&__filename=測試
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI