Monday, 9 July 2018

Custom Excel Download with Chinese Characters (for languages other than English)

In APEX 4.2, reports with some Chinese and other language characters gets replaced with inverted question marks if downloaded. This issue can be fixed using the below method.

1 Create a new blank page in the application.
 Create a new blank page in the application using Customized template by removing all the contents in the page, like navigation menus, APEX page Toolbar, and others as the entire page will be downloaded as a native Excel Spreadsheet.
2 Create PLSQL Region with no template. Use your report’s Source query instead of the below mentioned query in the Loop.

      ('<table border=1 white-space=nowrap><tr style="background-color:silver;">
    <th>Direct Report Total</th></tr>'

   FOR i IN (SELECT company, title, val
/*Company Title will have the Chinese characters Ex: 冲渣冷却塔 Slag cooling tower*/
             FROM   company_report
              WHERE ID = :p12_c_id)
      HTP.p (   '<tr height=80><th align=right>'
             || '</th><th>'
             || i.title
             || '</th><th>'
             || i.val
             || '</th></tr>'
3 Create Before Header process to set HTTP Headers.

Create a Before Header Process in the page with the required filename as mentioned below.

   OWA_UTIL.mime_header ('application/', FALSE);
   HTP.prn ('Content-Disposition: attachment; filename="spreadsheet.xls"
'  );

4 Redirect the Download button in your report screen to this new page.

In the report screen, create a button Download with
Action-> Redirect to Page (new page created above with custom download process).


Default Download will return the Chinese characters in the excel file as below:
Original Characters         :   冲渣冷却塔 Slag cooling tower
Default Download          :   ????? Slag cooling tower
Using Custom Download    :   冲渣冷却塔 Slag cooling tower

Also, In Chinese characters, ‘&’ symbol must be escaped as these will not be accepted by default. Replace ‘&’ in the data with ‘&amp;’ in the select query to escape the ‘&’ symbol.

1 comment: