Monday, August 27, 2007

Preserve Leading Zeros in ApEx CSV Export

I located a method of preserving leading zeros on values when exporting a report region to CSV in HTML DB (ApEx).

When Excel opens up a CSV file it defaults all cells to "General" formatting and attempts to "help the user" by removing leading zeros on cell values and converting them from strings to numbers. That can be a problem for values like product codes (SKUs) that begin with a zero.

The trick is to SELECT the value such that Excel will see it as a string and not a number. So prefix an equals sign and double quote on to the value and post fix a double quote. So

SELECT your_column
FROM your_table

becomes

SELECT '="'||your_column||'"'
FROM your_table

That is great except the users now see the additional characters in the report region. To get a round this use the following SELECT statement:

SELECT your_column, '="'||your_column||'"' quoted_column
FROM your_table

and put a "PL/SQL Express" condition (based on Sergio's blog entry) on each column. The conditional display express for YOUR_COLUMN would be:

INSTR(NVL(:REQUEST,'FOO'),'FLOW_EXCEL') = 0

The conditional display express for QUOTED_COLUMN would be:

INSTR(NVL(:REQUEST,'FOO'),'FLOW_EXCEL') != 0

To get even more customized CSV output look at Scott's blog entry.

2 comments:

Patrick Wolf said...

Would be useful to have that as default or as additional option for the CSV export. Maybe you should post it to the 3.1 enhancement thread on the forum.

Thanks for the useful tip!
Patrick

Anonymous said...

I will not acquiesce in on it. I over warm-hearted post. Expressly the title-deed attracted me to study the unscathed story.