Foren

Excel and csv codification

Daniel G, geändert vor 6 Jahren.

Excel and csv codification

Regular Member Beiträge: 141 Beitrittsdatum: 14.03.17 Neueste Beiträge
Hi everyone.

I am facing an issue with the codification of csv.

I made a portlet which is a form, and their results can be exported as csv file.
The file which results works fine when I open it with notepad, but when I open it with Microsoft Excel it fails because of the codification.

I was reading about this and most of the solutions that I found were about changing excel configurations.

I need to know if there is any way of codification which allows me to create directly a file which can be read by Microsoft Excel without codification mistakes.

For creating it, I use ContentTypes.APPLICATION_TEXT. Maybe I have to use another content type, but I tried with others and I couldn't make it work.

Thanks in advance.
thumbnail
Olaf Kock, geändert vor 6 Jahren.

RE: Excel and csv codification

Liferay Legend Beiträge: 6396 Beitrittsdatum: 23.09.08 Neueste Beiträge
Daniel G:
I need to know if there is any way of codification which allows me to create directly a file which can be read by Microsoft Excel without codification mistakes.


Yes.













(in order to give an answer with more details it would help to see the code that fails for you - best in a minimal example that demonstrates the problem)
Daniel G, geändert vor 6 Jahren.

RE: Excel and csv codification

Regular Member Beiträge: 141 Beitrittsdatum: 14.03.17 Neueste Beiträge
First of all, thanks for the help. I was trying to explain the way I do it.

I create the file with this:


String fileName ="Name"+".csv";
byte[] bytes = sb.toString().getBytes();
String contentType = ContentTypes.APPLICATION_TEXT;
 PortletResponseUtil.sendFile(request, response,fileName, bytes, contentType);


Then, I did this to include the data in the file

StringBundler sb = new StringBundler();
ResourceBundle rb = ResourceBundle.getBundle("content.Language", request.getLocale());


I take the questions of the form from the Language.properties. They are encode like this: if I want to write Holá, I do it this way: Holá
The answers are took from the portlet, which are written by the user.

I read from the properties with this:
String q = HtmlUtil.extractText(LanguageUtil.get(rb, objects.getString(elementName)));



With this way, I get a csv with good encode for opening it with notepad, but excel can't read it well.

If you have any doubt, ask me please.

Thanks for all
thumbnail
Olaf Kock, geändert vor 6 Jahren.

RE: Excel and csv codification

Liferay Legend Beiträge: 6396 Beitrittsdatum: 23.09.08 Neueste Beiträge
Well, with this code it's hard to reproduce. Where are you sending the content? In an action handler? Resource handler? rendering? Have you saved it to a file and opened the file with excel, instead of sending it through HTTP? What's the character set/encoding that this file is created in - you might want to state that for the downloaded content as well.