【问题标题】:unable to export thai character into excel无法将泰语字符导出到excel
【发布时间】:2011-01-12 00:24:54
【问题描述】:

使用此代码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@page import="java.io.*"%>
<%@page import="com.db.action.SearchFormDBImage"%>
<%@ page import=" java.util.*"%>
<%@page import ="org.apache.poi.hssf.usermodel.HSSFSheet"%>
<%@page import ="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
<%@page import ="org.apache.poi.hssf.usermodel.HSSFRow"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="refresh" content="600;URL=Logout"/>
<title>ExportToExcel</title>
</head>
<body >
 <form name="" method="post" id="exportForm" >
 <%! ServletOutputStream stream = null;
  FileOutputStream fout    = null;
  RequestDispatcher dispatcher=null;
  String userID="";
  String CountryCode="";
 %>
 <% 
 userID=(String)session.getAttribute("userID");
 CountryCode=(String)session.getAttribute("CountryCode"); 
 if(userID==null || CountryCode==null ){
  response.sendRedirect("SelectCountry.jsp");
 }
 else{
 %>
 <%
 try
 {
  response.setHeader("Pragma", "");
  response.setHeader("Expires","0");
  response.setHeader("Cache-Control", "");
  String filename = "SearchResults.xls";  
  response.setHeader("content-disposition","attachment; filename="+filename);
  response.setContentType("application/excel");
  OutputStream stream = null;
  HSSFWorkbook wb=null;
  HSSFRow row=null;
  ArrayList alSearch;
  List list_records=(List)session.getAttribute("alSearch"); 
  if (list_records != null && !list_records.isEmpty()) 
  {
   int size =list_records.size();
   wb = new HSSFWorkbook();
   HSSFSheet sheet = wb.createSheet("new sheet");
   row = sheet.createRow((short)0);      
   row.createCell((short)(0)).setCellValue("Business Date");   
   row.createCell((short)(1)).setCellValue("Work Source ID");   
   row.createCell((short)(2)).setCellValue("Batch Number");   
   row.createCell((short)(3)).setCellValue("Trans ID");   
   row.createCell((short)(4)).setCellValue("Item Type");   
   row.createCell((short)(5)).setCellValue("Amount");   
   row.createCell((short)(6)).setCellValue("Stub Account Number");
   row.createCell((short)(7)).setCellValue("Invoice No"); 
   row.createCell((short)(8)).setCellValue("Cheque Acct Number");   
   row.createCell((short)(9)).setCellValue("Cheque Number");   
   row.createCell((short)(10)).setCellValue("Bank Branch Code"); 
   row.createCell((short)(11)).setCellValue("Credit Note Indicator");
   row.createCell((short)(12)).setCellValue("Unique Identifier");
   row.createCell((short)(13)).setCellValue("Hope");
   int rowIncrement=1;
   int index=0;
    for(index = 0; rowIncrement<=size; index++) 
    {     
     com.db.bean.Bean obj=(com.db.bean.Bean)list_records.get(index);
     row     = sheet.createRow((short)rowIncrement);     
     row.createCell((short)(0)).setCellValue((String)obj.getBusDate());     
     row.createCell((short)(1)).setCellValue((String)obj.getWSIDDesc());     
     row.createCell((short)(2)).setCellValue((String)obj.getBatchNum());     
     row.createCell((short)(3)).setCellValue((String)obj.getTransID());     
     row.createCell((short)(4)).setCellValue((String)obj.getItemType());      
     String AmountPaid="";
     AmountPaid=(String)obj.getAmtPaid();    
     if(AmountPaid.length()>2 ){
      AmountPaid=AmountPaid.substring(0,AmountPaid.length()-2)+"."+AmountPaid.substring(AmountPaid.length()-2,AmountPaid.length());
     }
        if(AmountPaid.length()==1){
          AmountPaid="0.0"+AmountPaid;
         }
        if(AmountPaid.length()==2){
            AmountPaid="0."+AmountPaid;
           }
     row.createCell((short)(5)).setCellValue(AmountPaid);     
     row.createCell((short)(6)).setCellValue((String)obj.getStubAccNum());
     row.createCell((short)(7)).setCellValue((String)obj.getInvoiceNo());
     row.createCell((short)(8)).setCellValue((String)obj.getChequeAccNum());     
     row.createCell((short)(9)).setCellValue((String)obj.getChequeNum());     
     row.createCell((short)(10)).setCellValue((String)obj.getBankBranchCode());
     row.createCell((short)(11)).setCellValue((String)obj.getCni()); 
     row.createCell((short)(12)).setCellValue((String)obj.getUI());    
     row.createCell((short)(13)).setCellValue((String)obj.getEField2Value());
     rowIncrement++;
    }
    stream = response.getOutputStream();
    wb.write(stream);     
    stream.flush();
  }
  else
  {
    response.sendRedirect("SelectCountry.jsp");
  }
 }
 catch ( Exception ex )
  {
   ex.printStackTrace();
   System.out.println("Exception caught while generating the excel report: "+ex);
     response.sendRedirect("ExcelNotFound.jsp");
  } 
 finally
 {
  try{
     stream.close();
     session.removeAttribute("alSearch");
  }
  catch ( Exception ex )
  { 
   ex.printStackTrace(); 
   System.out.println("Exception caught while closing the the excel stream: "+ex);
     } 
 }
 %>
 <%}%>
