如何从XLSX文件读取汉字? (Java) [英] How read Chinese Characters from XLSX File? (Java)

查看:97
本文介绍了如何从XLSX文件读取汉字? (Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经可以从xlsx单元中读取文本,并且具有:

I can already read in texts from xlsx cells and have:

String s = cell.getStringCellValue();

但是,当打印出此String时,我得到了垃圾结果.为了解决这个问题,我使用了Internet.

However when printing out this String, I get rubbish results. To solve this problem I used the Internet.

我尝试了大约8种不同的方法,因此发现尚无关于SO的可行答案.我将我的IDE和XLSX文件的默认编码设置为UTF-8.拼音可以正确显示.

I tried about 8 different approaches and thus found that there is not yet a working answer on SO. I set the default encoding of my IDE and my XLSX Files to UTF-8. Pinyin can be correctly displayed.

有人知道什么可能是错误的,以及如何解决此问题?

Does anyone have an idea what could be wrong and how to solve this issue?

推荐答案

不清楚您使用汉字的问题来自何处,但我无法重现.

Not clear wherever your problem using chinese characters comes from, but I cannot reproduce it.

我在Excel中有以下工作簿:

I have the following workbook in Excel:

以下简单代码:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

import java.io.FileInputStream;

class ReadXSSFUnicodeTest {

 public static void main(String[] args) {
  try {

   Workbook wb = WorkbookFactory.create(new FileInputStream("ReadXSSFUnicodeTest.xlsx"));

   Sheet sheet = wb.getSheetAt(0);

   for (Row row : sheet) {
    for (Cell cell : row) {
     String string = cell.getStringCellValue();
     System.out.println(string);
    }
   }

   wb.close();

  } catch (Exception ex) {
   ex.printStackTrace();
  }
 }
}

产生:

如果问题是Windows无法在CMD控制台中正确显示Unicode字符,因为它没有带有字形的字体,则将内容写入文本文件:

If the problem is that Windows is not able displaying Unicode characters properly in CMD console because it has not a font with glyphs for it, then write the content to a text file:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

import java.io.FileInputStream;
import java.io.Writer;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.FileOutputStream;

class ReadXSSFUnicodeTest {

 public static void main(String[] args) {
  try {

   Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("ReadXSSFUnicodeTest.txt"), "UTF-8"));

   Workbook wb = WorkbookFactory.create(new FileInputStream("ReadXSSFUnicodeTest.xlsx"));

   Sheet sheet = wb.getSheetAt(0);

   for (Row row : sheet) {
    for (Cell cell : row) {
     String string = cell.getStringCellValue();
     out.write(string + "\r\n");
     System.out.println(string);
    }
   }
   out.close();   

   wb.close();

  } catch (Exception ex) {
   ex.printStackTrace();
  }
 }
}

此文件即使在Windows记事本中也应具有适当的内容:

This file then should have proper content even in Windows Notepad:

您还可以使用Swing(JTextArea)为测试输出提供自己的输出区域:

You could also using Swing (JTextArea) to provide your own output area for test outputs:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

import java.io.FileInputStream;
import java.io.Writer;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.FileOutputStream;

import javax.swing.*;
import java.awt.*;


class ReadXSSFUnicodeTest {

 public ReadXSSFUnicodeTest() {
  try {

   MySystemOut mySystemOut = new MySystemOut();

   Workbook wb = WorkbookFactory.create(new FileInputStream("ReadXSSFUnicodeTest.xlsx"));

   Sheet sheet = wb.getSheetAt(0);

   for (Row row : sheet) {
    for (Cell cell : row) {
     String string = cell.getStringCellValue();
     //System.out.println(string);
     mySystemOut.println(string);
    }
   }

   wb.close();

  } catch (Exception ex) {
   ex.printStackTrace();
  }
 }

 public static void main(String[] args) {
  ReadXSSFUnicodeTest readXSSFUnicodeTest= new ReadXSSFUnicodeTest();
 }

 private class MySystemOut extends JTextArea {

  private String output = "";

  private MySystemOut() {
   super();  
   this.setLineWrap(true);
   JFrame frame = new JFrame("My System Outputs");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   JScrollPane areaScrollPane = new JScrollPane(this);
   areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
   areaScrollPane.setPreferredSize(new Dimension(350, 150));
   frame.getContentPane().add(areaScrollPane, BorderLayout.CENTER);
   frame.pack();
   frame.setVisible(true);  
  }

  private void println(String output) {
   this.output += output + "\r\n";
   this.setText(this.output);
   this.revalidate();
  }
 }
}

这只是最简单的方法,而且只能获取测试输出,因为就AWT线程问题而言,它使用Swing并不是正确的方法.

This is only the simplest way and only to get test outputs since it uses Swing not the right way in terms of AWT threading issues.

这篇关于如何从XLSX文件读取汉字? (Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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