• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to encode hindi characters from java class to xls(Libreoffice calc) file?  RSS feed

 
sarita chaudhary
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my java class I am exporting some records to xls file using table tag which contains hindi charcters and the problem is that the hindi characters are not encoded in excel sheet(Libreoffice calc) and giving garbage value as आननà¥ヘद विहार instead of hindi characters.
I searched for this a lot and from other forums I solved my problem of java class but encoding into xls file is not getting
solved.

The records using table tag which is sent to excel method is::

table = table <tr>
<td> masterList.get(j).getBus_stop_cd() </td>
<td> masterList.get(j).getBus_stop_nm()</td>
<td>masterList.get(j).getDevnagiri_nm() </td>
<td>masterList.get(j).getBus_stop_address() </td>
masterList.get(j).getGlobal_short_cd()</td></tr>


table = table + "</table>";
session.setAttribute("abc", table);

In this entire data of <table> tag is sent to dispexcel method through setting into session i.e using this 'session.setAttribute("abc", table) and the Devnagiri_nm which is iterated from list contains the hindi characters.

The method use for exporting to excel is:
public ActionForward dispExcel(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {


HttpSession session = request.getSession();
String fileType = "xls";
String filename = (String) session.getAttribute("filename");
response.setContentType("application/vnd.ms-excel; charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=\"" + filename + "." + fileType + "\"");
PrintWriter out2 = response.getWriter();
String n = (String) session.getAttribute("abc");

out2.print(n);
out2.flush();
out2.close();
return null;
}

Can anyone will tell me how to solve this problem?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first thing to note is that this code does not create any kind of XLS/XLSX file, but an HTML file. You are then relying on a spreadsheet application (like Excel or Calc) to be able to open it, especially on that application to treat UTF-8 characters correctly. I don't know whether that's a reasonable expectation, but if you want complete control over the generation of the spreadsheet, I advise to create an XLS/XLSX file to start with, using a library like Apache POI.
 
sarita chaudhary
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your instant reply.
.xls file is only getting created.But only thing is that hindi characters are not supported.
I also tried using FileOutputstream.While using this it was asking for the UTF-8 option in the pop menu but after clicking on OK it did not display any records,empty file was displayed.
So, I stuck to previous option only.
Can you explain me in detail how to do solve this using a library like Apache POI.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No need to explain, the POI site has plenty of documentation. Start reading at http://poi.apache.org/spreadsheet/how-to.html and http://poi.apache.org/spreadsheet/quick-guide.html
 
sarita chaudhary
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I mentioned in previous msg I have used this option.
while using this option the path of the file is given explicitl for e.g

FileOutputStream fileOut = new FileOutputStream("workbook.xls");

and in my coding I am passing the filename dynamically.It is getting the file but the problem is no contents are
displayed even after selecting UTF-8 option from the pop menu occurred.

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have posted no code using POI, so what is "this option"?

Maybe someone else knows how to create HTML that can be reliably imported by spreadsheet applications.
 
sarita chaudhary
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one give me other suggestions which will really work for
creating HTML that can be reliably imported by spreadsheet applications.
Because Apache POI option is not really working for me.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I doubt that any HTML can be reliably imported, especially across various spreadsheet apps.

If you told us how exactly you tried to use POI, and how it did not work, we might be able to help with that. That question would be suitable for the Other Open Source Projects forum.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!