2010-02-02 50 views
1

使用此代碼無法泰語字符導出到Excel

<%@ 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,當它應該像ซิตี้แบงก์。

+0

旅行者,你有我永恆的感激之情。 – BobMcGee 2010-02-02 04:28:11

+0

<%@ page language =「java」contentType =「text/html; charset = ISO-8859-1」 pageEncoding =「ISO-8859-1」%> 在您的jsp指令中看到iso-8859-1編碼是麻煩的第一個跡象。 – 2010-02-02 07:19:59

回答

0

我相信你需要在Windows中安裝亞洲語言包。奇怪的字母和數字通常意味着您缺少包含該字符的字體(大多數字體不支持印地語/中文/泰文字符)。

+0

iam在網格中獲得正確的結果(ซิตี้แบงก์,。รัชดาภิษษก3(เทเลคอมทาวเวอวรร์))。但是出口Iam出現問題。 – Ravikumar 2010-02-02 04:32:15

+0

你要輸出什麼?如果它不是Unicode,則不能表示該字符,因此必須以該方式對其進行編碼。 – BobMcGee 2010-02-02 04:37:35

0
  1. 肯定的HSSFWorkbook LIB支持國際字符?有很多Excel庫在那裏沒有。一個是perl Excel庫的當前版本。它寫入I18N字符就好了。順便說一下,Excel的默認字符集在Win XP SP 2和Excel 2003中處理中文和許多其他語言都很好.MS Arial覆蓋了大量的Unicode空間,以消除這些類型的字符集問題。

  2. 如果問題是您的lib不支持I18N,請參閱workaround這不太好,但效果很好。

  3. 我注意到你的頁面設置爲ISO-8859-1字符集。當然,不應該是UTF-8?

1

嘿,我有同樣的問題。

我已經設置我的系統接受從右到左的語言,但我的希伯來字符沒有顯示出來,而是一堆十六進制。我已經將eclipse項目定義默認設置爲utf-8,但仍然在瀏覽器上顯示一堆十六進制。

然後瞧!我將jsps/servlets設置爲接受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是默認enconding,我發現這是國際化很沒用,爲什麼網仍然會繼續使用它作爲默認時,網頁的一部分即使不是全球化運動的主要推動者。

也許,你也可以在tomcat上執行以下配置,因爲http請求參數是get方法中url的一部分。如果你不這樣做,你必須只允許你的web應用程序中的post方法,如果你有泰國的html表單或http請求參數。

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

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

感謝您提供解決方案。 我試着用下面的代碼。它對我來說工作得很好。

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");