function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anand@SAASAnand@SAAS 

Save to CSV/Excel in visual force with UTF 8 chracters

I'm trying to create  visual force page that downloads as Excel or CSV . This works fine for English but for UTF 8 characters like Polish etc. some of the characters get replaced with "?" etc. I'm not sure there's a way to specify encoding when using contentTypes.

Best Answer chosen by Admin (Salesforce Developers) 
crop1645crop1645

This worked for me:

 

 

<apex:page controller="MyCustomController" cache="true" contenttype="application/x-excel#filename.xls" showheader="false">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
</head>
	<apex:pageBlock >
	    <apex:pageBlockTable value="{!allRecords}" var="line" columns="3">  
          	<apex:column headerValue="Col A">{!line.fieldA}</apex:column>
          	<apex:column headerValue="Col B">{!line.fieldB}</apex:column>
          	<apex:column headerValue="Col C">{!line.fieldC}</apex:column>
		</apex:pageBlockTable>
	</apex:pageBlock>
</apex:page>

 Trick is showheaders="false" and insert your own HTML <head> tag with the character encoding you are emitting. Otherwise, the default, at least for me in the US, was ISO 8859-1 which will not display UTF-8 characters above codepoint 7F successfully - limiting you to A-Z, a-z, 0-9, and common US/UK punctuation.  My inspiration for this was here.

 

Tested on IE8, Firefox, Chrome.

 

Excel appears to coerce the font to Arial Unicode MS 10pt which is a good font for UTF-8. 

 

All Answers

Anand@SAASAnand@SAAS

The issue is that when you use application.vnd-excel, and the page has some UTF-8 characters (polish, hungarian etc.) and the downloaded excel doe'snt render the characters correctly.

ahab1372ahab1372

I suppose the problem could be Excel. Maybe it does not evaluate the content type of the html page (because that is what it is).

 

I guess you can check by saving the file to disk, then from an Excel window, open or import the html file. I think the import lets you specify the character encoding. If it then displays the data correctly, it ignores the encoding when you open it directly.

 

Also check the font you are using, try changing it to the Arial UTF font

crop1645crop1645

This worked for me:

 

 

<apex:page controller="MyCustomController" cache="true" contenttype="application/x-excel#filename.xls" showheader="false">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
</head>
	<apex:pageBlock >
	    <apex:pageBlockTable value="{!allRecords}" var="line" columns="3">  
          	<apex:column headerValue="Col A">{!line.fieldA}</apex:column>
          	<apex:column headerValue="Col B">{!line.fieldB}</apex:column>
          	<apex:column headerValue="Col C">{!line.fieldC}</apex:column>
		</apex:pageBlockTable>
	</apex:pageBlock>
</apex:page>

 Trick is showheaders="false" and insert your own HTML <head> tag with the character encoding you are emitting. Otherwise, the default, at least for me in the US, was ISO 8859-1 which will not display UTF-8 characters above codepoint 7F successfully - limiting you to A-Z, a-z, 0-9, and common US/UK punctuation.  My inspiration for this was here.

 

Tested on IE8, Firefox, Chrome.

 

Excel appears to coerce the font to Arial Unicode MS 10pt which is a good font for UTF-8. 

 

This was selected as the best answer
Anand@SAASAnand@SAAS

Eric..excellent suggestion. The "Meta" tag in the document seems to be doing the trick. 

baller4life7baller4life7

Hi guys,

I tried the following code. The resulting excel sheet only contains the header, but not the contact names... What am I doing wrong?

 

<apex:page controller="MyController" cache="true" contenttype="application/x-excel#filename.xls" showheader="false">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
</head>
    <apex:pageBlock >
<apex:pageBlockTable value="{!contacts}" var="c" columns="1">
        
    <apex:column headerValue="Name">{!c.Name}</apex:column>
        
</apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

 

Thank you,

Josh :-)

baller4life7baller4life7

My bad. I fixed it.

tunnatunna

Hi Guys,

I have tried the Above code. its fine for few datas, but  I am having 1000+ Records including 35 Colomns.I unable to export the Data  to excel, Data is exported but unable to read, When i was trying to read the exported excel file its showing message that "Unable to read file". but in 2010v of Excel its working fine but 2003 its showing this error message.due to lots of data.

 

Please any body suggest me how can i solve this problem in 2003v of Excel

 

Thanks & Regards

Bijay Kumar