Java将CSV转换为XLSX
我正在尝试创建一个可以转换的程序。csv文件到。使用ApachePOI的xlsx文件。一切都很好,只不过那辆车坏了。生成的xlsx文件不能正确显示带有umlaut(ü,ä,ö)的德语字母。它只是打印一个问号。有谁能帮我做这件事吗? 非常感谢您的帮助
这是我写的代码。我不是Java方面的专家,所以如果我的代码看起来很基本,我会提前道歉
private static void csvtoxlsx() {
try{
String csvfile = "test.csv";
String xlsxfile = "test-changed.xlsx";
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("sheet1");
XSSFFont xssfFont = workbook.createFont();
xssfFont.setCharSet(XSSFFont.ANSI_CHARSET);
XSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(xssfFont);
String currentLine;
int RowNum = 0;
BufferedReader br = new BufferedReader(new FileReader(csvfile));
while((currentLine = br.readLine()) != null){
String[] str = currentLine.split(";");
RowNum++;
XSSFRow currentRow = sheet.createRow(RowNum);
for(int i=0; i< str.length; i++){
str[i] = str[i].replaceAll("\"","");
str[i] = str[i].replaceAll("=","");
XSSFCell cell = currentRow.createCell(i);
cell.setCellStyle(cellStyle);
cell.setCellValue(str[i].trim());
}
}
FileOutputStream fileOutputStream = new FileOutputStream(xlsxfile);
workbook.write(fileOutputStream);
fileOutputStream.close();
System.out.println("success");
}catch (Exception e){
e.printStackTrace();
}
}
# 1 楼答案
您可以在BufferedReader中传递如下所示的编码器,这样就可以按照预期正确读取字符
参考:https://docs.oracle.com/javase/7/docs/technotes/guides/intl/encoding.doc.html