</body>
</html>

我将泰语字符设置为 45IA,而它应该是 ซิตี้แบงก์。

【问题讨论】:

  • 航海者,我永远感激你。
  • 看到iso-8859-1编码在你的 jsp 指令中是麻烦的第一个迹象。

标签: java excel internationalization cjk


【解决方案1】:

嘿,我也遇到了同样的问题。

我已将系统设置为接受从右到左的语言,但我的希伯来语字符没有显示,而是显示为一堆十六进制。我已将 Eclipse 项目定义设置为默认为 utf-8,但浏览器上仍然显示了一堆十六进制。

然后瞧!我也将 jsps/servlet 设置为接受 utf-8。

要么有这个(用于一般输出),

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

或者这个(用于html输出),

<%@ page contentType="text/html; charset=UTF-8" %>

或(对于 excel 电子表格流),

<%@ page contentType="application/vnd.ms-excel; charset=UTF-8" %>

在你的jsp中。

在 http 响应对象上有 servlet 的等效设置。例如,

setContentType( "text/html; charset=UTF-8" );

你看,ISO-8859-1 是默认编码,我发现它对国际化毫无用处,为什么当网页是其中的一部分时,网络仍然会继续使用它作为默认编码,如果不是主要的-全球化运动的推动者。

也许,您也可以在 tomcat 中进行以下配置,因为 http 请求参数是 get 方法中 url 的一部分。如果你不这样做,你必须在你的 web 应用程序中只允许 post 方法,如果你有泰语的 html 表单或 http 请求参数。

在 $CATALINA_HOME/conf/server.xml,添加以下行,其中 port# 是您的 http 服务的端口:

<Connector port="port#" URIEncoding="UTF-8"/>

【讨论】:

    【解决方案2】:

    我相信您需要在 Windows 中安装亚洲语言包。奇怪的字母和数字通常意味着您缺少包含该字符的字体(大多数字体不支持印地语/中文/泰语字符)。

    【讨论】:

    • iam 得到正确的结果(ซิตี้แบงก์,ถ.รัชดาภิเษก 3 (เทเลคอมทาวเ)ว)但是在导出 iam 时遇到问题。
    • 您要导出到什么位置?如果不是 Unicode,则无法表示字符,因此必须以这种方式对其进行特殊编码。
    【解决方案3】:
    1. 确定 HSSFWorkbook 库支持国际字符吗?有很多 Excel 库没有。一个是当前版本的 perl Excel 库。它可以很好地写入 I18N 个字符。顺便说一句,Excel 的默认字符集在 Win XP SP 2 和 Excel 2003 中可以很好地处理中文和许多其他语言。MS Arial 覆盖了大量的 Unicode 空间,只是为了消除这些字符集问题。

    2. 如果问题是您的 lib 不支持 I18N,请查看 workaround 它不漂亮但运行良好。

    3. 我注意到您将页面设置为 ISO-8859-1 字符集。确定不应该是 UTF-8?

    【讨论】:

      【解决方案4】:

      感谢您提供解决方案。 我尝试使用以下代码。它对我来说很好用。

      POI 支持泰国(和任何 Unicode 字符)以很好地编写 Excel 表格。只需将单元格编码设置为 UTF-16(默认情况下它是不支持中文的 ISO-8859-1),并使用实际具有您要显示的字符的字体(例如 Arial Unicode MS)。例如:

      HSSFWorkbook wb = new HSSFWorkbook();
      HSSFSheet sheet = wb.createSheet("New sheet");
      HSSFFont font = wb.createFont();
      font.setFontName("Arial Unicode MS");
      HSSFCellStyle style = wb.createCellStyle();
      style.setFont(font);
      HSSFRow row = sheet.createRow((short) 0);
      HSSFCell cell = row.createCell((short) 0);
      cell.setEncoding(HSSFCell.ENCODING_UTF_16);
      cell.setCellStyle(style);
      cell.setCellValue("\u53f8"); 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-04
        相关资源
        最近更新 更多