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.

Steps:
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.

BEGIN
   HTP.p
      ('<table border=1 white-space=nowrap><tr style="background-color:silver;">
    <th>Company</th><th>Title</th>
    <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)
   LOOP
      HTP.p (   '<tr height=80><th align=right>'
             || i.company
             || '</th><th>'
             || i.title
             || '</th><th>'
             || i.val
             || '</th></tr>'
            );
   END LOOP;
END;
3 Create Before Header process to set HTTP Headers.

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



BEGIN
   OWA_UTIL.mime_header ('application/vnd.ms-excel', FALSE);
   HTP.prn ('Content-Disposition: attachment; filename="spreadsheet.xls"
'  );
   OWA_UTIL.http_header_close;
END;

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).

Output:

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

Note:
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.

No comments:

Post a Comment