留言板

Excel and csv codification

Daniel G,修改在6 年前。

Excel and csv codification

Regular Member 帖子: 141 加入日期: 17-3-14 最近的帖子
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,修改在6 年前。

RE: Excel and csv codification

Liferay Legend 帖子: 6403 加入日期: 08-9-23 最近的帖子
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,修改在6 年前。

RE: Excel and csv codification

Regular Member 帖子: 141 加入日期: 17-3-14 最近的帖子
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,修改在6 年前。

RE: Excel and csv codification

Liferay Legend 帖子: 6403 加入日期: 08-9-23 最近的帖子
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.