我有一个 Excel 电子表格,其中包含许多“中文(zh-TW)”和“英语(en-US)”单词,这些数据需要使用 utf-8 编码导入到数据库表中。 我的第一次尝试是使用 Excel“另存为”功能将数据导出为 CSV。但是,由于Excel不允许更改“另存为”功能的字符编码,因此无法正确读取数据。
最佳答案
经过搜索,我发现完成任务的简单方法如下:
将 Excel 工作表另存为“Unicode 文本 (.txt)”(Excel 采用 UTF16 编码并使用 TAB 分隔每列。
下面是用于读取此 Excel unicode 导出文件内容的 Java 代码片段:
File dataFile = new File("my-unicode-file-exported.txt")); InputStreamReader isr = new InputStreamReader(new FileInputStream(dataFile), "UTF-16"); BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) { // splits by TAB String elements = line.split("\t"); // process each element String element_01 = elements[0]; String element_02 = elements[1]; ..... } br.close();
关于java - 如何使用utf-8导出Excel CSV并读入Java?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34583675/