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

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

问题描述

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

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

String s = cell.getStringCellValue();

然而,当打印出这个字符串时,我得到了垃圾结果.为了解决这个问题,我使用了互联网.

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();
  }
 }
}

这只是获得测试输出的最简单方法,因为它使用 Swing 在 AWT 线程问题方面不是正确的方法.

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 文件中读取汉字?(爪哇)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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