Java POI Excel 编码 - 调试与生产
Java POI Excel encoding - Debug vs Production
我正在编写一个简单的程序,可以将 excel 页面从希伯来语翻译成英语。
为此,代码读取每个单元格的内容并将其与从简单的 csv 文件获取信息的地图进行比较。
运行 来自 IntelliJ 的程序,该程序完美运行并执行其应有的功能,但是将其编译为 jar,该程序不会那样做。
//Code for loading the csv contents to a map
private static Map<String,String> getLocalization(String pathToJar) {
String path = null;
path = pathToJar + "localization.csv";
String line = "";
HashMap<String, String> list = new HashMap<>();
try {
BufferedReader br = new BufferedReader(new FileReader(path));
while ((line = br.readLine()) != null) {
// use comma as separator
String[] array = line.split(",");
list.put(array[0], array[1]);
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
//Code for loading an Excel file and translating it
private static boolean updateExcel(Map<String,String> translation, String filepath, String pathToJar) {
String path = pathToJar + "temp\week.xlsx";
//Read Excel document first
FileInputStream input_document = null;
XSSFWorkbook my_xlsx_workbook = null;
try {
input_document = new FileInputStream(new File(path));
// convert it into a POI object
my_xlsx_workbook = new XSSFWorkbook(input_document);
} catch (IOException e) {
e.printStackTrace();
}
// Read excel sheet that needs to be updated
XSSFSheet my_worksheet = null;
if (my_xlsx_workbook != null) {
my_worksheet = my_xlsx_workbook.getSheetAt(0);
}
for (Row cells : my_worksheet) {
String name = "";
String shortCode = "";
//Get the row object
Row row = cells;
//Every row has columns, get the column iterator and iterate over them
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
//Get the Cell object
Cell cell = cellIterator.next();
//check the cell type and process accordingly
switch (cell.getCellType()) {
case STRING:
for (Map.Entry<String, String> entry : translation.entrySet()) {
if (cell.getStringCellValue().contains(entry.getKey())) {
cell.setCellValue(entry.getValue());
}
}
break;
}
}
}
my_worksheet.autoSizeColumn(1);
FileOutputStream outFile = null;
try {
if (input_document != null) {
input_document.close();
}
File finishedFile = new File(path);
outFile = new FileOutputStream(finishedFile);
my_xlsx_workbook.write(outFile);
outFile.close();
finishedFile.renameTo(new File(filepath));
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
csv 文件类似于:
hebrew_word,english_word
hebrew_word,english_word
hebrew_word,english_word
...
我检查了一些事情:
1. 正在从文件中读取地图(尝试将其编码为 UTF-8)
2.IntelliJ的设置设置为UTF-8
我认为这是一个编码问题,将地图的键输出到 excel 单元格显示了一堆特殊字符而不是单词。
如果需要任何其他信息,请告诉我,在此先感谢。
FileReader
是一个旧的实用程序 class,它使用默认的平台编码。 运行 在 IntelliJ 中,你说它被设置为 UTF-8,文件的编码也是如此。在 IntelliJ 之外,它取决于机器。 Windows到现在还没有使用UTF-8。
可以使用 Files.lines
因为 java 8:
try {
Path p = Paths.get(path);
Files.lines(p)
.map(line ->line.split(",\s*"))
.filter(array -> array.length >= 2)
.forEach(array -> list.put(array[0], array[1]));
} catch (IOException e) {
e.printStackTrace();
}
Files.lines
也可以传递一个字符集,但新的默认值是 UTF-8。
我正在编写一个简单的程序,可以将 excel 页面从希伯来语翻译成英语。
为此,代码读取每个单元格的内容并将其与从简单的 csv 文件获取信息的地图进行比较。
运行 来自 IntelliJ 的程序,该程序完美运行并执行其应有的功能,但是将其编译为 jar,该程序不会那样做。
//Code for loading the csv contents to a map
private static Map<String,String> getLocalization(String pathToJar) {
String path = null;
path = pathToJar + "localization.csv";
String line = "";
HashMap<String, String> list = new HashMap<>();
try {
BufferedReader br = new BufferedReader(new FileReader(path));
while ((line = br.readLine()) != null) {
// use comma as separator
String[] array = line.split(",");
list.put(array[0], array[1]);
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
//Code for loading an Excel file and translating it
private static boolean updateExcel(Map<String,String> translation, String filepath, String pathToJar) {
String path = pathToJar + "temp\week.xlsx";
//Read Excel document first
FileInputStream input_document = null;
XSSFWorkbook my_xlsx_workbook = null;
try {
input_document = new FileInputStream(new File(path));
// convert it into a POI object
my_xlsx_workbook = new XSSFWorkbook(input_document);
} catch (IOException e) {
e.printStackTrace();
}
// Read excel sheet that needs to be updated
XSSFSheet my_worksheet = null;
if (my_xlsx_workbook != null) {
my_worksheet = my_xlsx_workbook.getSheetAt(0);
}
for (Row cells : my_worksheet) {
String name = "";
String shortCode = "";
//Get the row object
Row row = cells;
//Every row has columns, get the column iterator and iterate over them
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
//Get the Cell object
Cell cell = cellIterator.next();
//check the cell type and process accordingly
switch (cell.getCellType()) {
case STRING:
for (Map.Entry<String, String> entry : translation.entrySet()) {
if (cell.getStringCellValue().contains(entry.getKey())) {
cell.setCellValue(entry.getValue());
}
}
break;
}
}
}
my_worksheet.autoSizeColumn(1);
FileOutputStream outFile = null;
try {
if (input_document != null) {
input_document.close();
}
File finishedFile = new File(path);
outFile = new FileOutputStream(finishedFile);
my_xlsx_workbook.write(outFile);
outFile.close();
finishedFile.renameTo(new File(filepath));
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
csv 文件类似于:
hebrew_word,english_word
hebrew_word,english_word
hebrew_word,english_word
...
我检查了一些事情:
1. 正在从文件中读取地图(尝试将其编码为 UTF-8)
2.IntelliJ的设置设置为UTF-8
我认为这是一个编码问题,将地图的键输出到 excel 单元格显示了一堆特殊字符而不是单词。
如果需要任何其他信息,请告诉我,在此先感谢。
FileReader
是一个旧的实用程序 class,它使用默认的平台编码。 运行 在 IntelliJ 中,你说它被设置为 UTF-8,文件的编码也是如此。在 IntelliJ 之外,它取决于机器。 Windows到现在还没有使用UTF-8。
可以使用 Files.lines
因为 java 8:
try {
Path p = Paths.get(path);
Files.lines(p)
.map(line ->line.split(",\s*"))
.filter(array -> array.length >= 2)
.forEach(array -> list.put(array[0], array[1]));
} catch (IOException e) {
e.printStackTrace();
}
Files.lines
也可以传递一个字符集,但新的默认值是 UTF-8。