有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何读取包含阿拉伯语列的excel

阅读excel工作表时,阿拉伯列显示为????其余英文栏目显示良好。我想utf-8问题我不知道我在哪里错过了什么。请做愉快的帮助

     FileInputStream fis = new FileInputStream(fileName);
    Workbook workbook = new XSSFWorkbook(fis);

System.out.println("Current Encoding " +
                    "::" + System.getProperty("file.encoding"));

即使在更改以下给定的am后,也会获得当前编码::Cp1252

netbeans 8.0.2

-J-Dfile.encoding=UTF-8在netbeans\u默认\u选项中添加

jsp(struts 1.3)

  <%@page pageEncoding="UTF-8"%>

     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

     <html:form action="/uploadApplicantAction"  method="post" acceptCharset="utf-8" 
    enctype="multipart/form-data">

雄猫8

在web中未注释。xml

<filter>
    <filter-name>setCharacterEncodingFilter</filter-name>
    <filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <async-supported>true</async-supported>
</filter>

<filter-mapping>
    <filter-name>setCharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

在tomcat服务器中添加了utf-8代码。xml

<Connector port="8080" protocol="HTTP/1.1" URIEncoding="UTF-8"
               connectionTimeout="20000"
               redirectPort="8443" />

共 (1) 个答案

  1. # 1 楼答案

    由于只有您的阿拉伯文文本被呈现为向后问号,因此似乎有可能:

    • 您读取文件的方式没有问题。我不认为你认为这可能是一个“utf-8问题”的建议是可能的,因为英文文本的翻译是正确的。如果存在编码/解码问题,您可能会在输出中看到replacement characters
    • 问题最可能的原因是输出所用的字体不支持阿拉伯语

    为了验证这一点,创建一个简单的Java应用程序,将一些阿拉伯语文本呈现到控制台非常简单:

    package arabicdemo;
    
    public class ArabicDemo {
    
        public static void main(String[] args) {
            // Use a font which supports Arabic, such as DejaVu Sans, Courier New or MS Arial Unicode.
            // - To set font in edit window: Tools > Options > Fonts & Colors > Syntax tab > Font
            // - To set font in Ouput window: Tools > Options > Miscellaneous > Output tab > Font 
            System.out.println("مرحبا بالعالم"); // "Hello world" in Arabic
        }
    
    }
    

    只需确保使用适当的字体,如代码示例注释中所述(因为您使用的是NetBeans)。下面是在NetBeans中运行的应用程序的屏幕截图,编辑窗口字体设置为Deja Vu Sans,输出窗口字体设置为Courier New

    ArabicTextInNetBeans

    在NetBeans的编辑和输出窗口中正确显示阿拉伯语文本后,请修改应用程序以使用相同的字体

    完成此操作后,在处理Excel文件时,应用程序的阿拉伯文本应正确呈现。如果没有,那么至少您已经消除了字体作为问题的潜在原因,因此请根据需要更新您的问题

    注释

    1. 您可能不希望/不需要在编辑窗口中修改字体。我只是为了完整性才提到它
    2. 您不应该设置-Dfile.encoding=UTF-8From a Java bug report in 2005

      The "file.encoding" property is not required by the J2SE platform specification; it's an internal detail of Sun's implementations and should not be examined or modified by user code. It's also intended to be read-only; it's technically impossible to support the setting of this property to arbitrary values on the command line or at any other time during program execution.

      The preferred way to change the default encoding used by the VM and the runtime system is to change the locale of the underlying platform before starting your Java program.

    3. 根据您在问题中提供的代码和配置详细信息,“UTF-8”设置在六个不同的位置。一旦应用程序正常工作,就值得花时间逐步删除它们,了解哪些设置是必需的,哪些不重要