Exporting Excel Files fails for Japanese Language

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Exporting Excel Files fails for Japanese Language

adi_300385
Hi DisplayTagTeam,

First of all, thank you for coming up with such a nice way of displaying tables in an excellent manner!

I came across a legacy code of ours which uses display tag 1.0. Need some help regarding the Export feature.

Problem:
For English locale, it works just fine. The export.xls file shows all the content, including the column headers correctly.
However, for Japanese locale, the headers (which should be in Japanese Language) are mangled.

Observations:
  1. [Opening With] Notepad, the export.xls file has the correct textual contents.
    Eg. Like this: [Note: My browser shows Japanese content here in the headers]
    "メール" "ユーザ名SoftwareSuper" "ユーザ管理グループ" "役割"
    "[hidden email]" "10013" "" "Administrator 
    "

    But the expected behaviour should be that, an xls file is not a text file and is heavily encoded and thus note readable from a text editor.
    [Question] What is causing the problem here? or When can this problem happen? I would like to encode the xls file just as a standard Windows excel file, instead of a simple text open-able file. What should I do or take care of?

  2. Some logs:

    13-Feb-2012 11:34:02 DEBUG [org.displaytag.filter.ExportDelegate.writeExport : 115] :: http-8080-Processor23 :: [Aditya] Header = Content-Disposition attachment; filename="report.xls"
    13-Feb-2012 11:34:02 DEBUG [org.displaytag.filter.ExportDelegate.writeExport : 116] :: http-8080-Processor23 :: [Aditya] ISO-8859-1 : text/html; charset=UTF-8
    13-Feb-2012 11:34:02 DEBUG [org.displaytag.filter.ExportDelegate.writeExport : 132] :: http-8080-Processor23 :: [Aditya] UTF-8
    13-Feb-2012 11:34:02 DEBUG [org.displaytag.filter.ExportDelegate.writeExport : 133] :: http-8080-Processor23 :: [Aditya] application/vnd.ms-excel; charset=UTF-8
    13-Feb-2012 11:34:02 DEBUG [org.displaytag.filter.ExportDelegate.writeExport : 156] :: http-8080-Processor23 :: [Aditya] inside String condition

    ExcelView class, by default, treats the content for Excel export as text. Is that causing the problem? Pls clarify a bit.

    public class ExcelView extends BaseExportView implements TextExportView { }
Thanks,
Aditya



------------------------------------------------------------------------------
Try before you buy = See our experts in action!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-dev2
_______________________________________________
displaytag-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/displaytag-user
Reply | Threaded
Open this post in threaded view
|

Re: Exporting Excel Files fails for Japanese Language

Michael-638
> Hi *DisplayTagTeam*,
>
> First of all, thank you for coming up with such a nice way of displaying
> tables in an excellent manner!
>
> I came across a legacy code of ours which uses display tag 1.0. Need some
> help regarding the *Export *feature.
>
> *Problem:*
> For English locale, it works just fine. The export.xls file shows all the
> content, including the column headers correctly.
> However, for Japanese locale, the headers (which should be in Japanese
> Language) are mangled.
>
> Observations:
>
>    1. [Opening With] Notepad, the export.xls file has the correct textual
>    contents.
>    Eg. Like this: [Note: My browser shows Japanese content here in the
>    headers]
>    "メール" "ユーザ名SoftwareSuper" "ユーザ管理グループ"
> "役割"
>    "[hidden email]" "10013" "" "Administrator
>    "
>
>    But the expected behaviour should be that, *an xls file is not a text
>    file and is heavily encoded and thus note readable from a text editor.*
>    [Question] What is causing the problem here? or When can this problem
>    happen? I would like to encode the xls file just as a standard Windows
>    excel file, instead of a simple text open-able file. What should I do
> or
>    take care of?

1. Make sure your JSP ist in UTF-8
2. Switch to CSV
3. Use my custom implementation:

import org.apache.commons.lang.StringUtils;
import org.displaytag.export.CsvView;

/**
 * Excelkompatibler CSV-Export:
 * <ol>
 * <li>Feldtrenner muss ein Semikolon sein.</li>
 * <li>Bei UTF-8 Kodierung <strong><a
 * href="http://wiki.sdn.sap.com/wiki/display/ABAP/Excel+files+-+CSV+format"
 * >muss</a></strong> der BOM vorhanden sein, sonst ist die Datei für Excel
 * nicht sauber lesbar. Obwohl der BOM <a
 * href="http://unicode.org/faq/utf_bom.html#bom5">nicht empfohlen</a> wird bei
 * UTF-8</li>.
 * </ol>
 */
public class ExcelCompatibleCsvView extends CsvView {

        @Override
        protected String getDocumentStart() {
                // Write Byte Order Mark
                return "\ufeff";
        }

        @Override
        protected String getCellEnd() {
                return ";";
        }

        @Override
        protected String escapeColumnValue(Object value) {
                String stringValue = StringUtils.trim(value.toString());
                if (!StringUtils.containsNone(stringValue, new char[] { '\n', ';' })) {
                        return "\"" + //$NON-NLS-1$
                                        StringUtils.replace(stringValue, "\"", "\\\"") + "\""; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                }

                return stringValue;
        }

}
--
Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir
belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de

------------------------------------------------------------------------------
Try before you buy = See our experts in action!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-dev2
_______________________________________________
displaytag-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/displaytag-user