如何使用 POI 解析 Excel 文件中的 UTF-8 字符 [英] How to parse UTF-8 characters in Excel files using POI

查看:45
本文介绍了如何使用 POI 解析 Excel 文件中的 UTF-8 字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 POI 成功解析 XLS 和 XLSX 文件.但是,我无法从 Excel 电子表格中正确提取特殊字符,例如中文或日文等 UTF-8 编码字符.我已经想出了如何从 UTF-8 编码的 csv 或制表符分隔的文件中提取数据,但对 Excel 文件没有运气.有人可以帮忙吗?

I have been using POI to parse XLS and XLSX files successfully. However, I am unable to correctly extract special characters, such as UTF-8 encoded characters like Chinese or Japanese, from an Excel spreadsheet. I have figured out how to extract data from a UTF-8 encoded csv or tab delimited file, but no luck with the Excel file. Can anyone help?

(评论中的代码片段)

HSSFSheet sheet = workbook.getSheet(worksheet); 
HSSFEvaluationWorkbook ewb = HSSFEvaluationWorkbook.create(workbook); 
while (rowCtr <= lastRow && !rowBreakOut) 
{ 
    Row row = sheet.getRow(rowCtr);//rows.next(); 
    for (int col=firstCell; col<lastCell && !breakOut; col++) { 
      Cell cell; 
      cell = row.getCell(col,Row.RETURN_BLANK_AS_NULL); 
      if (ctype == Cell.CELL_TYPE_STRING) { 
         sValue = cell.getStringCellValue(); 
         log.warn("String value = "+sValue); 
         String encoded = URLEncoder.encode(sValue, "UTF-8"); 
         log.warn("URL-encoded with UTF-8: " + encoded); 
         ....

推荐答案

我在从 Excel 文件中提取波斯语文本时遇到了同样的问题.我正在使用 Eclipse,只需转到 Project -> Properties 并将文本文件编码"更改为 UTF-8 即可解决问题.

I had the same problem while extracting Persian text from an Excel file. I was using Eclipse, and simply going to Project -> Properties and changing the "text file encoding" to UTF-8 solved the problem.

这篇关于如何使用 POI 解析 Excel 文件中的 UTF-8 字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