简体   繁体   English

POI Java将xlsx转换为CSV逗号和双引号

[英]POI Java to convert xlsx to CSV comma and double quotes

Trying to convert xlsx to CSV, where one column has multiple quotes and comma, when converting to CSV, it is separated to different cells instead of one cell. 尝试将xlsx转换为CSV,其中一列具有多个引号和逗号,当转换为CSV时,它被分隔到不同的单元格而不是一个单元格。 Below is scenario, 以下是场景,

实际数据

转换

The Title is moving to multiple, attaching the code 标题将移动到多个,并附加代码

if(cell.getStringCellValue().contains(",")){
              if (cell.getStringCellValue().contains("\"")){
                                String t= "\""+cell.getStringCellValue()+"\"";
                                data.append(t+",");
                                }
                                else{
                                data.append("\""+cell.getStringCellValue()+"\""+",");
                                }
                            }

You need to escape these special signs: 您需要转义这些特殊标志:

eg. 例如。 if you want to have values one and two,three as two columns in a CSV file enclose the second column in quotes: 如果要具有值onetwo,three则将CSV文件中的两列作为two,three列,将第二列用引号引起来:

one,"two,three"

To escape double quotes " in the column value encolse it with double quotes and use "" to escape the quotes inside the value. eg if you have a field with abc"def it should be escaped as: 为了逃避双引号"列中的值用双引号encolse并使用""如果你有一个字段逃避值内的引号如: abc"def应当转义为:

"abc""def"

So for your case it could be one liner instead of the whole if statement: 因此,对于您的情况,它可能是一个衬里而不是整个if语句:

data.append("\"").append(cell.getStringCellValue().replaceAll("\"", "\"\"")).append("\",");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM