PL/SQL Web Toolkit to create downloadable UTF-8 with BOM document with Chinese content

 

I encountered a problem when generating a UTF-8 CSV document with chinese words using the following code:

DECLARE

v_blob          BLOB;

BEGIN

dbms_lob.createtemporary(v_blob, TRUE);

dbms_lob.append(v_blob, TO_BLOB(utl_raw.cast_to_raw(‘any text…中文字…’)));

wpg_docload.download_file(v_blob);

dbms_lob.freetemporary(v_blob);

END;

When using Microsoft Excel 2010 to open the file in Windows 7, the chinese words cannot be displayed correctly (Even notepad can display it correct) . After investigation, it is found that the Excel can only display chinese words correctly when the file is encoded using UTF-8 with BOM. Therefore, I did some experiments and add the BOM by myself with the following code:

DECLARE

v_blob          BLOB;

BEGIN

dbms_lob.createtemporary(v_blob, TRUE);

dbms_lob.append(v_blob, TO_BLOB(HEXTORAW(‘EFBBBF’))); — Add BOM at the beginning: HEXTORAW(‘EFBBBF’)

dbms_lob.append(v_blob, TO_BLOB(utl_raw.cast_to_raw(‘any text…中文字…’)));

wpg_docload.download_file(v_blob);

dbms_lob.freetemporary(v_blob);

END;

The BOM is magical!

You must be logged in to post a comment.